You are on page 1of 37

Chapter 2: CPU Scheduling

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Basic Concepts
 In a system with a single CPU core, only one process can run at a time. Others
must wait until the CPU’s core is free and can be rescheduled.
 The objective of multiprogramming is to have some process running at all times,
to maximize CPU utilization. The idea is relatively simple.
 Process is executed until it must wait, typically for the completion of some I/O
request. In a simple computer system, the CPU then just sits idle.
 All this waiting time is wasted; no useful work is accomplished. With
multiprogramming, we try to use this time productively. Several processes are
kept in memory at one time. When one process has to wait, the operating
system takes the CPU away from that process and gives the CPU to another
process. This pattern continues. Every time one process has to wait, another
process can take over use of the CPU.
 Why Scheduling?
• Multiprogramming
 The part of the operating system that makes the choice is called the
scheduler and the algorithm it uses is called the scheduling algorithm.

Operating System Concepts – 10th Edition 5.2 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler
 Scheduling of this kind is a fundamental operating-system function.

Almost all computer resources are scheduled before use. The CPU is,
of course, one of the primary computer resources. Thus, its
scheduling is central to operating-system design.

 Whenever the CPU becomes idle, the operating system must select

one of the processes in the ready queue to be executed.

 The selection process is carried out by the CPU scheduler, which

selects a process from the processes in memory that are ready to


execute and allocates the CPU to that process.

Operating System Concepts – 10th Edition 5.3 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler
 The CPU scheduler selects from among the processes in ready queue, and
allocates a CPU core to one of them
• Queue may be ordered in various ways
 CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
 For situations 1 and 4, there is no choice in terms of scheduling. A new
process (if one exists in the ready queue) must be selected for execution.
 For situations 2 and 3, however, there is a choice.

Operating System Concepts – 10th Edition 5.4 Silberschatz, Galvin and Gagne ©2018
Preemptive and Nonpreemptive Scheduling

 When scheduling takes place only under circumstances 1 and 4, the


scheduling scheme is nonpreemptive.
 Otherwise, it is preemptive.
 Under Nonpreemptive scheduling, once the CPU has been allocated to a
process, the process keeps the CPU until it releases it either by terminating or
by switching to the waiting state.
 Virtually all modern operating systems including Windows, MacOS, Linux, and
UNIX use preemptive scheduling algorithms.
 Preemptive scheduling can result in race conditions when data are shared
among several processes.
 Consider the case of two processes that share data. While one process is
updating the data, it is blocked so that the second process can run. The
second process then tries to read the data, which are in an inconsistent state.

Operating System Concepts – 10th Edition 5.5 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
 CPU utilization – keep the CPU as busy as possible
 Throughput – # of processes that complete their execution per time unit
 Turnaround time – amount of time to execute a particular process
 Burst time- is the total time taken by the process for its execution on the CPU.
 Arrival time- is the time when a process enters into the ready state and is
ready for its execution

 Response time – amount of time it takes from when a request was submitted
until the first response is produced. (the time spent when the process is in the
ready state and gets the CPU for the first time.)

Operating System Concepts – 10th Edition 5.6 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
For example, here we are using the First Come First Serve CPU scheduling
algorithm for the below 3 processes:

Here, the response time of all the 3 processes are:


 P1: 0 ms
 P2: 7 ms because the process P2 have to wait for 8 ms during the execution of P1 and then after it will
get the CPU for the first time. Also, the arrival time of P2 is 1 ms. So, the response time will be 8-1 = 7
ms.
 P3: 13 ms because the process P3 have to wait for the execution of P1 and P2 i.e. after 8+7 = 15 ms, the
CPU will be allocated to the process P3 for the first time. Also, the arrival of P3 is 2 ms. So, the response
time for P3 will be 15-2 = 13 ms.
Response time = Time at which the process gets the CPU for the first time RT - Arrival time AT

Operating System Concepts – 10th Edition 5.7 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
 Waiting time – amount of time a process has been waiting in the ready
