CS204-OS-module 4
CS204-OS-module 4
MODULE IV
CPU SCHEDULING
Basic concepts
Maximum CPU utilization obtained with multiprogramming
KTU STUDENTS
Preemptive Scheduling
Scheduling Criteria
Many criteria have been suggested for comparing CPU-scheduling algorithms.
The criteria include the following:
1. CPU utilization- Keep the CPU as busy as possible.
2. Throughput – Number of processes that complete their execution per
time unit
3. Turnaround Time-The interval from the time of submission of a process
to the time of completion. Turnaround Time is the sum of the periods
spent waiting to get into memory, waiting in the ready queue, executing
on the CPU, and doing I/0 waiting time.
4. Waiting Time-sum of the periods spent waiting in the ready queue.
SCHEDULING ALGORITHMS
0
KTU STUDENTS
P1
27
P3
30
0 3 6 30
Shortest-Job-First Scheduling
This algorithm associates with each process the length of the process's next CPU
burst. When the CPU is available, it is assigned to the process that has the
smallest next CPU burst.
More appropriate term for this scheduling method would be the shortest-next-
CPU-burst algorithm.
P4 P1 P3 P2
0 3 9 16 24
1. t n actual
th
length of n CPU burst
KTU STUDENTS
2. n 1 predicted
3. , 0 1
value for the next CPU burst
4. Define :
Commonly, α set to ½
Preemptive version called shortest-remaining-time-first
=0
n+1 = n
Recent history does not count
=1
n+1 = tn
Only the actual last CPU burst counts
If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + … +(1 - )j tn -j + … +(1 - )n +1 0
Since both and (1 - ) are less than or equal to 1, each successive term has less
weight than its predecessor
A preemptive SJF algorithm will preempt the currently executing process,
whereas a nonpreemptive SJF algorithm will allow the currently running process
to finish its CPU burst.
P1 0 8
P2 1 4
P3 2 9
P4 3 5
P1 P2 P4 P1 P3
0 1 5 10 17 26
Priority Scheduling
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
0 1 6 16 18 19
Round-Robin Scheduling
The round-robin (RR) scheduling algorithm is designed especially for
timesharing systems.
Each process gets a small unit of CPU time (time quantum q), usually 10-100
milliseconds. After this time has elapsed, the process is preempted and added to
the end of the ready queue.
If there are n processes in the ready queue and the time quantum is q, then each
process gets 1/n of the CPU time in chunks of at most q time units at once. No
process waits more than (n-1)q time units.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch, otherwise
overhead is too high
Example of RR with Time Quantum = 4
Process Burst Time
P1
P2
KTU STUDENTS
24
3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
In general, the average turnaround time can be improved if most processes finish
their next CPU burst in a single time quantum.
KTU STUDENTS
method used to determine which queue a process will enter when that
process needs service
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served FCFS
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to queue Q1
At Q1 job is again served FCFS and receives 16 additional milliseconds
If it still does not complete, it is preempted and moved to queue Q2
Deadlocks
KTU STUDENTS
A process requests resources; if the resources are not available at that time, the process
enters a waiting state. Sometimes, a waiting process is never again able to change state,
because the resources it has requested are held by other waiting processes. This
situation is called a deadlock.
System Model
Under the normal mode of operation, a process may utilize a resource in only the
following sequence:
1. Request: The process requests the resource. If the request cannot be granted
immediately (for example, 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 (for example, if the resource
is a printer, the process can print on the printer).
Deadlock Characterization
Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes
Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that P0
is waiting for a resource that is held by P1, P1 is waiting for a resource that is
held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting
for a resource that is held by P0.
Resource-Allocation Graph
Deadlocks can be described more precisely in terms of a directed graph called a system
resource-allocation graph. A set of vertices V and a set of edges E.
KTU STUDENTS
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes in the system
R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
Process
Pi requests instance of Rj
Pi is holding an instance of Rj
KTU STUDENTS
Since no resource instance is currently available, we add a request edge P3→ R2 to the
above graph .At this point, two minimal cycles exist in the system:
P1 → R1 → P2 → R3 → P3 → R2 → P1
P2 → R3 → P3 → R2 → P2
Processes P1, P2, and 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 process P2 to
release resource R2. In addition, process P1 is waiting for process P2 to release resource
R1.
Now consider the resource-allocation graph below. In this example, we also have a
cycle:
P1 → R1 → P3 → R2 → P1
However, there is no deadlock. Observe that process P4 may release its instance of
resource type R2. That resource can then be allocated to P3, breaking the cycle.
Basic Facts
KTU STUDENTS
if several instances per resource type, possibility of deadlock
Deadlock prevention
Deadlock avoidance
Ignore the problem and pretend that deadlocks never occur in the system; used
by most operating systems, including UNIX
Deadlock Prevention
Mutual Exclusion – not required for sharable resources (e.g., read-only files);
must hold for non-sharable resources
Hold and Wait – must guarantee that whenever a process requests a resource, it
does not hold any other resources
No Preemption –
Preempted resources are added to the list of resources for which the
process is waiting
Process will be restarted only when it can regain its old resources, as well
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
Simplest and most useful model requires that each process declare the maximum
number of resources of each type that it may need
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:
If Pi resource needs are not immediately available, then Pi can wait until
all Pj have finished
KTU STUDENTS
Avoidance Algorithms
Claim edge Pi Rj indicated that process Pj may request resource Rj; represented
by a dashed line
KTU STUDENTS
The request can be granted only if converting the request edge to an assignment
edge does not result in the formation of a cycle in the resource allocation graph
Banker’s Algorithm
Multiple instances
When a process gets all its resources it must return them in a finite amount of
time
KTU STUDENTS
Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj
Safety Algorithm
Work = Available
4. If Finish [i] == true for all i, then the system is in a safe state
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k
instances of resource type Rj
3 resource types:
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Need
ABC
P0 743
P1 122
P2 600
P3 KTU STUDENTS
011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety
criteria
P1 302 020
P2 302 600
P3 211 011
P4 002 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies
safety requirement
Deadlock Detection
Detection algorithm
Recovery scheme
l Pi Pj if Pi is waiting for Pj
Periodically invoke an algorithm that searches for a cycle in the graph. If there is
a cycle, there exists a deadlock
KTU STUDENTS
Resource-Allocation Graph and Wait-for Graph
Detection Algorithm
KTU STUDENTS
If no such i exists, go to step 4
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
State of system
KTU STUDENTS
Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes; requests
2. How long process has computed, and how much longer to completion?
Rollback – return to some safe state, restart process for that state
KTU STUDENTS
Assistant Professors,
Department of CSE,
PERINTHALMANNA