Os Chapter 3
Os Chapter 3
CPU scheduling
Chapter Objectives:
To introduce CPU scheduling, which is the basis
for multiprogrammed operating systems.
To discuss evaluation criteria for selecting a
CPU-scheduling algorithm for a particular
system.
To describe various CPU-scheduling algorithms.
Basic Concepts
in secondary memory.
When they are stored or loaded in the main memory they are called
process.
This is the decision of long term scheduler that how many processes will
multi-programming of system.
Cont…
It also known as a job scheduler
queue.
Medium Term Scheduler
Most often, a running process needs I/O operation which doesn’t requires CPU.
Hence during the execution of a process when an I/O operation is required then
the operating system sends that process from running queue to blocked queue.
When a process completes its I/O operation then it should again be shifted to
ready queue.
Or in other word, a running process may become suspended if it makes an I/O
request.
In this condition, to remove the process from memory and make space for other
processes, the suspended process is moved to the secondary storage.
All these decisions are taken by the medium-term scheduler. Medium-term
scheduling is a part of swapping. Or This process is called swapping
Short-Term scheduler
Short-Term scheduler(CPU scheduler) selects a process among the processes that are ready to
Its main objective is to maximize CPU utilization and to increase system performance in
Exit time
Exit time is the time when a process completes its execution and
exit from the system.
Response time
Response time is the time spent when the process is in the ready state
and gets the CPU for the first time.
Response time = Time at which the process gets the CPU for the first time - Arrival time
For example, here we are using the First Come First Served CPU scheduling
algorithm for the below 3 processes:
process Arrival time Burst time
P1 0 ms 8 ms
P2 1 ms 7ms
P3 2 ms 10ms
Cont…
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.
Waiting time
Waiting time 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.
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 becuase 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.
What is the difference between waiting time and
response time?
Response time is the time spent between the ready state and
getting the CPU for the first time.
But the waiting time is the total time taken by the process in the
ready state.
Let's take an example of a round-robin scheduling algorithm. The
time quantum is 2 ms.
Cont…
Time quantum= 2 ms
Gantt chart
P1 P2 P1 P2 p2
0 2 4 6 8
2 4 6 8 10
The response time of the process P2 is 2 ms because after 2 ms, the CPU is
allocated to P2 and the waiting time of the process P2 is 4 ms i.e turnaround time -
burst time (10 - 6 = 4 ms).
Turnaround time
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 , P2 is 7 seconds ,and P3 will be 17
seconds
Note: 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.
Throughput
CPU utilization
Keep the CPU as busy as possible
Optimization Criteria
Max CPU utilization
Max throughput
• Shortest-Job-First (SJF)
• Priority
• Round-Robin (RR)
• Multilevel Queue and
0 24 27 30
P1 P2 P3 P1
0 3 6 30
Advantage
Simple and easy algorithm to implement
It is suitable for batch system
Disadvantage
The average waiting time is not minimal
It is not suitable for time sharing system
2. Shortest-Job-First (SJF) Scheduling
Also known as shortest job next.
The basic principle of this algorithm is to allocate the CPU to the process with least CPU
burst time.
Associate each process with the length of its next CPU burst.
Use these lengths to schedule the process with the shortest time.
Best approach to minimize waiting time.
Easy to implement in Batch systems where required CPU time is known in advance.
Impossible to implement in interactive systems where required CPU time is not
known.
The processer should know in advance how much time process will take.
Con’t…
Two schemes:
Non-preemptive: Once CPU given to the process it can not be
preempted until completes its CPU burst.
Preemptive: if a new process arrives with CPU burst length less
than remaining time of current executing process, preempt.
This scheme is know as the Shortest-Remaining-Time-First
(SRTF).
SJF is optimal: gives minimum average waiting time for a
given set of processes.
Note: If the two processes have same CPU burst time the FCFS
is used to break the tie.
Example of Non-Preemptive SJF
0 7 8 12 16
(0 + 5 + 7 + 7) / 4 = 4.75
The SJF gives less average waiting time than FCFS.
0 2 4 5 7 11 16
P1 0 12
P2 2 4
P3 3 6
P4 8 5
Gantt chart?
Waiting time?
Average waiting time?
Solution
time and arrival time. Based on these find the average waiting
time using
A. Non- preemptive SJF?
B. Preemptive SJF?
Process Arrival Time Burst Time
P1 0 3
P2 2 6
P3 4 4
P4 6 5
P5 8 2
3. Priority Scheduling
A priority number (integer) is associated with each process,
and the CPU is allocated to the process with the highest priority.
Priority may be determined by users or by some default
mechanism.
The system may determine the priority based on memory
A process that is ready to run but waiting for the CPU can be considered blocked.
A priority scheduling algorithm can leave some low priority processes waiting
indefinitely.
can prevent a low- priority process from ever getting the CPU.
Solution to the problem
A solution to the problem of indefinite blockage of low-priority
processes is aging. Aging is a technique of gradually increasing
the priority of processes that wait in the system for a long time.
For example:
P1 P2 P3 P2 P1
0 1 2 4 8 17
AWT = (8-1)+(4-2)+0/3
= 7+2+0/3=9/3=3 ms
Exercise (Non-preemptive)
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Therefore:
processes.
Each process gets a small unit of CPU time (time quantum or time slice )
switch.
Con’t…
queues:
– Foreground (interactive)
– Background (batch)
Each queue has its own scheduling algorithm:
– Foreground – RR
– Background – FCFS
Con’t…
Scheduling must be done between the queues.
Three queues:
Q0 – Time quantum 8 milliseconds
Q1 – 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.
The Multilevel Feedback Queue Scheduling is preemptive.
Multilevel Feedback Queues
Thank you !!!