(OS) - Unit-2.6 Process Scheduling
(OS) - Unit-2.6 Process Scheduling
(OS) - Unit-2.6 Process Scheduling
By,
Nabaraj Bahadur Negi
1
Introduction
• High throughput
3
Categories of Scheduling Algorithms
• Batch
• Interactive
• Preemptive is necessary
• Real time
• Completion Time: Time taken for the execution to complete, starting from arrival
time.
• Turn Around Time: Time taken to complete after arrival. In simple words, it is
the difference between the Completion time and the Arrival time.
• Waiting Time: Total time the process has to wait before it's execution begins. It is
the difference between the Turn Around time and the Burst time of the process.
• First-come first-served
8
Example :1
Process CPU Burst Time
P1 24
P2 3
P3 3
• In the first Gantt chart below, process P1 arrives first. Waiting time for P1 = 0;
P2 = 24; P3 = 27
• The average waiting time for the three processes is ( 0 + 24 + 27 ) / 3 = 17 ms.
• Suppose that the processes arrive in the order P2 , P3 , P1 ,Waiting time for
P1 = 6; P2 = 0; P3 = 3,In the second Gantt chart below, the same three
processes have an average wait time of ( 0 + 3 + 6 ) / 3 = 3 ms.
9
Example :2
P1 0 2
P2 3 1
P3 5 6
Gantt Chart: Here, black box represents the idle time of CPU.
P1 2 2–0=2 2–2=0
P2 4 4–3=1 1–1=0
P3 11 11- 5 = 6 6–6=0
Now,
Average Turn Around time = (2 + 1 + 6) / 3 = 9 / 3 = 3 unit
Average waiting time = (0 + 0 + 0) / 3 = 0 / 3 = 0 unit
11
Shortest-Job-First Scheduling (SJF)
• Shortest Job First (SJF) is an algorithm in which the process having the smallest
execution time is chosen for the next execution.
• It is a scheduling policy that selects for execution the waiting process with
Shortest job next (SJN), also known as shortest job first (SJF) or shortest
process next (SPN), the smallest execution time.
Non-Preemptive SJF
• Shortest next CPU burst first
• The shortest process is execution first.
Example :1
Process CPU burst time
P1 6
P2 8
P3 7
Average waiting time= (0+3+9+16)/4 = 7
P4 3 12
Example : 2
13
Shortest remaining time next
• Round-Robin Scheduling
• Priority Scheduling
• Multiple Queues
• Shortest Process Next
• Guaranteed Scheduling
• Fair-Share Scheduling
• Lottery Scheduling
15
Round-Robin Scheduling
• One of the Most widely used, oldest, fairest, and easiest algorithms
is round robin.
• Round robin is a preemptive algorithm
• The CPU is shifted to the next process after fixed interval time,
which is called time quantum/time slice.
• Process runs until it blocks or time quantum exceeded
• If the process has blocked or finished before the quantum has
elapsed, the CPU switching is done when the process blocks, of
course.
16
Example :1
17
Example :2
If the CPU scheduling policy is Round Robin with time quantum = 2
unit, calculate the average waiting time and average turn around time.
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
18
Process Turn Around time Waiting time
P1 13 – 0 = 13 13 – 5 = 8
P2 12 – 1 = 11 11 – 3 = 8
P3 5–2=3 3–1=2
P4 9–3=6 6–2=4
P5 14 – 4 = 10 10 – 3 = 7
Now,
Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6
unit
Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit
19
Priority Scheduling
• Jobs are run based on their priority, Always run the job with the highest
priority
• Priorities can be externally defined (e.g., by user) or based on some
process-specific metrics (e.g., their expected CPU burst)
• Can be preemptive
• Can be no preemptive
• Priorities can be static (i.e. they don’t change) or dynamic (they may
change during execution)
• Problem =Starvation – low priority processes may never execute
• Solution = Aging – as time progresses increase the priority of the process
20
Example :1 If the CPU scheduling policy is priority non-preemptive, calculate the
average waiting time and average turn around time. (lower number represents higher
priority)
Process Burst Time (ms) Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
• Priority scheduling Gantt Chart assuming all arrive at time 0
P1 0 8 3
P2 1 1 1
P3 2 3 2
P4 3 2 3
P5 4 6 4
Average waiting time (AWT)= ((0-0) + (8-1) + (9-2) + (12-3) + (14-4)) / 5 = 33 / 5 = 6.6
Average turnaround time (TAT)= ((8-0) + (9-1) + (12-2) + (14-3) + (20-4)) / 5= 53 / 5 =
10.6
22
Priority Preemptive scheduling :
23
Multi-level queue
24
• Let us consider an example of a multilevel queue-scheduling algorithm
with five queues:
• System Processes
• Interactive Processes
• Interactive Editing Processes
• Batch Processes
• Student Processes
25
Example 1: Consider below table of four processes under Multilevel queue
scheduling. Queue number denotes the queue of the process. Priority of queue 1 is
greater than queue 2. queue 1 uses Round Robin (Time Quantum = 2) and queue 2
uses FCFS.
Queue
Process Burst time Arrival time
number
P1 4 0 1
P2 3 0 1
P3 8 0 2
P4 5 10 1
Solution: Gantt chart of the problem
P1 P2 P1 P2 P3 P4 P3
0 2 4 6 7 10 15 20
26
Example 2: Consider below table of four processes under Multilevel queue
scheduling. Queue number denotes the queue of the process. Queue1 is having
higher priority and queue1 is using the FCFS approach and queue2 is using the
round-robin approach(time quantum = 2ms).
27
Multilevel Feedback Queue
• Multilevel feedback queue scheduling, allows a process to move between
queues.
• The idea is to separate processes with different CPU-burst characteristics.
• If a process uses too much CPU time, it will be moved to a lower-priority
queue.
• Similarly, a process that waits too long in a lower-priority queue may be
moved to a higher-priority queue.
28
• Multilevel feedback queue scheduler is defined by the following
parameters:
• The number of queues.
• The scheduling algorithm for each queue.
• The method used to determine when to upgrade a process to a higher-
priority queue.
• The method used to determine when to demote a process to a lower-
priority queue.
• The method used to determine which queue a process will enter when
that process needs service.
29
Guaranteed Scheduling
30
• A ratio of 0.5 means that a process has only had half of what it should
have had, and a ratio of 2.0 means that a process has had twice as much
as it was entitled to.
• The algorithm is then to run the process with the lowest ratio until its
ratio has moved above its closest competitor.
31
Fair-Share Scheduling
• Users are assigned some fraction of the CPU
• Scheduler takes into account who owns a process before scheduling it
• E.g., two users each with 50% CPU share
• User 1 has 4 processes: A, B, C, D
• User 2 has 2 processes: E, F
• If round-robin scheduling is used, a possible scheduling sequence that
meets all the constraints is this one:
A E B E C E D E A E B E C E D E ...
• On the other hand, if user 1 is entitled to twice as much CPU time as user
2, we might get
A B E C D E A B E C D E ...
Numerous other possibilities exist, of course, and can be exploited, depending
on what the notion of fairness is. 32
Lottery Scheduling [ Waldspurger and Weihl 1994 ]
• Jobs receive lottery tickets for various resources
• E.g., CPU time
• At each scheduling decision, one ticket is chosen at random and the job
holding that ticket wins
• If there are 100 tickets outstanding, and one process holds 20 of them, it will
have a 20% chance of winning each lottery. In the long run, it will get about
20% of the CPU.
• Lottery scheduling can be used to solve problems that are difficult to handle
with other methods.
• One example is a video server in which several processes are feeding video
streams to their clients, but at different frame rates.
• Suppose that the processes need frames at 10, 20, and 25 frames/sec. By
allocating these processes 10, 20, and 25 tickets, respectively, they will
automatically divide the CPU in approximately the correct proportion, that
is, 10 : 20 : 25.
33
Scheduling in Real-Time Systems
• A process that fails to meet this test cannot be scheduled because the
total amount of CPU time the processes want collectively is more than
the CPU can deliver.
35