Professional Documents
Culture Documents
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
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
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:
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.
or
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.
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
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
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
P1 P2 P3
0 24 27 30
Operating System Concepts – 10th Edition 5.15 Silberschatz, Galvin and Gagne ©2018
FCFS Scheduling (Cont.)
P2 P3 P1
0 3 6 30
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
Operating System Concepts – 10th Edition 5.21 Silberschatz, Galvin and Gagne ©2018
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
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
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
Drawbacks
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
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
Operating System Concepts – 10th Edition 5.33 Silberschatz, Galvin and Gagne ©2018
Example of Priority Scheduling
Operating System Concepts – 10th Edition 5.34 Silberschatz, Galvin and Gagne ©2018
Exercise
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
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