Scheduling Algorithm
Scheduling Algorithm
Scheduling Algorithm
Scheduling
Learning Objectives
After reading this chapter, you will be able to:
• Understand the basic concepts of scheduling.
• Discuss the criteria for scheduling.
• Explainvarious scheduling algorithms.
• Discuss scheduling for multiprocessorsystems.
Explain real-time scheduling.
• Evaluate various scheduling algorithms. -ram
-PAL
4.1 INTRODUCTION
As discussed in Chapter 2, CPU scheduling is the procedure employed for deciding_which-of
the ready processes the CPU should be allocated to. CPU scheduling plays a pivotal role in the
basic framework of the operating system owing to the fact that the CPU is one of the primary
resources of the computer system. The algorithm used by the scheduler to c out the selection
of a rocess for execution is known aÄéKédiiliii@ålGGFithm. number of scheduling algorithms
are available for CP scheduling. Each scheduling algon influences the resource utilization,
overall system performance, and quality of service provided to the user. Therefore, one has to
reason out a number of criteria to be considered while selecting an •algorithmon a particular
system.
CPU burst
sum—o ;
seanf ("*d" Snum) ; I/O burst
while (i 10 ) CPV burst
sum num i;
When to Schedule
An important facet of scheduling is to determinewhen the scheduler should make scheduling
decisions. The following circumstances may require the scheduler to make sCheduling decisions:
en a process switches to waitin state. This situation may occur in case the
process has to wait for L/Oor the termma 1 n Itsc 1 process or some another
reason. In such
•tuations, the •cheduler has to select some ready process for exec.lltipn.
• a process switches from runmng to read state ue to occurrence of n interrupt.
In such
situations, the scheduler may ecr e to run a process from the ready queue. If the
Interrupt
was caused by some I/O device that has now comp eted its task, the
scheduler may choose the
process that was blocked and waiting for the I/O.
When a process switches fro waiting state to ready state, or example,
in cases where the
process has completed its I/O operation. In suc SituaIons, the scheduler
may select either
e process that has now come to the ready state, or the current
process, which may be
continued.
When a process terminates and the systgn. In t s case, the scheduler has to select a
process for execution from the set of ready processes.
(c) Dispatcher
The CPU scheduler only selects a process to be executed next on the
CPU but it cannot assign CPU
to the selected process. The function of setting up the execution of the
selected process on the CPU
is performed by another module of the operatingsystem, known as dispatcher.
The dispatcher
involves the following three steps to perform this function:
I. Context switching is performed.The kernel saves the context of currently
and restores the saved state of the process selected by the CPU scheduler.
running process
In case the process
selected by the short-term scheduler is new, the kernel loads its
context.
2. The system switches from the kernel mode to user mode, as a user process
is to be executed.
Scheduling
3. The execution of the user process
selected by the CPU scheduler is started by transferring the
control either to the instruction
that was supposed to be executed
interrupted or to the first at the time the præess was
instruction, if the process is
going to be executed for the first time after
its creation.
Preemptive scheduling algorithms: The CPU can be forcibl taken back from the currently
runnin The preemp_tgdprocess
rocessbefore its completion and allocate to some other py_ocgss.
is put back in the rea y queue and resumes its executionwhen it is scheduled a ain. Thus, •a
many times before its completion.In preemptive scheduling, the
decision to schedule another process is made whenever an interrupt occurs causing the currently
running process to switch to ready state, or when a process having higGVrFiGiffhfiühe currently
15 21 28 33
Initially, pt enters the ready queue at t = O
and CPU is allocated to it. While PI is executing, Pa, P,
and p 4 enter the ready queue at t = 2, t =
3, and t 5, respectively. When PI completes, CPU is
allocated to ? 2 as it has entered before P, and P4.
When P2 completes, P, gets the CPU after which
gets the CPU.
Since turnaround time = exit time— entry time therefore:
Turnaroundtime for
Turnaround time for P 2 = (21 —2) = 19 ms
Turnaround time for PJ = (28 —3) = 25 ms
Turnaround time for P 4 = (33 —5) = 28 ms
Average turnaround time = (15 + 19 + 25 + 28)/4 = 21.75 ms
Since waiting time = turnaround time —processing time, therefore:
Waitingtime for PI
Waiting time for P2 = (19—6) = 13 ms
Waiting time for P, = (25 —7) = 18 ms
Waiting time for Pa = (28 —5) = 23 ms
Average waiting time = (0+ 13 + 18 + 23)/4 = 13.5 ms
The performance of FCFS scheduling algorithm largely depends pp the order of arrival of processes
in the ready queue. having long
short CPU burst or vice versa. To illustrate this, assume that the processes (shown in Example 4.1)
4. enter the ready queue in the orde g, % , P 3 and Pt. ow, the processes will be scheduled as shown
in the following Gantt chart.
5 11 18 33
Using the above formulae, the average turnaround time and average waiting time can be computed as:
Average turnaround time = [(5 —0) + (Il —2) + (18 —3) + (33 —5)]/4 = 14.25 ms
Average waiting time = [(5 —5) + (9 —6) + (15 —7) + (28 —15)]/4 = 6 ms
It is clear that if processes having shorter CPU bursts execute before those having longer CPU
bursts, the average may-reduce significantly.
Advantages
• It is easier to understand and implement, as processes are simply to be added at the end of the
queue and No process in between these two points in the
queue needs to be accessed.
• Itiswell suited for batch systems where the longer!imeperiods foreach process areoftenacceptable.
60 Scheduling
Disadvantages
never
• The average waiting time is not minimal.Therefore,this schedulingalgorithm is
recomrÄéiidWWhere performance is a major issue.
For example, assume
e It reduces the CPU and I/O devices utilization under some circumstances.
ip the ready ueue.
that there is one Ion CPCbOtiiid rocessandmån¯AiOüf110- ound rocessgs
Now, it may appen thåt while the CPU-boundprocess is executing, the O-bound processes
they have to wait till the
complete their I/O and come to the ready queue for execution. There this time.
remain idle during
CPU-bound process releases the CPU; besides the I/O dev-ces also
deuce queue and the CPU
When the CPU-bound procesS needs To pe orm I/O, it comes to the
is allocated to I/O-boundprocesses.As the
the CPU idle. Then
they execute quickly and come back to the device queue, thereby leaving
CPU, which again results in
the CPU-bound process enters the ready queue and is allocated the
This happens again and again
having I/O processes waiting in ready queue, at some point of time.
I/O devices utilization.
until the CPU-bound process is done, thus resulting in low CPU and
amount of cpu
• It is not suitable for time sharing systems where each process needs the same
time.
7 9 12 17
Pi enters the ready queue at t = 0 and gets the CPU as there are no other processes in the
Initially,
While it is executing, P2, P3 and P4 enter the queue at t = l, t = 3 and t = 4 respectively
queue.
CPU becomes free, that is, at t = 7, it is allocatedto P3 because it is has the shortest CPU
When completes, CPU is allocatedfirst to P, and then to Pz.
three processes. When P3
burst among the
time = exit time —entry time,
therefore:
Since turnaround
PI = (7 —0) = 7 ms
Turnaround time for
for P2 = (17 —I) =
16 ms
Turnaround time (9 —3) = 6 ms
for P3
Turnaround time
for P4 = (12 —4)
= 8 ms
Turnaround time
Scheduling
61
Average turnaround time = (7 + 16 +
6 + 8)/4 = 9.25 ms
Sincewaiting time = turnaround time —
processing time, therefore:
Waitingtime for PI = (7 —7) = 0 ms
Waitingtime for P2 = (16 —5) Il ms
Waitingtime for Pg = (6 —2) = 4 ms
Waitingtime for P4 (8 —3) 5 ms
Average waiting time = (0 + Il +4
+ 5)/4 = 5 ms
Advantages
. It elimimtes the variance in waitin
and turnaround times. In fact, it is optimal with respect to
average waiting time if all processes
are available at the same time. This is due to the fact that
short processes are made to run before
longer ones, which decreases the waiting time for short
processes and_increases-the waiting time for
long processes. However, the reduction in waiting
time is more than the increment and thus, the
average waiting time decreases.
Disadvantages
It is di lcult lement as it needs to know the length of CPU burst of processes in advance.
In practice, it is difficult to obtain pnor owledge of required processing times of processes.
Many systems expect users to provide estimates of CPU burst of processes, which may not
always be correct.
It does not favour processes having longer CPU burst. This is because long processes will not
be allowed to get the CPU as long as the short processes continue to enter the ready queue. This
results in starvation of long processes.
Solution: The processes will be scheduled as depicted in the following Gantt chart.
3 5 11 17
Advantages
• Along process that is near completion maybe favoured overshortprocesses entering the system.
This results in an improvement in the tumaround time of the long process.
Disadvantages
Like SJF, it also requires an estimate of the next CPU burst of a process in advance.
• Favouring a long process that is nearing completion over the several short processes entering the
system may adversely affect the turnaround times of short processes.
• It favours only those long processes that are just about to complete and not those who have just
started their operation. Thus, starvation of long processes may still occur.
7 10 16
Initially,PI enters the ready queue at t = 0 and gets the CPU as there are no other processes
in the queue. While it is executing, P P and P4 enter the queue at t = 1, t = 3, and t = 4,
respectively.When the CPU becomes free, that is, at t = 7, it is allocated to P3 because it has the
highest priority (that is, 1) among the three processes. When P3 completes, CPU is allocated to
the next lower priority process, that is, P4 and finally, the lowest priority process P2 is executed.
Sincetumaround time = exit time —entry time, therefore:
Tumaround time for PI = (7 —0) = 7 ms
Tumaround time for P2 = (16 —l) = 15 ms
Turnaround time for P3 (10 —3) = 7 ms
Turnaroundtime for P4 = (12 —4) = 8 ms
s 10 16
o 1 3 6
Advantages
• Important processes are never made to wait merely because less important processes are under
execution.
Disadvantages
• It suffers from the problem of starvation ofloyerpriority processes, since the continuous arrival
of higher priority processes will indefinitelyprevent lower priority processes from acquiring the
CPU. One possible solution to this problem aging, which is a process Of gradually raising the
priority of a low priority process in step with increase in its waiting time. If the priority Of a IOW
priority process is increased after each fixed time interval, it is ensured that at some time it will
become the highest priority process and ill finally get executed.
Initially, when a process enters, its response ratio is 1. It goes on increasing at the rate of (I/CPU
burst) as the process's waiting time increases.
*Æxample 4.5: Consider four processes PI , P2, Pg, and P4 with their arrival timesmd required CPU
V'/ burst (in milliseconds) as shown in the following table:
Process
Arrival time 2 3 4
CPU burst (ms) 3 4 5 2
How will these processes be scheduled according to HRN scheduling algorithm? Compute the
average waiting time and average turnaround time.
Solution:The processes will be scheduled as depicted in the follgwing Gantt chart.
9 14
3 7
By the time PI completes, P2
Initially, Pi enters the ready queue at t = 0 and CPU is allocated to it.
3, the response ratio of P2 is ((3-2)+4)/4
and P3 have arrived at t = 2 and t = 3, respectively. At t =
scheduled next. During execution of P2,
= 1.25 and of P3 is I as it has just arrived. Therefore P2 is =
enters the queue at t = 4. When P2 completes at t = 7, the response ratio of P 3 is ((7-3)+5)/5
P4
response ratio, the CPU is allocated to it and after
I .8 and of P4 is ((7-4)+2)/2 = 2.5. As P4 has higher
its completion, P3 is executed.
time, therefore:
Since turnaround time = exit time —entry
Turnaround time for PI = (3 —0) = 3 ms
Turnaround time for P2 = (7 —2) = 5 ms
Turnaround time for 23 = (14 —3) = 11 ms
Turnaround time for P4 = (9 —4) = 5 ms
11 + 5)/4 = 6 ms
Average turnaround time = (3 + 5 +
—processing time, therefore:
Since waiting time = turnaround time
Waiting time for PI = (3 —3) = 0 ms
Waiting time for P2 = (5 —4) = 1 ms
Waitingtime for P3 (11 —5) = 6 ms
Waiting time for P4 = (5 —2) = 3 ms
+ 6 + 3)/4 = 2.5 ms
Average waiting time = (0+ 1
Advantages
waiting time, the response ratio of
processes. This is because with increase in
It favours short
speedily as compared to long processes. Thus, they are scheduled
short processes increases
earlier than long processes.
response ratio Of
with increase in waiting time, the
• Unlike SJF, starvation docs not occur since
they arc schcdulcd.
long processes also increases and eventually
Disadvantages
oftbc expected service time (CPU burst) ofa
• Like SJF and SRTN. it also requires an estimatc
Arrival time 3 4
CPU burst (ms) 10 5 2 3
Assuming that the time slice is 3 ms how will these processes be scheduled according
to round
robin scheduling algorithm? Compute the average waiting time and average turnaround
time.
Solution: Tije processes will be scheduled as depicted in the following
Gantt chart.
3 6 14 16 19
Initially, P; enters the ready queue at t z and gets the CPU
for 3 ins. While it executes, P2 an
p y enter the queue at t •v J and t 3, respectively.Since,
Pi does not execute within 3 ms' a
interrupt occurs when the time slice gets over. P. is
preempted (with remaining CPU burst
7 ms), put back in the queue after P, becauseP, has not
to Pa. During execution of entered yet and the CPU is allocate
P, enters in the queue at
t 4 and is put at to the
behind P; , When P, times out, it is preempted (with end Of queu
remaining CPU burst of
end of queue, behind p.. The CPU is allocated to the 2 ms) and sent to
next process in
the queue, that is, to P 3 and
Scheduling 67
executes completely before the time slice expires.
Thus, the CPU is allocated to the next process
in the queue which is Pr. PI again executes for
3 ms, is then preempted (with remaining CPU burst
of 4 ms) and joins the end of the queue, behind Pa . The CPU is now allocated to P4. P, cxccutcs
completely within its time slice, so the CPU is allocated to the process at the head of thc queue,
that is, P2. As P2 completes before the time out occurs, the CPU is switched to Pi at t 16 for
another 3 ms. When the time slice expires, the CPU is reallocated to Pi since it is the only proccys
remaining: there is no queue left.
Since tumaround time = exit time —entry time, therefore:
Turnaround time for PI = (20 —0) = 20 ms
Turnaround time for P2 = (16 —l) = 15 ms
Turnaround time for P3 = (8 —3) = 5 ms
Turnaround time for P4 = (14 —4) = 10 ms
Average turnaround time = (20 + 15 + 5 + 10)/4 = 12.5 ms
Since waiting time = turnaround time —processing time, therefore:
Waiting time for PI = (20 —10) = 10 ms
Waiting time for P2 = (15 —5) = 10 ms
Waiting time for P3 = (5 —2) = 3 ms
Waiting time for (10 —3) = 7 ms
Average waiting time = (10 + 10 + 3 + 7)/4 = 7.5 ms
The performance of round robin scheduling is greatly affected by the size of the time quantum.
If the time quantum is too small, a number of context switches occur which, in turn, increase the
system overhead. More time will be spent in performing context switching rather than executing
the processes. On the other hand, if the time quantum is too large, the performance of round robin
simply degrades to FCFS.
Note: If the time quantum is too small, say I ms, the round robin scheduling is called processor
sharing.
Advantages
• It is efficient for time sharing systems where the CPU time is divided among the competing
processes.
• It increases impartiality as regards priority to processes is concerned.
Disadvantages
system
• Processes(even short ones) may take a long time to execute.This decreasesthe
throughput.
after each time out.
• It needs some extra hardware support, such as a timer to cause interrupt
of the processes can complete their execution
Note: Ideally, the time quantum should be such that 80%
within the given time quantum.
System processes
Highest priority
Interactive processes
CPU
Batch processes
Lowest priority
Advantages
• It is fair to I/O-bound(short)processesas these processesneed not wait too long and are
executed quickly.
• It prevents starvation by moving a lower priority process to a higher priority queue if it has been
waiting for too long.
Disadvantages
• It is the most complex scheduling algorithm.
• Moving the processes between queues causes a number of context switches, which results in an
increased overhead.
The turnaround time for long processes may increase significantly.
read ueue for all the processors (sec Figure 4.4(b)), In thc former case, it may happen that at
any given moment, the ready queue of one processor may bc cmpVWhilc thc othcr processor
is very busy in executing processes. To obviate this sort of situation, the is
-preferred,in which all the_nroccsscs entet into onc qucuc and arc schcduJc?lon any availabJg
processor.
CPU,
CPU?
cptJ3
CPUn
CPU,
CPU2
CPI-J3
CPUn