queue.
 It is the total time spent by the process in the ready state waiting for CPU.
For example, consider the arrival time of all the below 3 processes to be 0
ms, 0 ms, and 2 ms and we are using the First Come First Serve scheduling
algorithm.

Operating System Concepts – 10th Edition 5.8 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
 Then the waiting time for all the 3 processes will be:
• P1: 0 ms
• P2: 8 ms because P2 have to wait for the complete execution of P1 and arrival
time of P2 is 0 ms.
• P3: 13 ms because P3 will be executed after P1 and P2
i.e. after 8+7 = 15 ms and the arrival time of P3 is 2 ms. So, the waiting time of P3
will be: 15-2 = 13 ms.
 Waiting time = Turnaround time - Burst time

Operating System Concepts – 10th Edition 5.9 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
Turnaround time: is the total amount of time spent by the process from
coming in the ready state for the first time to its completion.

Turnaround time = Burst time + Waiting time

or

Turnaround time = Exit time - Arrival time

 For example, if we take the First Come First Serve scheduling algorithm,
and the order of arrival of processes is P1, P2, P3 and each process is
taking 2, 5, 10 seconds.
 Then the turnaround time of P1 is 2 seconds because when it comes at 0th
second, then the CPU is allocated to it and so the waiting time of P1 is 0
sec and the turnaround time will be the Burst time only i.e. 2 seconds.

Operating System Concepts – 10th Edition 5.10 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
 The turnaround time of P2 is 7 seconds because the process P2 have to
wait for 2 seconds for the execution of P1 and hence the waiting time of P2
will be 2 seconds.

 After 2 seconds, the CPU will be given to P2 and P2 will execute its task.
So, the turnaround time will be 2+5 = 7 seconds. Similarly, the turnaround
time for P3 will be 17 seconds because the waiting time of P3 is 2+5 = 7
seconds and the burst time of P3 is 10 seconds. So, turnaround time of P3
is 7+10 = 17 seconds.

 Different CPU scheduling algorithms produce different turnaround time for


the same set of processes. This is because the waiting time of processes
differ when we change the CPU scheduling algorithm.

Operating System Concepts – 10th Edition 5.11 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
 Throughput: is a way to find the efficiency of a CPU. It can be defined
as the number of processes executed by the CPU in a given amount of
time.
 For example, let's say, the process P1 takes 3 seconds for execution,
P2 takes 5 seconds, and P3 takes 10 seconds.
 So, throughput, in this case, the throughput will be (3+5+10)/3 = 18/3 = 6
seconds.
PROCESS BURST TIME (IN SEC.)

P1 24

P2 3

P3 4

Solution: Here is Gantt Chart

Figure – Gantt Chart Average waiting time is a crucial parameter to judge it's performance.
Lower the Average Waiting Time, better the scheduling algorithm
Avg. TAT = (24 + 27 + 31) / 3 = 27.33 sec
Avg. WT = (0 + 24 + 27) / 3 = 17.0 sec

Operating System Concepts – 10th Edition 5.12 Silberschatz, Galvin and Gagne ©2018
Scheduling Algorithm Criteria

The Purpose of a Scheduling algorithm


 Max CPU utilization
 Fair allocation of CPU
 Max throughput
 Min turnaround time
 Min waiting time
 Min response time

Operating System Concepts – 10th Edition 5.13 Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling

 The process that requested the CPU first is allocated the CPU and keeps it
until it released it, either due to completion or request of an I/O operation.
The process that has been in the ready queue the longest is selected for
running.

 The process with the minimal arrival time will get the CPU first. The lesser
the arrival time, the sooner will the process gets the CPU.

 Process execution begins with CPU burst, followed by an I/O burst, followed
by another CPU burst, then by another I/O burst and so on.

Operating System Concepts – 10th Edition 5.14 Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17

Operating System Concepts – 10th Edition 5.15 Silberschatz, Galvin and Gagne ©2018
FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order:


