OS Notes Module 1 2
OS Notes Module 1 2
OS Notes Module 1 2
UNIT 1
Chapter-1: Introduction to Operating System, System Structures
Utilization Efficiency
User Convenience
Hardware
Operating system
Application programs
define the ways in which the system resources are used to solve the
computing problems of the users
Users
System view
Resource allocator
Control program
OS is a resource allocator
Decides between conflicting requests for efficient and fair resource use
OS is a control program
Controls execution of programs to prevent errors and improper use of the
computer
Everything else is either a system program (ships with the operating system) or an
application program
Single user cannot keep CPU and I/O devices busy at all times
Multiprogramming organizes jobs (code and data) so CPU always has one to execute
When it has to wait (for I/O for example), OS switches to another job and so on
CPU scheduling: the system must choose one job among several jobs ready to run.
Timesharing (multitasking):
Time-Sharing is a logical extension of Multiprogramming where many users share the
system simultaneously.
The CPU executes several jobs that are kept in memory by switching among them.
CPU switching is so fast that the user can interact with each program while it is
running.
If processes don’t fit in memory, swapping moves them in and out from the memory
à memory management
Operating-System Operations
OS is interrupt driven
Other process problems include infinite loop, processes modifying each other
or the operating system
User mode
A bit, mode bit, is added to computer hardware to indicate the current mode
Kernel (0)
User (1)
When a user process requests a service from OS via a system call, it transit from user
mode to kernel mode to fulfill the request
At boot time, hardware starts in kernel mode, then OS is loaded (mode bit = 0)
Whenever a trap occurs hardware switches from user to kernel mode (mode bit = 0)
When a user process requests a service from OS via a system call, it transit from user
mode to kernel mode to fulfill the request
Provides the means for protecting the OS from erroneous programs and
erroneous programs from each other.
If any attempt is made to execute privileged instructions in user mode, hardware does
not execute these instructions and treats as traps
MS-DOS written for Intel 8088 architecture which has no mode bit and
therefore no dual mode.
Pentium provides dual mode; and Windows, Linux, Solaris make use of the feature
and provide protection to OS
Timer:
A user program should not stuck in infinite loop i.e. not returning to OS.
The period may be fixed (1/60 sec) or variable (from 1 milisecond to 1 second)
At 0, an interrupt occurs
Set up timer before scheduling process to regain control or terminate program that
exceeds allotted time.
Services provided by OS
User Interface
Program execution
I/O operations
File-system manipulation
Communications
Error detection
Resource allocation
Accounting
User Interface
Batch Interface uses files for execution containing commands and directives
Graphics User Interface (GUI) is window and uses pointing device, keyboard,
menus etc.
Program execution
I/O operations
A running program may require I/O which may involve a file to load in
memory or an I/O device like keyboard
Since user programs cannot execute I/O operations directly due to efficiency
and protection purpose, the operating system must provide means to perform
I/O.
File-system manipulation
Communications
Error detection
OS needs to be constantly aware of possible errors
Debugging facilities can greatly enhance the user’s and programmer’s abilities
to efficiently use the system
Accounting
To keep track of which users use how much and what kinds of computer
resources
This record keeping may be used for account billing or for accumulating usage
statistics.
Concurrent processes should not interfere with each other or with OS itself
Authentication
Some systems (Unix) have multiple command interpreters, these interpreters are
known as shells
Commands are to manipulate the files as create, delete, list, print, copy, execute etc.
rm file.text
This command will search a file named rm and load in memory and execute it
Mouse is moved to position its pointer on images and icons on the screen that
represent programs, files, directories etc.
Clicking various mouse buttons over objects in the interface cause various
actions (provide information, options, execute function, open directory
(known as a folder)
Traditionally Unix has CLI but few GUI like Common Desktop (CDE) and X-
Windows are available with commercial version of Unix such as Solaris,
IBM’s AIX system.
Traditionally Unix has CLI but few GUI like Common Desktop (CDE) and X-
Windows are available with commercial version of Unix such as Solaris,
IBM’s AIX system.
Apple Mac OS X as “Aqua” GUI interface with UNIX kernel underneath and
shells available
System Calls:
System calls provide an interface to the services provided by the OS
These routines are written in a high-level language (C or C++), low level tasks may
be in assembly language
System call sequence to copy the contents of one file to another file
System calls are mostly accessed by programs via a high-level Application Program
Interface (API) rather than directly using system call.
Dept of ISE, DSCE Page 10
Course Name: Operating System Code: IPCC22IS34
The API specifies a set of functions that are available to an application programmer,
including the parameters that are passed with each function.
Three most common APIs are Win32 API for Windows, POSIX API for POSIX-
based systems (including all versions of UNIX, Linux, and Mac OS X), and Java API
for the Java virtual machine (JVM)
A program having APIs can be compiled and run on any system that support the same
API.
Actual system calls can be more detailed and it is difficult to work with them.
A run-time support system for most of the programming languages provides a system
call interface that serves as the link to system calls made available by OS.
A run-time support system provides a set of functions built into libraries included
with the compiler
The system call interface invokes intended system call in OS kernel and returns status
of the system call with its return value.
The programmers need not know about the system call implementation and just need
to obey API
Most of the details of OS interface are hidden from programmer by API and managed
by run-time support library (set of functions built into libraries included with
compiler)
More information like parameters is required than simply identity of desired system
call
Dept of ISE, DSCE Page 11
Course Name: Operating System Code: IPCC22IS34
Three general methods are used to pass parameters between a running program and
the operating system.
Store the parameters in a block of memory, and the memory address is passed
as a parameter in a register as in Linux, Solaris
Push the parameters onto the stack by the program, and pop off the stack by
operating system.
File management
Device management
Information maintenance
Communications
Process control
To halt the execution of a running program either normally (end) or abnormally
(abort), OS transfers the to command interpreter
For coordination of concurrent processes, system calls may be wait event, signal event
end, abort
load, execute
System Calls:
Example: MS DOS environment FreeBSD Running Multiple
Programs
File management:
Create and delete a file (or directory), need a file name and few attributes
Determine the values of various file attributes like file name, file type, protection
codes
open, close
Device management
A process needs several resources for execution like main memory, disk drives, files
etc.
All resources may be considered as devices which can be requested and released.
(same as open and close a file)
Information maintenance
Many system calls exist for the purpose of transferring the information between the
user program and OS
System calls to return information about the system such as number of current users,
OS version, amount of free memory or disk
System calls to return to get and set the information for files, processes, devices
Communications
Types of inter-process communication:
Message passing
Shared memory
Connection to be established
Processes can exchange the information by reading and writing the data in
shared memory
Shared memory is faster and can be done in same computer but synchronization is the
issue
User goal.
System goal.
User goals
Operating system should be convenient to use, easy to learn, reliable, safe, and
fast
System goals
Linux and Window XP are written in C but some code sections like device drivers
and saving & restoring the state of registers are written in assembly
Fast implementation
Compact
Easy to port
Reduced speed
Solution
Modern processors have deep pipelining and multiple functional units that can
handle complex dependencies.
A process needs certain resources, including CPU time, memory, files, and I/O
devices, to accomplish its task.
3.1.1The Process
A program is a passive entity i.e. a file containing a list of instructions stored on disk
called as executable file.
A process is an active entity with a program counter specifying the next instruction to
execute and a set of associated resources.
Entering the name of the executable file on the command line(ex prog.exe or
a.out)
A process includes:
Process in memory
Two processes may be associated with same program but they are not considered as
separate processes e.g. many copies of one mail program.
The state of a process is defined in part by the current activity of that process.
Process state
Program counter
CPU registers
Memory-management information
Accounting information
1. Process state
2. Program counter
The counter indicates the address of next instruction to be executed for this
process.
3. CPU registers
Along with the program counter, this state information must be saved when
an interrupt occurs, to allow the process to be continued correctly afterward.
This includes a process priority, pointers to scheduling queues, and any other
scheduling parameters.
5. Memory-management information
6. Accounting information
Amount of CPU and real time used, time limits, account numbers , job or
process numbers.
The objective of time sharing is to switch CPU among the processes so frequently that
users can interact with each process while it is running.
To meet these objectives, process scheduler selects an available process for execution
on CPU.
For a single-processor system, there will never be more than one running process,.
If there are more processes, the rest will have to wait until the CPU is free and can be
reshecduled.
3.2.1Scheduling Queues:
Job queue
Ready queues
Device queues
Ready queue
Set of all processes residing in main memory, ready and waiting to execute.
Header of the queue contains the pointers to first and last PCB.
A new process initially put in ready queue, where it waits for CPU for execution.
The process issues an I/O request, and then be placed in an I/O device queue.
The process creates a new subprocess and waits for its termination.
A process migrates between the various scheduling queues throughout its life time.
The operating system must select the processes from or to the scheduling queues.
Selects process from ready queue which should be executed next and allocates
CPU.
Medium-term scheduler
Swap in the process from secondary storage into the ready queue.
I/O-bound process – spends more time doing I/O than computations, many
short CPU bursts may be needed.
CPU-bound process – spends more time doing computations; long CPU bursts
are required.
System should have mix of both type of processes so I/O waiting queue and ready
queue will have equal and balanced work load.
Context Switch
Many users processes and system processes run simultaneously.
The system must save the state of the old process and load the saved state of the new
process.
Context-switch time is overhead and the system does no useful work while switching.
Switching speed varies from machine to machine depending upon memory, number of
registers and hardware support.
User data
Program counter
System stack
PCB
Contents:
Overview
Multithreading Models
Overview
Multithreaded Processes
If one application should be implemented as a set of related units of execution. Two
possible ways:
Multiple processes
Heavy weight
Less efficient
Multiple threads
A thread
shares a code section, data section, open files etc. with other threads belonging
to the same process.
Multithreaded Processes
A traditional (or heavy weight) process has a single thread of control.
If a process can have multiple threads, it can do more than one task.
Multiple threads
A multithreaded process contains several different flows of control within the same
address space.
A single PCB & user space are associated with the process and all the threads belong
to this process.
Each thread has its own thread control block (TCB) having following information:
Why multithreading
Use of traditional processes incurs high overhead due to process switching.
Unavoidable overhead of saving the state of running process and loading the
state of new process.
Use of threads splits the process state into two parts – resource state remain with the
process while execution state is associated with a thread.
This state needs to be saved and restored while switching between threads of a
process. The resource state remains with the process.
Resource state is saved only when the kernel needs to perform switching
between threads of different processes.
Examples
Web browser
Word processor
Displaying graphics
Benefits of Multithreading
Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
User threads are supported above the kernel and are managed without kernel support.
Examples:
POSIX Pthreads
Mach C-threads
Solaris threads
If a thread performs a blocking system call, the kernel can schedule another kernel
thread.
Windows XP
Solaris
Mac OS X
Tru64 UNIX
Linux
Multithreading Models
There must exist a relationship between user threads and kernel threads
Many-to-One
One-to-One
Many-to-Many
Many-to-One Model
Many user-level threads are mapped to single kernel thread.
It is fast, efficient.
Drawback: If one user thread makes a system call and only one kernel thread is
available to handle, entire process will be blocked.
One-to-One Model
Each user-level thread is mapped to one kernel thread
If blocking call from one user thread to one kernel thread, another kernel thread can
be mapped to other user thread.
Drawback: Creating a user thread requires the creation of corresponding kernel thread
which is overhead.
Windows 95/98/NT/2000/XP
Linux
Many-to-Many Model
Many user-level threads are mapped to a smaller or equal number of kernel threads.
Examples
IRIX
HP-UX
Tru64 UNIX
Scheduling Algorithms
Basic Concepts: Multiprogrammed execution
Program 1
Program 2
P1 P2 P1 P2 P1 P2
Basic Concepts
How to obtain maximum CPU utilization with multiprogramming?
CPU scheduling is the task of selecting a waiting process from the ready queue and
allocating the CPU to it.
CPU Scheduler
CPU Scheduler (short term scheduler) selects one process from the processes in
memory that are ready to execute, and allocates the CPU.
Ready queue may be implemented as FIFO queue, a tree or an ordered link where
PCBs are linked waiting for CPU to be available.
Terminates.
Nonpreemptive
No forceful switching from running state to waiting process.
Once CPU has been allocated to a process, the process keeps the CPU, process
switches from running state to waiting state only due to:
I/O request
Some OS service
Preemptive
Forceful switching from running state to waiting process.
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
Switching context
Jumping to the proper location in the user program to restart that program
Dispatch latency
It is the time taken by the dispatcher to stop one process and start another running.
Scheduling Criteria
Different CPU scheduling algorithms have different properties and choice of one
algorithm depends on different criteria.
CPU utilization – Aim is to keep the CPU as busy as possible (lightly loaded: 40%,
heavily loaded: 90%)
(High)
Throughput – number of processes that complete their execution per unit time (very
long process: 1 process/hour, short: 10 processes/sec)
(High)
Turnaround time – amount of time to execute a particular process (from submission to
completion, includes execution time, I/O time and waiting time)
(Least)
Waiting time – amount of time a process has been waiting in the ready queue (CPU
scheduling algorithm does not affect execution time and time required for I/O, it can
only reduce waiting time)
(Least)
Response time – amount of time it takes from when a request was submitted until the
first response is produced, not output (for time-sharing environment)
(Least)
Optimization Criteria for scheduling
Max CPU utilization
Max throughput
Scheduling Algorithms
First-Come, First-Served (FCFS)
Shortest-Job-First (SJF)
Priority Scheduling
When CPU is free, it is allotted to the process at the head of the queue.
It is non-preemptive.
P1 , P2 , P3
Gantt Chart for the schedule
P1 P2 P3
0 24 27 30
Waiting time for P1 = 0
P2 = 24
P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 51/3 = 17
P1 24
P2 3
P3 3
P2 , P3 , P1 .
P2 P3 P1
0 3 6 30
Waiting time for P1= 6; P2 = 0; P3 = 3
Convoy effect
Thus average waiting time in FCFS vary substantially if CPU burst time vary greatly.
All the processes wait for the one big process to get off the CPU.
CPU is assigned to the process with the shortest next CPU burst time.
If two processes have the same length CPU burst, FCFS is used to break the tie.
SJF is optimal and gives minimum average waiting time for a given set of processes.
Example of SJF
Process Burst Time
P1 6
P2 8
P3 7
P4 3
Gantt Chart for the schedule
P4 P1 P2
P3
0 3 9 16 24
Once CPU is given to the process it cannot be preempted until completes its
CPU burst.
Preemptive SJF
If a new process arrives with CPU burst length less than remaining time of
current executing process, current process is preempted. This scheme is know
as
Shortest-Remaining-Time-First (SRTF).
P3 4.0 1
P4 5.0 4
Gantt Chart for the schedule
P1 P3 P2 P4
0 7 8 12 16
Average waiting time = (0 + (7-4) + (8-2) + (12-5))/4 = 16/4 = 4
P1 P2 P3 P2 P4 P1
0 11 16
2 4 5 7
Average waiting time = (9 + 1 + 0 +2)/4 = 12/4 = 3ms
Priority Scheduling
A priority number (integer) is associated with each process.
The CPU is allocated to the process with the highest priority (smallest integer º
highest priority).
Preemptive
Nonpreemptive
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Scheduler will preempt the CPU if the priority of newly arrived process is higher than
priority of running process.
P1 6 3 0
P2 1 1 2
P3 2 4 3
P4 1 5 5
P5 3 2 6
Priority Scheduling
Problem is Starvation i.e. low priority processes may wait for indefinite time or never
execute.
(In a heavily loaded system, a steady stream of higher priority process can prevent a
low priority process from ever getting the CPU)
Each process gets a small unit of CPU time (time quantum), usually 10-100
milliseconds.
After this time elapses, the process is preempted and added to the end of the ready
queue.
CPU scheduler goes around the ready queue allocating the CPU to each process for a
time interval of up to 1 time quantum.
CPU scheduler picks the first process from the ready queue, sets a timer to interrupt
after 1 time quantum and dispatches the process.
After 1 time quantum, process either will finish the execution or preempted and
scheduler will schedule other process from head of the queue.
P1 14
P2 10
P3 8
Process Management
1)The Process concept
An operating system executes a variety of programs:
A process needs certain resources, including CPU time, memory, files, and I/O
devices, to accomplish its task.
1.1)The Process
A program is a passive entity i.e. a file containing a list of instructions stored on disk
called as executable file.
A process is an active entity with a program counter specifying the next instruction to
execute and a set of associated resources.
Entering the name of the executable file on the command line(ex prog.exe or
a.out)
A process includes:
Process in memory
Two processes may be associated with same program but they are not considered as
separate processes e.g. many copies of one mail program.
1.2)Process State
As the process executes ,it changes state.
The state of a process is defined in part by the current activity of that process.
new: The process is being created.
ready: The process is waiting to be assigned to a processor.
running: Instructions are being executed.
waiting: The process is waiting for some event to occur (I/O or reception of
signal)
terminated: The process has finished execution.
Only one process can be running on any processor at any instant.
Process state
Program counter
CPU registers
Memory-management information
Accounting information
3. Process state
4. Program counter
5. CPU registers
Along with the program counter, this state information must be saved
when an interrupt occurs, to allow the process to be continued correctly
afterward.
6. Memory-management information
8. Accounting information
Amount of CPU and real time used, time limits, account numbers , job or
process numbers,
2.1)Scheduling Queues:
Job queue
Ready queues
Device queues
Ready queue
Set of all processes residing in main memory, ready and waiting to execute.
Header of the queue contains the pointers to first and last PCB.
A new process initially put in ready queue, where it waits for CPU for execution.
The process issues an I/O request, and then be placed in an I/O device
queue.
The process creates a new subprocess and waits for its termination.
A process migrates between the various scheduling queues throughout its life
time.
The operating system must select the processes from or to the scheduling queues.
Selects process from ready queue which should be executed next and
allocates CPU.
Medium-term scheduler
Swap in the process from secondary storage into the ready queue.
System should have mix of both type of processes so I/O waiting queue and ready
queue will have equal and balanced work load.
Context Switch
Many users processes and system processes run simultaneously.
The system must save the state of the old process and load the saved state of the
new process.
Context-switch time is overhead and the system does no useful work while
switching.
User data
Program counter
System stack
PCB
Operations on Processes
In multiprogramming, processes may be created and deleted dynamically.
Process Creation
Each child process needs resources, resource sharing can be handled as:
Execution
Address space
UNIX examples
exec() system call used after a fork to replace the process’ memory space
with a new program.
Process Termination
Process executes last statement and ask OS to terminate itself through exit().
Child process may return output data to its parent via wait().
Process’ resources like virtual memory, open files, I/O buffers are
deallocated by operating system.
A process can cause termination of other process appropriate system call (e.g.
TerminateProcess() in Win 32)
Parent may terminate execution of children processes abort(). Reasons may be:
Parent is exiting:
Cooperating Processes
The concurrent processes executing in the OS may be either independent
processes or cooperating processes.
Cooperating processes (share data with other) can affect or be affected by the
execution of another process.
Computation speed-up
Convenience
An individual user may have many tasks on which to work at same time
(editing, printing, compiling).
Shared Memory
Message passing
Shared Memory
Message passing
Consumer may wait for new data but producer can always
produce new data.
Bounded-buffer solution
Bounded-buffer solution
Shared buffer is implemented as a circular array with two logical pointers in and
out.
Variable in points to next free position in the buffer and out points to first full
position in the buffer.
in == out
Buffer is full when
item nextConsumed
while (true) {