Chapter-2-3-Process Scheduling
Chapter-2-3-Process Scheduling
Job queue :
► Set of all processes in the system
► The short term scheduler picks the job from the ready
queue and dispatch to the CPU for the execution.
Scheduling Queues (Cont’d)
Device queues:
► Set of processes waiting for an I/O device.
► Long-Term Scheduler
► Short-Term Scheduler
► Medium-Term Scheduler
Scheduler (Cont’d)
Long-Term Scheduler
►Also known as the Job Scheduler.
to execute.
Scheduler (Cont’d)
Short-Term Scheduler
►Also known as the CPU Scheduler.
►Selects the next process from the Ready Queue to be exe-
Scheduler
Types of CPU Scheduling
CPU scheduling decisions may take place under the follow-
ing four circumstances:
1. When a process switches from the running to
the waiting state.
2. When a process switches from the running to
the ready state.
3. When a process switches from the waiting state to
the ready state.
4. When a process terminates.
Types of CPU Scheduling (Cont’d)
► In circumstances 1 and 4, there is no choice in terms
of scheduling. A new process must be selected for ex-
ecution.
► However in circumstances 2 and 3, there is a choice.
Disadvantages
► If a high priority process frequently arrives in the
ready queue, low priority process may starve.
► Overheads of scheduling the processes.
Types of CPU Scheduling (Cont’d)
Non-preemptive scheduling
► Applied when a process terminates, or a process switches
Advantage
► Does not have overheads.
Disadvantages
► Rigid.
► If a process with long burst time is running CPU, then an-
other process with less CPU burst time may starve.
CPU Scheduling: Criteria
►There are different criteria to check when considering
the “best” scheduling algorithm, they are:
CPU Utilization
► keep the CPU as busy as possible.
►Ideally 100% of the time.
►40% lightly loaded
►90% heavily loaded.
Throughput
► It is the total number of processes completed per unit of
time.
CPU Scheduling: Criteria (Cont’d)
Turnaround Time
►It is the amount of time taken to execute a particular
process.
►The interval from the time of submission of the process to
Load Average
►It is the average number of processes residing in the ready
Completion Time
►The completion time is the time when the process stops exe-
cuting, which means that the process has completed its burst
time and is completely executed.
Scheduling Algorithms
► In order to maximize CPU utilization, computer scientists
have developed a set of algorithms that determine which
process should run first and which should run last.
► The algorithms used are as follows:
► First Come First Serve
► Shortest Job First(SJF)
► Priority
► Round Robin
► Multilevel Queue
► Etc.
Scheduling Algorithms(Cont’d)
First Come First Serve (FCFS)
► The process which arrives first, gets executed first.
►Or the process which requests the CPU first, gets
Process P1 P2 P3 P4
Average waiting time (AWT) 0 21 24 30
Scheduling Algorithms(Cont’d)
Example 2: Consider the process arrives in the order P4, P2, P3,
P1 that have been given in Example1; Calculate the average
waiting time and compare the result
Process Burst time
P4 2
P2 3
P3 6
p1 21
Solution: If the process arrive in the order P4, P2, P3, and P1; we
get the result show in the following Gannt chart.
Scheduling Algorithms(Cont’d)
Process P4 P2 P3 P1
Average waiting time (AWT) 0 2 5 11
Scheduling Algorithms(Cont’d)
Example 3: Consider 5 processes whose arrival and burst time are
given in the table below. Calculate average waiting and average
turned around time, if FCFS scheduling algorithm is followed.
PID Arrival Burst time -
m
time Co e
= m
1 4 5 e l ti nd
m
ti iva u
d rr ro
n a a
2 6 4
r ou e - n ed e
a im ur m
3 0 3 d
e nt T ti
rn i o = rst
Tu pilat e u
4 6 2 it m - B
n g e
i i
t tim
5 5 4 a
W
Scheduling Algorithms(Cont’d)
Solution:
GANTT
Chart
►Two types:
► Non Pre-emptive
► Pre-emptive
ready queue for execution, with arrival time as 0 for all and
given burst times.
PID Burst time
1 21
2 3
3 6
4 2
Scheduling Algorithms(Cont’d)
► 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.
► By comparison, If we were using FCFS algorithm in
► If the arrival time for processes are different, all the pro-
cesses are not available in the ready queue at time 0.
Scheduling Algorithms(Cont’d)
Pre-emptive Shortest Job First
►Also known as Shortest remaining Time(SRT) or Shortest
Next Time(SNT).
►Jobs are put into ready queue as they arrive, but as a process
OR
Scheduling.
► There is no way to know the length of the next CPU burst.
Scheduling Algorithms(Cont’d)
Priority CPU Scheduling
►It is one of the most common scheduling algorithms in
batch systems.
►Each process is assigned a priority.
liseconds in length.
Some important characteristics of the RR Algorithm:
► It resides under the category of Preemptive Algorithms.
operating system.
Example 7: There are six processes, their arrival and burst
time are given below in the table. The time quantum of the
system is 4 units. The calculate the average waiting times of
the give processes.
Scheduling Algorithms(Cont’d)
Remaining
PID Arrival Burst Burst Time
time time
1 0 5 P2 P3 P4 P5 P1 P6 P1: 1
1 2 3 4 6
2 1 6 P2:2
3 2 3
P2 P5
Ready Queue P5:1
4 3 1
5 4 5
Gantt chart
6 6 4
P1 P2 P3 P4 P5 P1 P6 P2 P5
1
0 4 8 11 12 6 17 21 23 24
Scheduling Algorithms(Cont’d)
Turn Around Time = Completion Time - Arrival Time
To recap
Waiting Time = Turn Around Time - Burst Time
PID Arrival time Burst time Completion time Turn Around Time Waiting Time
1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
6 6 4 21 15 11
Scheduling Algorithms(Cont’d)
Advantages of RR Scheduling Algorithm
►In terms of average response time, this algorithm gives the
best performance.
► All the processes get a fair allocation of CPU.
queue.
Scheduling Algorithms(Cont’d)
►Low throughput.
Deadlock
► Deadlock is a situation where a set of processes are
blocked because each process is holding a resource and
waiting for another resource acquired by some other
process.
Requests a Resource
► Firstly, the process requests the resource. If the request can-
not be granted immediately(e.g.: resource is being used by
any other process). Then the requesting process must wait
until it can acquire the resource.
Deadlock (Cont’d)
Use Resource
►The Process can operate on the resource ( e.g. if the resource is
a printer then in that case process can print on the printer).
Release Resource
►The Process releases the resource.
Deadlock Characterization
► A deadlock situation can arise if the following four con-
ditions hold simultaneously in a system:
I. Mutual exclusion:
► Only one process at a time can use the resource.
► If another process requests that resource, the requesting
process must be wait until the resource has been released.
II. Hold and wait :
► There must exist a process that is holding at least one re-
source and is waiting to acquire additional resources that are
currently being held by other processes.
Deadlock Characterization (Cont’d)
III.No pre-emption:
► Resources cannot be pre-empted;
► Resource can be released only voluntarily by the process
holding it, after that process, has completed its task.
IV. Circular wait:
in the system.
► R = {R1, R2, …, Rm}, the set consisting of all resource
► no deadlock
II. Allow the system to enter a deadlock state and then re-
cover
III. Ignore the problem and pretend that deadlocks never occur
in the system; used by most operating systems, including
UNIX
Methods for handling Deadlocks (Cont’d)
Deadlock Prevention
► Prevent a deadlock before it can occur.
► The system checks each transaction before it is executed to
III. No Preemption:
► If a process that is holding some resources requests an-
other resource that cannot be immediately allocated to it,
then all resources currently being held are released
► Preempted resources are added to the list of resources for
which the process is waiting
Methods for handling Deadlocks (Cont’d)
Deadlock Avoidance
Requires that the system has some additional a priori informa-
tion available.
►Each process declare the maximum number of resources of
deadlock
►Deadlock avoidance method ensures that a system will never
Deadlock Detection
►If a system does not implement either deadlock pre-