P2 , P 3 , P 1
 The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case

Operating System Concepts – 10th Edition 5.16 Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling

Operating System Concepts – 10th Edition 5.17 Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling
 Consider the processes P1, P2, P3, P4 given in the below table, arrives for
execution in the same order, with Arrival Time 0, and given Burst Time, let's
find the average waiting time using the FCFS scheduling algorithm.

Operating System Concepts – 10th Edition 5.18 Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling
 The average waiting time will be 18.75 ms
 For the above given processes, first P1 will be provided with the CPU
resources,
• Hence, waiting time for P1 will be 0
• P1 requires 21 ms for completion, hence waiting time for P2 will be 21 ms
• Similarly, waiting time for process P3 will be execution time of
P1 + execution time for P2, which will be (21 + 3) ms = 24 ms.
• For process P4 it will be the sum of execution times of P1, P2 and P3.
 The GANTT chart above perfectly represents the waiting time for each process.

Operating System Concepts – 10th Edition 5.19 Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling
Advantages
 It is the simplest of all non-preemptive scheduling algorithms: process
selection & maintenance of the queue is simple
 There is a minimum overhead and no starvation
 It is often combined with priority scheduling to provide efficiency
Drawbacks
 Poor CPU and I/O utilization: CPU will be idle when a process is blocked
for some I/O operation
 Poor and unpredictable performance: it depends on the arrival of processes
 Unfair CPU allocation: If a big process is executing, all other processes will
be forced to wait for a long time until the process releases the CPU. It
performs much better for long processes than short ones

Operating System Concepts – 10th Edition 5.20 Silberschatz, Galvin and Gagne ©2018
Shortest-Job-First (SJF) Scheduling

 Process with the shortest expected processing time (CPU burst) is


selected next.
 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. If the next CPU bursts of two processes are
the same, FCFS scheduling is used.
 Use these lengths to schedule the process with the shortest time
 SJF is optimal – gives minimum average waiting time for a given set
of processes
• The difficulty is knowing the length of the next CPU request
• Could ask the user

Operating System Concepts – 10th Edition 5.21 Silberschatz, Galvin and Gagne ©2018
Example of SJF

ProcessArriva l Time Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

Operating System Concepts – 10th Edition 5.22 Silberschatz, Galvin and Gagne ©2018
SJF Example

Operating System Concepts – 10th Edition 5.23 Silberschatz, Galvin and Gagne ©2018
SJF Example 2
Consider the below processes available in the ready queue for execution,
with arrival time as 0 for all and given burst times.

Operating System Concepts – 10th Edition 5.24 Silberschatz, Galvin and Gagne ©2018
SJF Example 3
As you can see in the GANTT chart above, the process P4 will be picked up
first as it has the shortest burst time, then P2, followed by P3 and at last P1.
We scheduled the same set of processes using the First come first
serve algorithm in the previous slide, and got average waiting time to be 18.75
ms, whereas with SJF, the average waiting time comes out 4.5 ms.

Operating System Concepts – 10th Edition 5.25 Silberschatz, Galvin and Gagne ©2018
Example of Shortest-remaining-time-first

 Now we add the concepts of varying arrival times and preemption to


the analysis
Process arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

Operating System Concepts – 10th Edition 5.26 Silberschatz, Galvin and Gagne ©2018
SJF
Advantages

 It gives superior turnaround time performance to SJFS, because a short


job is given immediate preference to a running longer process

Drawbacks

 There is a risk of starvation of longer processes


 High overhead due to frequent process switch
 Difficulty with SJFS
 Figuring out required processing time of each process

Operating System Concepts – 10th Edition 5.27 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR)
 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.
 A small amount of CPU time called a quantum or time slice is defined.
According to the quantum, a clock interrupt is generated at periodic intervals.
When the interrupt occurs, the currently running process is placed in the
ready queue, and the next ready process is selected on a FCFS basis.
 The CPU is allocated to each process for a time interval of upto one quantum.
