Unit 3.4 Process Scheduling
Unit 3.4 Process Scheduling
Which process is given control of the CPU and for how long?
Process Scheduling
• It is the activity of managing the multiple processes.
• It is done by the process manager.
• It is the task of selection of next process to be executed when the
running process is removed/completed.
• It is an essential part of multiprogramming OS. It allows more than one
process to be loaded into the memory at a time and OS has to schedule
the processes for execution.
When to Schedule
1. When a new process is created.
2. When a process terminates.
3. When a process blocks on I/O, on semaphore, waiting for child
termination etc.
4. When an I/O interrupt occurs.
5. When quantum expires.
Goals of Scheduling
• Fairness: Each process gets fair share of the CPU.
• Efficiency: When CPU is 100% busy then efficiency is increased.
• Response Time: Minimize the response time for interactive user.
• Throughput: Minimizes jobs per given time period.
• Waiting Time: Minimizes total time spent waiting in the ready queue.
• Turnaround Time: Minimizes the time between submission and
termination.
Scheduler
• It is a program within OS that handles process scheduling.
• It’s main task is to select the jobs to be submitted into the system and to
decide which process to run.
• Types:
• Long Term Scheduler: also called job scheduler. It determines which programs are
admitted to the system for processing. It selects processes from the queue and
loads them into memory for execution.
• Short Term Scheduler: also called CPU scheduler. It selects a process among the
processes that are ready to execute and allocates CPU to one of them.
• Medium Term Scheduler: It determines the process to be removed from the
memory. It is in-charge of handling the swapped-out processes.
Dispatcher
• It is a special program within OS that is executed after scheduler.
• When a scheduler completes its job of selecting a process, then the
dispatcher takes the process to the desired state.
• The functions of dispatcher includes:
• Context Switching
• Switching to User Mode
• Jumping to proper location in user program to restart the program.
Difference between Dispatcher and Scheduler
Dispatcher Scheduler
• It is a module in OS that gives • It is a module in OS which selects
control of CPU to the process a process among various
selected by short term scheduler. processes.
• It works independently. It works
• Working of dispatcher is immediately when required.
dependent on scheduler.
• It works on various algorithm
• It has fixed working process with such as FCFS, SJF, SRTN, RR etc.
no multiple algorithms for its
implementation.
Context Switching
• It is the process of switching the processing of one process to another
process. It is done by the dispatcher.
• In multitasking OS, context switching enables multiple processes to share a
single CPU.
• When the dispatcher switches the CPU from executing one process to
another, the state from the current running process is stored into its PCB.
After this, the state of the next process to be executed is loaded from its own
PCB and execution begins. This switching is termed as Context Switching.
• It is an expensive task as the CPU time is wasted during switching.
• During context switching, the following information is stored for later use:
Program Counter, Scheduling Information, Currently used register, Changed
State, I/O State Information, Accounting Information.
Types of Scheduling
• Preemptive Scheduling
• Non-Preemptive Scheduling
Preemptive Scheduling
• The resources are allocated to a process for a limited time.
• Process can be interrupted in between.
• If a high priority process frequently arrives in the ready queue, low
priority process may starve.
• Preemptive scheduling has the overheads of scheduling the
processes.
• Preemptive scheduling is flexible.
Non-Preemptive Scheduling
• Once resource are allocated to a process, the process holds it till it
completes its burst time or switches to waiting state.
• Process cannot be interrupted till it terminates or switches to waiting
state.
• If a process with long burst time is running CPU, then another process
with less CPU burst time may starve.
• It is not flexible as preemptive.
Scheduling Criteria
• Different CPU Scheduling algorithms have different properties.
• Multiple criteria have been suggested for comparing CPU scheduling
algorithms. Some of them are:
• CPU utilization: keep the CPU as busy as possible.
• Throughput: number of processes that complete their execution per time
unit.
• Turnaround time: amount of time to execute a particular process.
• Waiting time: amount of time a process has been waiting in the ready queue.
• Response time: amount of time it takes from when a request was submitted
until the first response, not output.
Scheduling Algorithm
• FCFS (First Come First Served)
• SJF (Shortest Job First) Batch Processing System
• SRTN (Shortest Remaining Time Next)
• RR (Round Robin)
• Priority Scheduling
• Multilevel Queue Scheduling Interactive (Online) System
• Multilevel Feedback Queue Scheduling
Batch System Scheduling
• Batch processing is a technique in which an OS collects the programs
and data together in a batch before processing starts.
• The major process scheduling algorithms based on batch system
scheduling are:
• First Come First Serve (FCFS)
• Shortest Job First (SJF)
• Shortest Remaining Time First (SRTF)
Interactive System Scheduling
The process scheduling algorithms based on Interactive system
scheduling are:
• Round Robin Scheduling
• Priority Scheduling
• Multiple queues (Multilevel Queue Scheduling)
• Multilevel Feedback Queue Scheduling
Real Time System Scheduling
The major process scheduling algorithms based on Real Time
scheduling are:
• Priority Fair Share Scheduling
• Guaranteed Scheduling
• Lottery Scheduling Real Time System
• Highest Response Ratio Next (HRN)
First Come First Serve (FCFS)
• In FCFS, the processes are scheduled in the order they are received.
• FCFS provides an efficient, simple and error free process scheduling
algorithm that saves valuable CPU resources.
• It uses non-preemptive scheduling. Once the process has the CPU, it runs
to the completion.
• It can be easily implemented by managing a simple queue or by sorting
according to the time the process was received.
• Problems:
• It doesn’t guarantee a good response time.
• It has larger average waiting time.
FCFS (Contd..)
• Example 1: Consider the processes P1, P2, P3 and P4 given in the table
below, arrives for execution in the order, with zero arrival time and given
burst time. Find the average waiting time and average turnaround time
using FCFS scheduling algorithm.
Process Burst Time
P1 21
P2 3
P3 6
P4 2
0 21 24 30 32
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P1 21-0=21 21-21=0
P2 24-0=24 24-3=21
P3 30-0=30 30-6=24
P4 32-0=32 32-2=30
P1 P2 P3 P4
0 21 24 30 32
FCFS (Contd..)
Gantt Chart:
P1 P2 P3 P4
0 21 24 30 32
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P1 21-0=21 21-21=0
P2 24-2=22 22-3=19
P3 30-2=28 28-6=22
P4 32-3=29 29-2=27
P4 P2 P3 P1
0 2 5 11 32
SJF (Contd..)
Gantt Chart:
P4 P2 P3 P1
0 2 5 11 32
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P4 2-0=2 2-2=0
P2 5-0=5 5-3=2
P3 11-0=11 11-6=5
P1 32-0=32 32-21-11
P1 P4 P2 P3
0 21 23 26 32
SJF (Contd..)
Gantt Chart:
P1 P4 P2 P3
0 21 23 26 32
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P1 21-0=21 21-21=0
P4 23-3=20 20-2=18
P2 26-2=24 24-3=21
P3 32-2=30 30-6=24
P1 P2 P2 P4 P3 P1
3 5 7 13 32
0 2
Shortest Remaining Time First (contd..)
Gantt Chart:
P1 P2 P2 P4 P3 P1
0 2 3 5 7 13 32
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P1 32-0=32 32-21=11
P2 5-2=3 3-3=0
P4 7-3=4 4-2=2
P3 13-2=11 11-6=5
P1 P1 P4 P1 P3 P2
1 2 3 6 10 17 25
Shortest Remaining Time First (contd..)
Gantt Chart:
P1 P1 P4 P1 P3 P2
1 2 3 6 10 17 25
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P1 10-1=9 9-6=3
P2 25-1=24 24-8=16
P3 17-2=15 15-7=8
P4 6-3=3 3-3=0
• If the process completes its execution within the defined quantum, then it is removed
from the queue otherwise it has to wait for another time slice and is placed at the
back of the ready queue.
• If the quantum is very large, each process is given as much time as required for
completion. It will be similar to FCFS.
• If quantum is small, system will be busy at just switching from one process to another
process, the overhead of context switching causes the system efficiency degrading.
Round Robin (RR) Scheduling
Advantages:
• Fair allocation of CPU across the process.
• Used in timesharing system.
• Low average waiting time when process lengths (CPU burst time) vary widely.
Disadvantages:
• Poor average waiting time when process lengths are identical.
• We have to perform a lot of context switching here, which will keep the CPU
idle.
• Consider there are four process P1,P2, P3 and P4 given in below table
with arrival time 0 and given burst time. Compute Average waiting
and Turnaround time using RR algorithm.(with time quantum=5)
Process Burst Time
P1 21
P2 3
P3 6
P4 2
• Gantt chart:
Process Arrival time Burst time Completion time Turnaround time Waiting time
P1 0 21 32 32-0=32 32-21=11
P2 0 3 8 8-0=8 8-3=5
P3 0 6 21 21-0=21 21-6=15
P4 0 2 15 15-0=15 15-2=13
• It is a non-preemptive scheduling.
Priority Scheduling (contd..)
Advantages
• Higher priority processes like system processes are executed first.
Disadvantages
• It can lead to starvation if only higher priority process comes into the
ready state. Low priority processes may never execute.
• If the priorities of two processes are the same, then we have to use some
other scheduling algorithm (usually FCFS).
Priority Scheduling (contd..)
• Example 1: Consider the processes P1, P2, P3 and P4 given in the
table below, arrives for execution in the same order, with arrival time
0, and given burst time. Find the average waiting time and average
turnaround time using the Priority Scheduling algorithm.
Process Burst Time Priority
P1 21 2
P2 3 1
P3 6 4
P4 2 3
Priority Scheduling (contd..)
Gantt Chart:
P2 P1 P4 P3
0 3 24 26 32
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P2 3-0=3 3-3=0
P1 24-0=24 24-21=3
P4 26-0=26 26-2=24
P3 32-0=32 32-6=26
P1 P3 P2 P4
0 5 13 16 22
Priority Scheduling (contd..)
P1 P3 P2 P4
0 5 13 16 22
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P1 5-0=5 5-5=0
P3 13-2=11 11-8=3
P2 16-1=15 15-3=12
P4 22-3=19 19-6=13
Gantt Chart
Process Arrival Time Burst Time Completion Time Turnaround Time Waiting Time
P1 0 10 17 17-0=17 17-10=7
P2 1 5 8 8-1=7 7-5=2
P3 2 2 4 4-2=2 2-2=0
Here, there are two queues: Queue 1 and Queue 2. Queue 1 is having
higher priority and is using the FCFS. Queue 2 is using RR (Quantum=2).
Use multiple queue and calculate average waiting time and turn around
time.
Multiple Queues (Multilevel Queue Scheduling)
P1 P4 P2 P3 P2 P3 P3 P3
0 5 11 13 15 16 18 20 22
Calculation:
Process Turnaround time Waiting time
(Completion time-arrival time) (Turnaround time-burst time)
P1 5-0=5 5-5=0
P4 11-0=11 11-6=5
P2 16-0=16 16-3=13
P3 22-0=22 22-8=14
P2 17 0 34 34-0=34 34-17=17
Disadvantages
• It can’t be implemented practically because the burst time of all the
processes cannot be known in advance.
Priority Fair Share Scheduling
• It is used in OS in which CPU usage is equally distributed among
system users or groups, as opposed to equal distribution among
processes.
• If four users (A, B, C, D) are concurrently executing one process each,
the scheduler will logically divide the available CPU cycles such that
each users gets 25% of the whole (100%/4=25%).
• One common method of logically implementing the fair-share
scheduling strategy is to recursively apply the round robin scheduling
strategy at each level of abstraction (process, user, group).
Guaranteed Scheduling
• It is a scheduling algorithm used in multitasking OS that guarantees fairness
by monitoring the amount of CPU time spent by each user and allocating
resources accordingly.
• It makes real promises to the users about the performance.
• If there are n users logged in, each user will receive 1/n of the CPU power.
Similarly, on a single user system with n processes running, all things being
equal, each one should get 1/n of the CPU cycles.
• To make good on this promise, the system must keep track of how much CPU
each process has had since its creation. It compute the ratio of actual CPU
time consumed to the CPU time entitled.
• The ratio of 0.5 means that the process only had 50% of what is should have
had.
Lottery Scheduling
• Processes are scheduled in a random manner.
• It can be preemptive or non-preemptive.
• In this scheduling, each process is given at least one lottery ticket and
scheduler picks a random ticket and process having that ticket is
executed.
• The process having higher number of tickets will have higher chance
of execution.