OS unit 2
OS unit 2
sponsoring
Priorities thecan work, beand other, often
defined eitherpolitical,
internally factors.
or externally. Internally defined priorities use some
Priority
measurable quantity or quantities to compute theorpriority
scheduling can be either preemptive nonpreemptive.
of a process. When a processtime
For example, arrives at the
limits, ready
memory
queue, its priority
requirements, is compared
the number of open with theand
files, priority
the ratioUNIT
of the 2
currentlyI/O
of average running
burst toprocess.
averageACPU preemptive
burst have priority
been
scheduling
used in computing priorities. External priorities are set by criteria outside the operating system, such the
algorithm will preempt the CPU if the priority of the newly arrived process is higher than as
priority
the importanceof the currently Scheduling
running process. ACriteria
nonpreemptive priority scheduling algorithm will simply
putDifferent
the newCPU-scheduling
process at the head algorithms
of the ready havequeue.
different properties, and the choice of a particular algorithm may
A
Aone
favour multilevel
major class queue
of processes
problem scheduling algorithm
over another.
with priority scheduling partitions
In choosing
algorithms whichtheindefinite
is ready
algorithmqueue to into
use inseveral
blocking, separate
ora starvation.
particular Aqueues.
situation,
process weThemust
that
considerprocesses are
the properties
is ready permanently
to run but of waiting assigned
the various to
for algorithms.one queue, generally based on some property
the CPU can be considered blocked. A priority scheduling algorithm can of the process, such as
memory
Many
leave somesize,
criteria process
low haveprioritypriority, or
beenprocesses process
suggested fortype.
waiting comparing
indefinitely. CPU-scheduling
In a heavily loaded algorithms. Whichsystem,
computer characteristics
a steadyare
used forEach queue
comparison
stream has its own scheduling
can makeprocesses
of higher-priority a substantial algorithm.
can difference For example, separate
in which algorithm
prevent a low-priority queues
process isfrom judged might
evertogettingbe used
be best.
theThefor
CPU. foreground
criteria include
and
A solution to the problem of in definite blockage of low-priority processes is aging. Agingwhile
the following:background processes. The foreground queue might be scheduled by an RR algorithm, the
involves
CPU background
gradually
utilization. queue
increasing We is want
scheduled
the priority
to keep by of
an processes
the FCFS
CPU as algorithm.
thataswait
busy in theConceptually,
possible. system for a CPU long utilization
time. For can example,
range fromif
0In addition,
priorities there
range from
to 100 percent. must be scheduling
127 system,
In a real among
(low) toit0 should
(high), range the queues,
we could which
fromincrease
40 percent is commonly
the priority implemented
of a waiting
(for a lightly as fixed-priority
process by
loaded system) to 901 every
percent
preemptive
15 minutes.
(for scheduling.
a heavily loaded system). For example, the foreground queue may have absolute priority over the
background
Example:queue.
Throughput. If the CPU is busy executing processes, then work is being done. One measure of work is the
number
Consider of the example
processes thatof are
a multilevel
completed queue
per timescheduling algorithm
unit, called with fiveFor
throughput. queues, listed below
long processes, in rate
this ordermay of be
priority:
Round-Robin
one process Scheduling
per hour; for short transactions, it may be ten processes per second.
1.
TurnaroundSystem
The round-robin processes
time.(RR) The scheduling
interval from algorithm
the timeis of designed
submissionespecially
of a for timesharing
process to the timesystems. It is similaristothe
of completion
2. Interactive
FCFS scheduling,
turnaround processes
but preemption
time. Turnaround time is added
the sum to enable the system
of the periods spent to waiting
switch between
to get into processes.
memory, waiting in the
3.
ready
A smallInteractive
queue,unitexecutingediting
of time, called processes
on thea CPU, and doing or
time quantum I/O. time slice, is defined. A time quantum is generally from10
4.
Waiting Batch processes
to 100 milliseconds in length.
time. The CPU-scheduling algorithm does not affect the amount of time during which a process
5.
executes Student
The ready or queueprocesses
does I/O. is treated
It affectsas aonlycircular
the amount
queue. To of time
implement
that a RR process
scheduling,
spends waiting
we againin treat
the ready
the ready
queue.
Waiting
Each
queue as queue
timea FIFOhas absolute
is the queue priority
sum ofoftheprocesses. over
periods spent lower-priority
Newwaitingprocesses queues.
in the areready No process
addedqueue. in the batch queue,
to the tail of the ready queue. The CPU for example,
could
Response run
schedulertime. unless
picksIn the queues
theanfirst
interactive for system
process system, processes,
from theturnaround
ready queue, interactive
timesetsmaya notprocesses,
timer be to and criterion.
theinterrupt
best interactive
after 1Thus,editing
time processes
another
quantum, measure
and
were all
isdispatches empty.
the time from the submission of a request until the first response is produced. This measure, called response
the process.
time,
If an
One is interactive
ofthetwotime it editing
things takes
will to process
start
then entered
The the
responding,
happen. notready
process the may queue
time while
it takes
have toaoutput
a CPU batch process
burst the
of response.
less was1The
than running,
time the batch
turnaround
quantum. timeIn is
process
generally would
this case,limited be
the process preempted.
by the itself
speed willof therelease
outputthe CPU voluntarily. The
device.
Another possibility is to time-slice among the queues. Here, each queue gets a certain portion of the CPU
scheduler will then proceed to the next process in the ready queue. If the CPU burst of the currently
time,
It iswhich
desirable it can then schedule among its various processes. Forand instance, in the foreground–background
running process to maximize
is longer thanCPU1 time utilization
quantum, andthethroughput
timer will go off to and
minimize turnaround
will cause time,towaiting
an interrupt the
queue
time, operating example,
and response the
time. foreground queue can be given 80 percent of the CPU time for RR scheduling
system. A context switch will be executed, and the process will be put at the tail of the ready
among its processes, while the
queue. The CPU scheduler will then select the next process in the ready queue.
background
The average queue waitingreceives
time under 20 percent
the RRof the CPU
policy to give
is often long.to its processes on an FCFS basis.
Scheduling
The performance algorithms of the RR algorithm depends heavily on the size of the time quantum. At one extreme, if
First-Come, First-Served
the time quantum is extremely Scheduling
large, the RR policy is the same as the FCFS policy. In contrast, if the time
First-Come,
quantum is extremely small (say, 1scheduling
First-Served (FCFS) millisecond), algorithm
the RRisapproach
the simplest canCPU-scheduling
result in a large algorithm.
number of context
With this scheme,
switches. It creates theaprocess
processor that sharing
requestsand the creates
CPU first an isappearance
allocated the thatCPU
each first.
of n processes has its own
The implementation
processor running atof1/n thethe
FCFSspeed policy
of theisrealeasily managed with a FIFO queue. When a process enters the ready
processor.
queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the
Example:
head of the queue. The running process is then removed from the queue.
Multilevel
There isQueue a convoy effect as all the other processes wait for the one big process to get off the CPU. This effect
Scheduling
results
Another in lower
class CPU and devicealgorithms
of scheduling utilizationhas thanbeenmightcreated
be possible if the shorter
for situations processes
in which were allowed
processes are easily to go
first.
classified into different groups.
FCFS scheduling algorithm is nonpreemptive. Once the CPU has been allocated to a process, that process
keeps the CPU until it releases the CPU, either by terminating or by requesting I/O.
The FCFS algorithm is thus particularly troublesome for time-sharing systems, where it is important that each
1
Multilevel Feedback Queue Scheduling
Normally, when the multilevel queue scheduling algorithm is used, processes are permanently assigned
to a queue when they enter the system. If there are separate queues for foreground and background
processes, for example, processes do not move from one queue to the other, since processes do not
change their foreground or background nature.
This setup has the advantage of low scheduling overhead, but it is inflexible.
The multilevel feedback queue scheduling algorithm, in contrast, allows a process to move between
queues.
The idea is to separate processes according to the characteristics of their CPU bursts. If a process uses
too much CPU time, it will be moved to a lower-priority queue. This scheme leaves I/O-bound and
interactive processes in the higher-priority queues. In addition, a process that waits too long in a lower-
priority queue may be moved to a higher-priority queue. This form of aging prevents starvation.
A process entering the ready queue is put in queue 0. A process in queue 0 is given a time quantum of
8 milliseconds. If it does not finish within this time, it is moved to the tail of queue 1. If queue 0 is
empty, the process at the head of queue 1 is given a quantum of 16 milliseconds. If it does not
complete, it is preempted and is put into queue 2. Processes in queue 2 are run on an FCFS basis but
are run only when queues 0 and 1 are empty.
Processor Affinity
Most SMP systems try to avoid migration of processes from one processor to another and instead attempt to
keep a process running on the same processor. This is known as processor affinity—that is, a process has an affinity
for the processor on which it is currently running.
Processor affinity takes several forms,
Soft Affinity: The operating system will attempt to keep a process on a single processor, but it is possible for
a process to migrate between processors.
Hard Affinity: It allows a process to specify a subset of processors on which it may run. The main-memory
architecture of a system can affect processor affinity issues. Consider, non-uniform memory access (NUMA).
The CPUs on a board can access the memory on that board faster than they can access memory on other
boards in the system.
Load Balancing
Load balancing attempts to keep the workload evenly distributed across all processors in an SMP
system. It is necessary only on systems where each processor has its own private queue of eligible processes to
execute.
There are two general approaches to load balancing:
1..Push Migration
With push migration, a specific task periodically checks the load on each processor and—if it finds an
imbalance—evenly distributes the load by moving (or pushing) processes from overloaded to idle or less-
busy processors.
3
2.Pull Migration
Pull migration occurs when an idle processor pulls a waiting task from a busy processor. Push and pull
migration need not be mutually exclusive and are in fact often implemented in parallel on load-balancing
systems.
DEADLOCKS
System model:
A system consists of a finite number of resources to be distributed among a number of competing
processes. The resources are partitioned into several types, each consisting of some number of
identical instances. Memory space, CPU cycles, files, I/O devices are examples of resource types. If a
system has 2 CPUs, then the resource type CPU has 2 instances.
A process must request a resource before using it and must release the resource after using it. A
process may request as many resources as it requires to carry out its task. The number of resources as
it requires to carry out its task. The number of resources requested may not exceed the total number of
resources available in the system. A process cannot request 3 printers if the system has only two.
A process may utilize a resource in the following sequence:
1.REQUEST: The process requests the resource. If the request cannot be granted immediately (if
the resource is being used by another process), then the requesting process must wait until it can
acquire the resource.
2.USE: The process can operate on the resource .if the resource is a printer, the process can print
on the printer.
3.RELEASE: The process release theresource.
For each use of a kernel managed by a process the operating system checks that the process has
requested and has been allocated the resource. A system table records whether each resource is free
(or) allocated. For each resource that is allocated, the table also records the process to which it is allocated.
If a process requests a resource that is currently allocated to another process, it can be added to a queue
of processes waiting for this resource.
To illustrate a deadlocked state, consider a system with 3 CDRW drives. Each of 3 processes old one of these
CDRW drives.
If each processes now request another drive, the 3 processes will be in a deadlocked state. Each is waiting for the event
“CDRW is released” which can be caused only by one of the other waiting processes. This example illustrates a deadlock
involving the same resource type.
Deadlocks may also involve different resource types. Consider a system with one printer and one
DVD drive. The process Pi is holding the DVD and process P j is holding the printer. If P i
requests the printer and Pj requests the DVD drive, a deadlock occurs.
DEADLOCK CHARACTERIZATION:
In a deadlock, processes never finish executing, and system resources are tied up, preventing other jobs
from starting.
4
NECESSARY CONDITIONS:
A deadlock situation can arise if the following 4 conditions hold simultaneously in a system:
1. MUTUAL EXCLUSION: Only one process at a time can use the resource. If another process requests that
resource, the requesting process must be delayed until theresource has beenreleased.
2. HOLD AND WAIT: A process must be holding at least one resource and waitingto acquire additional
resources that are currently being held by otherprocesses.
3. NO PREEMPTION: Resources cannot be preempted. A resource can be released only voluntarily by the
process holding it, after that process has completed itstask.
4. CIRCULAR WAIT: A set {P0,P1,…..Pn} of waiting processes must exist such that P0 is waiting for
resource held by P1, P1 is waiting for a resource held by P2,……,Pn-1 is waiting for a resource held by Pn
and Pn is waiting for a resource held byP0.
Dot within the rectangle.A request edge points to only the rectangle Rj.An assignment edge must also
designate one of the dots in therectangle.
When process Pi requests an instance of resource type Rj, a request edge is inserted in the resource
allocation graph. When this request can be fulfilled, the request edge is instantaneously
transformed to an assignment edge. When the process no longer needs access to the resource, it
releases the resource, as a result, the assignment edge is deleted. The sets P, R, E:
P= {P1, P2, P3}
R= {R1, R2, R3, R4}
E= {P1 ->R1, P2 ->R3, R1 ->P2, R2 ->P2, R2 ->P1, R3 ->P3}
5
One instance of resource type R1
Two instances of resource type R2 One instance of resource type R3 Three instances of resource type
R4 PROCESS STATES:
Process P1 is holding an instance of resource type R2 and is waiting for an instance of resourcetype
R1.
Process P2 is holding an instance of R1 and an instance of R2 and is waiting for instance of
R3.Process P3 is holding an instance of R3.
If the graph contains no cycles, then no process in the system is deadlocked. Ifthe graph does contain
a cycle, then a deadlock may exist.
Suppose that process P3 requests an instance of resource type R2. Since no resource instance is
currently available, a request edge P3 ->R2 is added to the graph.
2 cycles:
P1 ->R1 ->P2 ->R3 ->P3 ->R2 ->P1P2 ->R3 ->P3 ->R2 ->P2
Processes P1, P2, P3 are deadlocked. Process P2 is waiting for the resource R3, which is held by
process P3.process P3 is waiting for either process P1 (or) P2 to release resource R2. In
addition,process P1 is waiting for process P2 to release resource R1.
We also have a cycle: P1 ->R1 ->P3 ->R2 ->P1
However there is no deadlock. Process P4 may release its instance of resource type R 2. Thatresource
can then be allocated to P3, breaking the cycle.
DEADLOCK PREVENTION
For a deadlock to occur, each of the 4 necessary conditions must held. By ensuring that at leastone of
these conditions cannot hold, we can prevent the occurrence of a deadlock.
Mutual Exclusion – not required for s h a r e a b l e resources; must hold for non shareable
resources
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any
other resources
-Require process to request and be allocated all its resources before it begins execution, or allow
process to request resources only when the process has none.
-Low resource utilization,starvation problem;
No Preemption –
--If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released
--Preempted resources are added to the list of resources for whichthe process is waiting
--Process will be restarted only when it can regain its old resources, aswell as the new
ones that it is requesting
Circular Wait – impose a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration Deadlock Avoidance Requires that the
system has some additional a priori information available
--Simplest and most useful model requires that each process declare the
maximum number
of resources of each type that it may need
--The deadlock-avoidance algorithm dynamically examines the resource- allocation
state to ensure that there can never be a circular-wait condition
Resource-allocation state is defined by the number of available and allocated
resources, and the maximum demands of the processes .
Safe State
When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state
System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes
in the systems such that for each Pi, the resources that Pi can still request can be satisfied
by currently available resources + resources held by all the Pj, with j <I
That is:
o If Pi resource needs are not immediately available, then Pi can wait until all
Pj have finished
o When Pj is finished, Pi can obtain needed resources, execute,return allocated
resources, and terminate
o When Pi terminates, Pi +1 can obtain its needed resources,
and so on If a sys tem is in safe state no deadlocks
If a system is in uns afe state possibility of deadlock Avoidance ensure
that a system will never enter an unsafe stateAvoidance algorithms
Single instance of a resource type
o Use a resource-allocation graph Multiple instances of a resource type
o Use the banker’s algorithm
Resource-Allocation Graph Scheme
Claim edgePiÆRj indicated that process Pj may request resource Rj;represented by a
dashed line
Claim edge converts to request edge when a process requests a resource Request edge
converted to an assignment edge when the resource is allocatedto the process When a
resource is released by a process, assignment edge reconverts to a claim edge Resources
must be claimed a priori in the system.
Resource Preemption
Selecting a victim – minimize cost
Rollback – return to some safe state, restart process for that state Starvation –
same process may always be picked as victim, include numberof rollback in cost
factor
System Calls
fork()
Most operating systems (including UNIX, Linux, and Windows) identify processes
according to a unique process identifier (or pid), which is typically an integer
number.
A new process is created by the fork () system call. The new process consists of a
copy of the address space of the original process.
This mechanism allows the parent process to communicate easily with its child
process. Both processes (the parent and the child) continue execution at the
instruction after the fork (), with one difference: the return code for the fork () is
zero for the new (child) process, whereas the (nonzero) process identifier of the child
is returned to the parent.
exec()
After a fork () system call, one of the two processes typically uses the exec () system
call to replace the process’s memory space with a new program.
The exec () system call loads a binary file into memory and starts its execution. In this
manner, the two processes are able to communicate and then go their separate ways.
wait()
The parent can then create more children; or, if it has nothing else to do while the
child runs, it can issue a wait () system call to move itself off the ready queue until the
termination of the child. Because the call to exec () overlays the process’s address
space with a new program, the call to exec () does not return control unless an error
occurs.
grams to interact with the operating system. A computer program makes a system
callsS
sS when it requests the operating system’s kernel.
SYSTEM CALL INTERFACE FOR PROCESS MANAGEMENT
A system call is initiated by the program executing a specific instruction, which triggers a
switch to kernel mode, allowing the program to request a service from the OS. The OS then
handles the request, performs the necessary operations, and returns the result back to the
program.
System calls are essential for the proper functioning of an operating system, as they provide a
standardized way for programs to access system resources. Without system calls, each
program would need to implement its methods for accessing hardware and system services,
leading to inconsistent and error-prone behavior.
Fork(): The fork() system call is used by processes to create copies of themselves. It is one of
the methods used the most frequently in operating systems to create processes. When a parent
process creates a child process, the parent process’s execution is suspended until the child
process is finished. The parent process regains control once the child process has finished
running.
Exit(): A system call called exit() is used to terminate a program. In environments with
multiple threads, this call indicates that the thread execution is finished. After using the exit()
system function, the operating system recovers the resources used by the process.
Wait(): In some systems, a process might need to hold off until another process has finished
running before continuing. When a parent process creates a child process, the execution of the
parent process is halted until the child process is complete. The parent process is stopped
using the wait() system call. The parent process regains control once the child process has
finished running.
System call provides an interface between user program and operating system. The
structure of system call is as follows −
When the user wants to give an instruction to the OS then it will do it through system
calls. Or a user program can access the kernel which is a part of the OS through
system calls.
The duplicate process consists of all data in the file description and registers common.
The original process is also called the parent process and the duplicate is called the
child process.
The fork call returns a value, which is zero in the child and equal to the child’s PID
(Process Identifier) in the parent. The system calls like exit would request the services
for terminating a process.
Example
wait − Processes are supposed to wait for other processes to complete their work.
exec() − This function is used to replace the program executed by a process. The child
sometimes may use exec after a fork for replacing the process memory space with a
new program executable making the child execute a different program than the parent.
wait() − The parent uses a wait function to suspend execution till a child terminates.
Using wait the parent can obtain the exit status of a terminated child.