When a process finishes its quantum, it is added to the ready queue, when it
is requesting I/O it is added to the waiting queue
 The ready queue is treated as a circular queue

Operating System Concepts – 10th Edition 5.28 Silberschatz, Galvin and Gagne ©2018
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
 The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

 Typically, higher average turnaround than SJF, but better response

Operating System Concepts – 10th Edition 5.29 Silberschatz, Galvin and Gagne ©2018
Example of RR
Consider the below example with time quantum=5

Operating System Concepts – 10th Edition 5.30 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR)
Features
 The oldest, simplest, fairest and most widely used preemptive scheduling
 Reduces the penalty that short processes suffer with FCFS
Drawbacks
 CPU-bound processes tend to receive unfair portion of CPU time, which results
in poor performance for I/O bound processes
 It makes implicit assumption that all processes are equally important. It does not
take external factors into account
 Maximum overhead due to frequent process switch
Difficulty with RRS
• The length of the quantum should be decided careful
• E.g.1 quantum =20ms, context switch =5ms, % of context switch = 5/25
*100=20%
 Poor CPU utilization
 Good interactivity

Operating System Concepts – 10th Edition 5.31 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR)
E.g.1 quantum =500ms, context switch =5ms, % of context switch = 5/505 *100<1%
 Improved CPU utilization
 Poor interactivity
Setting the quantum too short causes
 Poor CPU utilization
 Good interactivity
Setting the quantum too long causes
 Improved CPU utilization
 Poor interactivity
 A quantum around 100ms is often reasonable compromise

Operating System Concepts – 10th Edition 5.32 Silberschatz, Galvin and Gagne ©2018
Priority Scheduling
 A priority number (integer) is associated with each process
 Each process is assigned a priority and the runnable process with the
highest priority is allowed to run i.e. a ready process with highest
priority is given the CPU.

 The CPU is allocated to the process with the highest priority (smallest
integer  highest priority)
• Preemptive
• Nonpreemptive

 SJF is priority scheduling where priority is the inverse of predicted next


CPU burst time

 Problem  Starvation – low priority processes may never execute

 Solution  Aging – as time progresses increase the priority of the


process

Operating System Concepts – 10th Edition 5.33 Silberschatz, Galvin and Gagne ©2018
Example of Priority Scheduling

Process arri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

 Priority scheduling Gantt Chart

 Average waiting time = 8.2

Operating System Concepts – 10th Edition 5.34 Silberschatz, Galvin and Gagne ©2018
Exercise

o Consider the following processes arrive at time 0


Process P1 P2 P3 P4 P5
Priority 2 4 5 3 1
Service Time (Ts) 3 6 4 5 2
Turn around time (Tr) 18 10 4 15 20
Response time 15 4 0 10 18
Tr/Ts 6 1.67 1 3 10
Average response time = (0+15+4+10+18)/5 = 9.4
Average turn around time = (18+10+4+15+20)/5=13.4
Throughput = 5/20= 0.25

Draw the GANTT Chart for the Schedule?

Operating System Concepts – 10th Edition 5.35 Silberschatz, Galvin and Gagne ©2018
Priority Scheduling w/ Round-Robin
 It is often convenient to group processes into priority classes and use priority
scheduling among the classes.
Consider ProcessA arri Burst TimeT Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
 Run the process with the highest priority. Processes with the same
priority run round-robin

 Gantt Chart with time quantum = 2

Operating System Concepts – 10th Edition 5.36 Silberschatz, Galvin and Gagne ©2018
Priority Scheduling
Advantages
 It considers the fact that some processes are more important than others,
i.e. it takes external factors into account
Drawbacks
 A high priority process may run indefinitely and it can prevent all other
processes from running. This creates starvation on other processes. There
are two possible solutions for this problem:
 Assigning a maximum quantum to each process
 Assigning priorities dynamically, i.e. avoid using static priorities

Operating System Concepts – 10th Edition 5.37 Silberschatz, Galvin and Gagne ©2018

You might also like