SlideShare a Scribd company logo
Chapter 6: CPU Scheduling
 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
Objectives
 To introduce CPU scheduling, which is the basis for
multiprogrammed operating systems
 To describe various CPU-scheduling algorithms
 To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system
Basic Concepts
 Scheduling is a fundamental
operating system function.
 Maximum CPU utilization
obtained with
multiprogramming.
 The algorithm, the scheduler
uses is called scheduling
algorithm.
 Several processes are kept in
memory at one time.
 CPU–I/O Burst Cycle –
Process execution consists of
a cycle of CPU execution and
I/O wait
 CPU burst followed by I/O
burst
 CPU burst distribution is of
main concern
CPU-Scheduling
(with multiprograming)
 In a single processor system, only one process can run at a time.
Others must wait until CPU is free and can be rescheduled.
 Whenever the CPU becomes idle, the operating system must select
one of the processes in the ready queue to be executed (the records
in the queues are generally PCBs of the pocesses).
 The short term scheduler (or CPU scheduler- a part of OS) selects a
process to get the processor (CPU) among the processes which are
already in memory. (i.e. from the ready queue).
 Processor scheduling algorithms try to answer the following crucial
question.
 Which process in the ready queue will get the processor?
 In order to answer this, one should consider the relative importance
of several performance criteria.
CPU Scheduler
 CPU scheduler selects from the processes in memory that are
ready to execute, and allocates the CPU to one of them.
 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.
 5.A new process joins the ready queue
1
1
2
2
3
3
4
4
5
5
Preemptive – Nonpreemptive Scheduling
 In (1) and (4), a new process must be selected from the ready queue.
 In (2), (3) and (5), previously running process or a new process may
be selected.
 Scheduling algorithms that act only in circumstances (1) and (4) are
called nonpreemptive(or cooperative). Once CPU has been
allocated to a process, that process keeps the CPU until it releases
the CPU (either by termination or by requesting I/O). This scheduling
method was used by Microsoft Windows 3.x.
 Otherwise it is preemptive. Windows 95 and all subsequent versions
of Windows operating systems have used preemptive scheduling.
1
1
2
2
3
3
4
4
5
5
Interrupt:
Scheduler picks
another process
Scheduler
dispatch:
Scheduler picks
this process to
execute
Preemptive – Nonpreemptive Scheduling
 A scheduling algorithm which acts on all circumstances is called
preemptive. (i.e. such an algorithm can select a new process in
circumstances 2, 3 and 5).
 The CPU is allocated to the highest-priority process among all
ready processes. The scheduler is called each time a process
enters the ready state.
 Advantages of non-preemptive scheduling algorithms:
 They cannot lead the system to a race condition.
 They are simple.
 Disadvantage of non-preemptive scheduling algorithms:
 They do not allow real multiprogramming.
 Advantage of preemptive scheduling algorithms:
 They allow real multiprogramming.
 Disadvantages of preemptive scheduling algorithms:
 They can lead the system to a race condition.
Dispatcher
 Dispatcher module (invoked during every process
switch) gives control of the CPU to the process
selected by the short-term scheduler; this involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to
restart that program
 Dispatch latency – time it takes for the dispatcher to
stop one process and start another running.
Scheduling Criteria
 CPU utilization - keep the CPU as busy as possible. It is
defined as:
(processor busy time) / (processor busy time +
processor idle time)
 Throughput – # of processes that complete their
execution per time unit.
 Turnaround time – The interval from the time of
submission to the time of completion of a process.
 Waiting time – amount of time a process has been
waiting in the ready queue (waiting for I/O device is not
counted)
 Response time – amount of time it takes from when a
request was submitted until the first response is
produced, not output.
Performance Criteria
 It is desirable that we,
 Maximize CPU utilization
 Maximize throughput
 Minimize turnaround time
 Minimize waiting time
 Minimize response time
CPU Scheduling Algorithms
 First- Come, First-Served (FCFS) - Non preemptive
 Shortest-Job-First (SJF) - Non preemptive
 Shortest-Remaining-Time-First (SRTF) - Preemptive
 Priority Scheduling - Non preemptive, Preemptive
 Round Robin (RR) - Preemptive
 Priority – RR (Multilevel Queue Scheduling, Multilevel Feedback
Queue Scheduling)
Operating System Concepts
First-Come, First-Served (FCFS) Scheduling
 This is the simplest one. In this algorithm the set of
ready processes is managed as FIFO (first-in-first-
out) Queue. The processes are serviced by the
CPU until completion in the order of their entering in
the FIFO queue.
 Once allocated the CPU, each process keeps it until
releasing either due to termination or requesting I/O.
 Average waiting time under FCFS policy is often
quite long.
 FCFS scheduling algorithm is nonpreemptive.
Example: FCFS Scheduling
Consider the following set of processes that arrive at time 0, with the
length of the CPU burst given in milliseconds (ms).
Process CPU Burst Time (ms)
P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
W(P1) = 0; W(P2) = 24ms; W(P3 )= 27ms
Average waiting time: (0 + 24 + 27)/3 = 17ms
CPU utilization = (30/(30+0))x100 = 100 %
Gantt Chart is a bar chart that illustrates a particular schedule, including the
start and finish times of each of the participating processes.
P1 P2 P3
24 27 30
0
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1 .
 The Gantt chart for the schedule is:
 W(P1)= 6ms; W(P2) = 0; W(P3) = 3ms
 Average waiting time: (6 + 0 + 3)/3 = 3ms
 tat(P1)= 30ms; tat(P2) = 3ms; tat(P3) = 6ms
 Much smaller wait time than previous case.
P1
P3
P2
6
3 30
0
Shortest-Job-First (SJF) Scheduling
 Associate with each process the length of its next CPU burst.
Use these lengths to schedule the process with the shortest
next CPU burst time. If the next CPU bursts of two processes
are the same, FCFS scheduling is used to break the tie.
 Two versions:
 nonpreemptive – once CPU given to the process it cannot 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, then preempt
the current one. This scheme is known as
Shortest-Remaining-Time-First (SRTF).
 SJF is optimal – gives minimum average waiting time for a
given set of processes.
 The main problem with the SJF algorithm is to know for each
process the next CPU burst time!!! However some techniques
exist to predict this next CPU burst time (could ask to the
user).
 Another problem is that starvation of long processes may
occur.
ProcessArrival Time CPU Burst Time(ms)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
 SJF (non-preemptive)
 W(P1) = 0; W(P2) = 8-2 = 6ms;
 W(P3 ) = 7-4 = 3ms ; W(P4 ) = 12-5 = 7ms
 Average waiting time = (0 + 6 + 3 + 7)/4 = 4ms
Example of Non-Preemptive SJF
P1 P3 P2
7 16
0
P4
8 12
Example of SJF
ProcessArri Arrival Time l CPU Burst Time(ms)
P1 0 6
P2 0 8
P3 0 7
P4 0 3
 SJF scheduling chart
 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 ms.
P3
0 3 24
P4
P1
16
9
P2
Example of Preemptive SJF (SRTF)
ProcessArrival Time CPU Burst Time(ms)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
 SJF (preemptive)
 Process P1 is started at time 0, since it is the only process in the queue.
Process P2 arrives at time 2. The remaining time for process P1 (5ms) is
larger than the time required by process P2 (4ms), so process P1 is
preempted, and process P2 is scheduled.
 W(P1) = (0-0)+(11-2)=9ms; W(P2) = (2-2)+(5-4)=1ms;
 W(P3 )= 4-4=0; W(P4 )= 7-5=2ms
 Average waiting time = (9 + 1 + 0 +2)/4 = 3ms
P1 P3
P2
4
2 11
0
P4
5 7
P2 P1
16
Example of Shortest-remaining-time-first
 Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT CPU Burst Time(ms)
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart
 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 ms.
P4
0 1 26
P1
P2
10
P3
P1
5 17
Round Robin (RR) (preemptive)
 This scheduling algorithm is designed specially for time
sharing systems. It is similar to FCFS scheduling, but
preemption is added to switch between processes.
 Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds. After this time
has elapsed, the process is preempted and added to the
end of the ready queue.
 If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time
in chunks of at most q time units at once. No process
waits more than (n-1)q time units.
 Performance
 q very large  FIFO
 q small  q must be large with respect to context switch,
otherwise overhead is too high.
Round Robin (continue)
 The scheduler allocates the CPU to each process in the
ready queue for a time interval of up to 1 time quantum in
FIFO (circular) fashion. If the process still running at the
end of the quantum; it will be preempted from the CPU.
 A context switch will be executed, and the process will be
put at the tail of the ready queue. Then the scheduler will
select the next process in the ready queue. However, if
the process has blocked or finished before the quantum
has elapsed, the scheduler will then proceed with the
next process in the ready queue.
Example: RR with Time Quantum = 20
Process Burst Time(ms)
P1 53
P2 17
P3 68
P4 24
 The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
Consider the following set of processes that arrive at time 0 in the
order P1, P2, P3, P4, with the length of the CPU burst given in
milliseconds.
Example: RR with Quantum=10ms
 Consider the following set of process that arrive at time 0 in the
order A,B,C,D with the following given CPU burst time. Find the
average waiting time with RR of quantum : 10 ms
Process CPU burst time (ms)
A 20
B 40
C 14
D 6
A B C D A B C B B
Gantt Chart:
0 10 20 30 36 46 56 60 70 80
W (A) = 36-10 =26 ms W (B) = (10-0) + (46-20) + (60-56)= 40 ms
W (C) = (20-0) + (56-30) = 46 ms W (D) = 30-0 = 30 ms
Average waiting time = (26+40+46+30) /4 = 142/4 = 35.5 ms
Example: RR with Quantum = 10ms
 Consider the following set of process that arrive at time 0 in the order
A,B,C,D with the following given CPU burst time. Find the average
waiting time with RR of quantum = 10 ms and context switch time = 2ms
Process CPU burst time I/O CPU burst time
A 10 40 10
B 40 - -
C 14 - -
D 6 - -
A B C D B C A B B
0 10 12 22 24 34 36 42 44 54 56 60 62 72 74 84 86 96
0 10 50
Wait (A) = 62-50 = 12 ms Wait (B) = 12+22+20+2 = 56 ms
Wait (C) = 24 + 22 = 46 ms Wait (D) = 36 ms
Average waiting time = 150/4 = 37.5 ms
A
idle
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
 The Gantt chart is:
 Typically, higher average turnaround than SJF, but better
response
 q should be large compared to context switch time
 q usually 10ms to 100ms, context switch < 10 microsecond
P P P
1 1 1
0 18 30
26
14
4 7 10 22
P2
P3
P1
P1
P1
Time Quantum and Context Switch Time
Figure showing how smaller time quantum increases context switches.
Priority Scheduling
 A priority number (integer) is associated with each
process
 The CPU is allocated to the process with the highest
priority ((smallest or largest integer value may be
defined as the highest priority)
 Preemptive
 Nonpreemptive
 SJF is priority scheduling where priority is the inverse of
predicted next CPU burst time
 Problem  Starvation – low priority processes may
never execute
 Solution  Aging – as time progresses increase the
priority of the process
Example - Priority Scheduling
Process Arrival time CPU burst time Priority
A 0 100 3
B 0 10 1
C 0 300 3
D 0 60 5
E 80 150 4
Non-preemptive priority Algorithm:
D A E C B
0 60 160 310 610 620
W (A) = 60ms, W (B) = 610ms, W (C) = 310ms, W (D) = 0 ms, W (E) = 80ms
Preemptive priority Algorithm (if a job come with high priority there is a switching):
D A E A C B
0 60 80 230 310 610 620
W(A) = 210 ms, W(B) = 610 ms, W(C) = 310 ms, W(D)=0ms, Wait (E) = 0 ms
5 highest
1 lowest
Example - Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1 (highest)
P3 2 4
P4 1 5
P5 5 2
 Priority scheduling Gantt Chart
 Average waiting time = 8.2 msec
1
0 1 19
P1
P2
16
P4
P3
6 18
P
Multilevel Queue Scheduling
 Another class of scheduling algorithms has been created for situations
in which processes are easily classified into different groups(classes).
 Among classes a priority scheduling algorithm is used, inside a class
RR is used.
 A ready queue is used for each class.
 The following shows a system with five priority classes where RR is
used in each class. As long as there are runnable processes in priority
class 5, just run each one for one quantum (i.e. round robin fashion),
and never bother with lower priority classes. If priority class 5 in
empty , then run the class 4 processes in round robin fashion , and so
on. If priorities are not adjusted from time to time, lower priority classes
may all starve.
 While a class 3 process is running if a highest queue process arrives,
class 3 process would be preempted.
 Time quantum value can be different for classes.
 Each queue may have its own scheduling algorithm.
Multilevel Queue Scheduling
(Priority 5)
(Priority 1)
Multilevel Queue Scheduling
Process Arrival time CPU burst time (ms) Priority
A 0 10 2
B 3 7 1
C 4 6 2
D 12 5 1
E 18 8 1
Assume that Quantum=4 ms for Queue 1 and Quantum =3 ms for Queue
2. Assume that Queue 1 has higher priority when compared to Queue 2.
A B B A D D C E E A C A C
0 3 7 10 12 16 17 18 22 26 29 32 34 36
W (A) = 24ms W (B) = 0ms W (C) = 26ms W (D) = 0ms
W (E) = 0ms W (F) = 10ms
Multilevel Feedback Queue Scheduling
 Normally, when the multilevel queue scheduling algorithm is used,
processes are permanently assigned to a queue when they enter
the system.
 The multilevel feedback queue scheduling algorithm allows a
process to move between queues.
 The scheduler first executes all processes in Queue 1. Only when
Queue 1 is empty will it execute processes in Queue 2. Similarly,
processes in Queue 3 will be executed only if Queues 1 and 2 are
empty. A process that arrives for Queue 1 will preempt a process in
Queue 2.
 A process entering the ready queue is put in Queue 1. If it does not
finish within one quantum, it is moved to the tail of Queue 2. If it
does not complete there in one quantum, it is then preempted and
put into Queue 3.
Multilevel Feedback Queue Scheduling
The figure above shows an example for multilevel feedback queue where
Queue 1 uses RR with Quantum = 8ms, Queue 2 uses RR with
Quantum = 16 and Queue 3 uses FCFS.
Queue 1
Queue 2
Multilevel Feedback Queue Scheduling
Process Arrival time CPU burst time I/O CPU burst time
A 0 5 6 0
B 3 4 2 3
C 4 2 3 4
D 7 5 2 7
E 14 3 2 4
Assume Queue 1 with Quantum =4 ms and Queue 2 with Quantum =
7ms, both use Round Robin.
Queue_1 : ABCDBCEED
Queue_2 : AD
A B C D B C E A D E idle D
0 4 8 10 14 17 21 24 25 26 30 34 41
idle B C idle E A D
0 8 10 13 24 26 32 34
CPU
I/O
Algorithm Evaluation
 How to select CPU-scheduling algorithm for an OS?
 Determine criteria, then evaluate algorithms
 Takes a particular predetermined workload and defines the
performance of each algorithm for that workload
 Consider 5 processes arriving at time 0, in the order given,
with the length of the CPU burst given in ms.
 Consider the FCFS, SJF and RR(quantum = 10 ms)
scheduling algorithms for this set of processes. Which
algorithm would give the minimum average waiting time?
Algorithm Evaluation
 For each algorithm, calculate minimum average waiting time
 Simple and fast, but requires exact numbers for input, applies
only to those inputs
 FCFS is (0+10+39+42+49)/5=28ms:
 Non-preemptive SJF is (10+32+0+3+20)/5=13ms:
 RR is (0+32+20+23+40)/5=23ms:

More Related Content

PPTX
Operating systems - Processes Scheduling
PPTX
Preemptive process example.pptx
PPTX
3Chapter Three- CPU Scheduling this is the best.pptx
PPT
Ch05 cpu-scheduling
PPT
OS-operating systems- ch05 (CPU Scheduling) ...
PDF
Process scheduling (CPU Scheduling)
PDF
PPTX
Operating Systems - CPU Scheduling Process
Operating systems - Processes Scheduling
Preemptive process example.pptx
3Chapter Three- CPU Scheduling this is the best.pptx
Ch05 cpu-scheduling
OS-operating systems- ch05 (CPU Scheduling) ...
Process scheduling (CPU Scheduling)
Operating Systems - CPU Scheduling Process

Similar to Process Scheduling in Ope Spptystems rating (20)

PPT
Operating System Scheduling
PPTX
CPU Scheduling
PPTX
Cpu scheduling
PPTX
CPU Scheduling.pptx this is operating system
PPT
Cpu scheduling
PPT
ch_scheduling (1).ppt
PDF
Unit iios process scheduling and synchronization
PDF
cpu scheduling.pdfoieheoirwuojorkjp;ooooo
PPTX
Chapter 3 - Operating System Scheduling.pptx
PDF
Operating System-Process Scheduling
PPT
Process management in os
PPTX
Cpu_sheduling.pptx
PPTX
CPU Scheduling
PPTX
CPU SCHEDULING ALGORITHMS-FCFS,SJF,RR.pptx
PPT
Window scheduling algorithm
PDF
CH06.pdf
PPT
Unit2 CPU Scheduling 24252.ppBBBBBBBBBBt
PPT
Unit2 CPU Scheduling 24252 (sssssss1).ppt
DOCX
• A process is a program under execution. • Its current activity is indicated...
PDF
OS Process Chapter 3.pdf
Operating System Scheduling
CPU Scheduling
Cpu scheduling
CPU Scheduling.pptx this is operating system
Cpu scheduling
ch_scheduling (1).ppt
Unit iios process scheduling and synchronization
cpu scheduling.pdfoieheoirwuojorkjp;ooooo
Chapter 3 - Operating System Scheduling.pptx
Operating System-Process Scheduling
Process management in os
Cpu_sheduling.pptx
CPU Scheduling
CPU SCHEDULING ALGORITHMS-FCFS,SJF,RR.pptx
Window scheduling algorithm
CH06.pdf
Unit2 CPU Scheduling 24252.ppBBBBBBBBBBt
Unit2 CPU Scheduling 24252 (sssssss1).ppt
• A process is a program under execution. • Its current activity is indicated...
OS Process Chapter 3.pdf
Ad

Recently uploaded (20)

PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
communication and presentation skills 01
PDF
PPT on Performance Review to get promotions
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Artificial Intelligence
PDF
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
introduction to high performance computing
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
communication and presentation skills 01
PPT on Performance Review to get promotions
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Artificial Intelligence
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
III.4.1.2_The_Space_Environment.p pdffdf
Fundamentals of safety and accident prevention -final (1).pptx
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Nature of X-rays, X- Ray Equipment, Fluoroscopy
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Fundamentals of Mechanical Engineering.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Soil Improvement Techniques Note - Rabbi
introduction to high performance computing
Ad

Process Scheduling in Ope Spptystems rating

  • 1. Chapter 6: CPU Scheduling  Basic Concepts  Scheduling Criteria  Scheduling Algorithms
  • 2. Objectives  To introduce CPU scheduling, which is the basis for multiprogrammed operating systems  To describe various CPU-scheduling algorithms  To discuss evaluation criteria for selecting a CPU- scheduling algorithm for a particular system
  • 3. Basic Concepts  Scheduling is a fundamental operating system function.  Maximum CPU utilization obtained with multiprogramming.  The algorithm, the scheduler uses is called scheduling algorithm.  Several processes are kept in memory at one time.  CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait  CPU burst followed by I/O burst  CPU burst distribution is of main concern
  • 4. CPU-Scheduling (with multiprograming)  In a single processor system, only one process can run at a time. Others must wait until CPU is free and can be rescheduled.  Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed (the records in the queues are generally PCBs of the pocesses).  The short term scheduler (or CPU scheduler- a part of OS) selects a process to get the processor (CPU) among the processes which are already in memory. (i.e. from the ready queue).  Processor scheduling algorithms try to answer the following crucial question.  Which process in the ready queue will get the processor?  In order to answer this, one should consider the relative importance of several performance criteria.
  • 5. CPU Scheduler  CPU scheduler selects from the processes in memory that are ready to execute, and allocates the CPU to one of them.  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.  5.A new process joins the ready queue 1 1 2 2 3 3 4 4 5 5
  • 6. Preemptive – Nonpreemptive Scheduling  In (1) and (4), a new process must be selected from the ready queue.  In (2), (3) and (5), previously running process or a new process may be selected.  Scheduling algorithms that act only in circumstances (1) and (4) are called nonpreemptive(or cooperative). Once CPU has been allocated to a process, that process keeps the CPU until it releases the CPU (either by termination or by requesting I/O). This scheduling method was used by Microsoft Windows 3.x.  Otherwise it is preemptive. Windows 95 and all subsequent versions of Windows operating systems have used preemptive scheduling. 1 1 2 2 3 3 4 4 5 5 Interrupt: Scheduler picks another process Scheduler dispatch: Scheduler picks this process to execute
  • 7. Preemptive – Nonpreemptive Scheduling  A scheduling algorithm which acts on all circumstances is called preemptive. (i.e. such an algorithm can select a new process in circumstances 2, 3 and 5).  The CPU is allocated to the highest-priority process among all ready processes. The scheduler is called each time a process enters the ready state.  Advantages of non-preemptive scheduling algorithms:  They cannot lead the system to a race condition.  They are simple.  Disadvantage of non-preemptive scheduling algorithms:  They do not allow real multiprogramming.  Advantage of preemptive scheduling algorithms:  They allow real multiprogramming.  Disadvantages of preemptive scheduling algorithms:  They can lead the system to a race condition.
  • 8. Dispatcher  Dispatcher module (invoked during every process switch) gives control of the CPU to the process selected by the short-term scheduler; this involves:  switching context  switching to user mode  jumping to the proper location in the user program to restart that program  Dispatch latency – time it takes for the dispatcher to stop one process and start another running.
  • 9. Scheduling Criteria  CPU utilization - keep the CPU as busy as possible. It is defined as: (processor busy time) / (processor busy time + processor idle time)  Throughput – # of processes that complete their execution per time unit.  Turnaround time – The interval from the time of submission to the time of completion of a process.  Waiting time – amount of time a process has been waiting in the ready queue (waiting for I/O device is not counted)  Response time – amount of time it takes from when a request was submitted until the first response is produced, not output.
  • 10. Performance Criteria  It is desirable that we,  Maximize CPU utilization  Maximize throughput  Minimize turnaround time  Minimize waiting time  Minimize response time
  • 11. CPU Scheduling Algorithms  First- Come, First-Served (FCFS) - Non preemptive  Shortest-Job-First (SJF) - Non preemptive  Shortest-Remaining-Time-First (SRTF) - Preemptive  Priority Scheduling - Non preemptive, Preemptive  Round Robin (RR) - Preemptive  Priority – RR (Multilevel Queue Scheduling, Multilevel Feedback Queue Scheduling) Operating System Concepts
  • 12. First-Come, First-Served (FCFS) Scheduling  This is the simplest one. In this algorithm the set of ready processes is managed as FIFO (first-in-first- out) Queue. The processes are serviced by the CPU until completion in the order of their entering in the FIFO queue.  Once allocated the CPU, each process keeps it until releasing either due to termination or requesting I/O.  Average waiting time under FCFS policy is often quite long.  FCFS scheduling algorithm is nonpreemptive.
  • 13. Example: FCFS Scheduling Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds (ms). Process CPU Burst Time (ms) P1 24 P2 3 P3 3  Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: W(P1) = 0; W(P2) = 24ms; W(P3 )= 27ms Average waiting time: (0 + 24 + 27)/3 = 17ms CPU utilization = (30/(30+0))x100 = 100 % Gantt Chart is a bar chart that illustrates a particular schedule, including the start and finish times of each of the participating processes. P1 P2 P3 24 27 30 0
  • 14. FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P2 , P3 , P1 .  The Gantt chart for the schedule is:  W(P1)= 6ms; W(P2) = 0; W(P3) = 3ms  Average waiting time: (6 + 0 + 3)/3 = 3ms  tat(P1)= 30ms; tat(P2) = 3ms; tat(P3) = 6ms  Much smaller wait time than previous case. P1 P3 P2 6 3 30 0
  • 15. Shortest-Job-First (SJF) Scheduling  Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest next CPU burst time. If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie.  Two versions:  nonpreemptive – once CPU given to the process it cannot 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, then preempt the current one. This scheme is known as Shortest-Remaining-Time-First (SRTF).  SJF is optimal – gives minimum average waiting time for a given set of processes.  The main problem with the SJF algorithm is to know for each process the next CPU burst time!!! However some techniques exist to predict this next CPU burst time (could ask to the user).  Another problem is that starvation of long processes may occur.
  • 16. ProcessArrival Time CPU Burst Time(ms) P1 0 7 P2 2 4 P3 4 1 P4 5 4  SJF (non-preemptive)  W(P1) = 0; W(P2) = 8-2 = 6ms;  W(P3 ) = 7-4 = 3ms ; W(P4 ) = 12-5 = 7ms  Average waiting time = (0 + 6 + 3 + 7)/4 = 4ms Example of Non-Preemptive SJF P1 P3 P2 7 16 0 P4 8 12
  • 17. Example of SJF ProcessArri Arrival Time l CPU Burst Time(ms) P1 0 6 P2 0 8 P3 0 7 P4 0 3  SJF scheduling chart  Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 ms. P3 0 3 24 P4 P1 16 9 P2
  • 18. Example of Preemptive SJF (SRTF) ProcessArrival Time CPU Burst Time(ms) P1 0 7 P2 2 4 P3 4 1 P4 5 4  SJF (preemptive)  Process P1 is started at time 0, since it is the only process in the queue. Process P2 arrives at time 2. The remaining time for process P1 (5ms) is larger than the time required by process P2 (4ms), so process P1 is preempted, and process P2 is scheduled.  W(P1) = (0-0)+(11-2)=9ms; W(P2) = (2-2)+(5-4)=1ms;  W(P3 )= 4-4=0; W(P4 )= 7-5=2ms  Average waiting time = (9 + 1 + 0 +2)/4 = 3ms P1 P3 P2 4 2 11 0 P4 5 7 P2 P1 16
  • 19. Example of Shortest-remaining-time-first  Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT CPU Burst Time(ms) P1 0 8 P2 1 4 P3 2 9 P4 3 5  Preemptive SJF Gantt Chart  Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 ms. P4 0 1 26 P1 P2 10 P3 P1 5 17
  • 20. Round Robin (RR) (preemptive)  This scheduling algorithm is designed specially for time sharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes.  Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.  If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.  Performance  q very large  FIFO  q small  q must be large with respect to context switch, otherwise overhead is too high.
  • 21. Round Robin (continue)  The scheduler allocates the CPU to each process in the ready queue for a time interval of up to 1 time quantum in FIFO (circular) fashion. If the process still running at the end of the quantum; it will be preempted from the CPU.  A context switch will be executed, and the process will be put at the tail of the ready queue. Then the scheduler will select the next process in the ready queue. However, if the process has blocked or finished before the quantum has elapsed, the scheduler will then proceed with the next process in the ready queue.
  • 22. Example: RR with Time Quantum = 20 Process Burst Time(ms) P1 53 P2 17 P3 68 P4 24  The Gantt chart is: P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Consider the following set of processes that arrive at time 0 in the order P1, P2, P3, P4, with the length of the CPU burst given in milliseconds.
  • 23. Example: RR with Quantum=10ms  Consider the following set of process that arrive at time 0 in the order A,B,C,D with the following given CPU burst time. Find the average waiting time with RR of quantum : 10 ms Process CPU burst time (ms) A 20 B 40 C 14 D 6 A B C D A B C B B Gantt Chart: 0 10 20 30 36 46 56 60 70 80 W (A) = 36-10 =26 ms W (B) = (10-0) + (46-20) + (60-56)= 40 ms W (C) = (20-0) + (56-30) = 46 ms W (D) = 30-0 = 30 ms Average waiting time = (26+40+46+30) /4 = 142/4 = 35.5 ms
  • 24. Example: RR with Quantum = 10ms  Consider the following set of process that arrive at time 0 in the order A,B,C,D with the following given CPU burst time. Find the average waiting time with RR of quantum = 10 ms and context switch time = 2ms Process CPU burst time I/O CPU burst time A 10 40 10 B 40 - - C 14 - - D 6 - - A B C D B C A B B 0 10 12 22 24 34 36 42 44 54 56 60 62 72 74 84 86 96 0 10 50 Wait (A) = 62-50 = 12 ms Wait (B) = 12+22+20+2 = 56 ms Wait (C) = 24 + 22 = 46 ms Wait (D) = 36 ms Average waiting time = 150/4 = 37.5 ms A idle
  • 25. Example of RR with Time Quantum = 4 Process Burst Time P1 24 P2 3 P3 3  The Gantt chart is:  Typically, higher average turnaround than SJF, but better response  q should be large compared to context switch time  q usually 10ms to 100ms, context switch < 10 microsecond P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  • 26. Time Quantum and Context Switch Time Figure showing how smaller time quantum increases context switches.
  • 27. Priority Scheduling  A priority number (integer) is associated with each process  The CPU is allocated to the process with the highest priority ((smallest or largest integer value may be defined as the highest priority)  Preemptive  Nonpreemptive  SJF is priority scheduling where priority is the inverse of predicted next CPU burst time  Problem  Starvation – low priority processes may never execute  Solution  Aging – as time progresses increase the priority of the process
  • 28. Example - Priority Scheduling Process Arrival time CPU burst time Priority A 0 100 3 B 0 10 1 C 0 300 3 D 0 60 5 E 80 150 4 Non-preemptive priority Algorithm: D A E C B 0 60 160 310 610 620 W (A) = 60ms, W (B) = 610ms, W (C) = 310ms, W (D) = 0 ms, W (E) = 80ms Preemptive priority Algorithm (if a job come with high priority there is a switching): D A E A C B 0 60 80 230 310 610 620 W(A) = 210 ms, W(B) = 610 ms, W(C) = 310 ms, W(D)=0ms, Wait (E) = 0 ms 5 highest 1 lowest
  • 29. Example - Priority Scheduling ProcessA arri Burst TimeT Priority P1 10 3 P2 1 1 (highest) P3 2 4 P4 1 5 P5 5 2  Priority scheduling Gantt Chart  Average waiting time = 8.2 msec 1 0 1 19 P1 P2 16 P4 P3 6 18 P
  • 30. Multilevel Queue Scheduling  Another class of scheduling algorithms has been created for situations in which processes are easily classified into different groups(classes).  Among classes a priority scheduling algorithm is used, inside a class RR is used.  A ready queue is used for each class.  The following shows a system with five priority classes where RR is used in each class. As long as there are runnable processes in priority class 5, just run each one for one quantum (i.e. round robin fashion), and never bother with lower priority classes. If priority class 5 in empty , then run the class 4 processes in round robin fashion , and so on. If priorities are not adjusted from time to time, lower priority classes may all starve.  While a class 3 process is running if a highest queue process arrives, class 3 process would be preempted.  Time quantum value can be different for classes.  Each queue may have its own scheduling algorithm.
  • 32. Multilevel Queue Scheduling Process Arrival time CPU burst time (ms) Priority A 0 10 2 B 3 7 1 C 4 6 2 D 12 5 1 E 18 8 1 Assume that Quantum=4 ms for Queue 1 and Quantum =3 ms for Queue 2. Assume that Queue 1 has higher priority when compared to Queue 2. A B B A D D C E E A C A C 0 3 7 10 12 16 17 18 22 26 29 32 34 36 W (A) = 24ms W (B) = 0ms W (C) = 26ms W (D) = 0ms W (E) = 0ms W (F) = 10ms
  • 33. Multilevel Feedback Queue Scheduling  Normally, when the multilevel queue scheduling algorithm is used, processes are permanently assigned to a queue when they enter the system.  The multilevel feedback queue scheduling algorithm allows a process to move between queues.  The scheduler first executes all processes in Queue 1. Only when Queue 1 is empty will it execute processes in Queue 2. Similarly, processes in Queue 3 will be executed only if Queues 1 and 2 are empty. A process that arrives for Queue 1 will preempt a process in Queue 2.  A process entering the ready queue is put in Queue 1. If it does not finish within one quantum, it is moved to the tail of Queue 2. If it does not complete there in one quantum, it is then preempted and put into Queue 3.
  • 34. Multilevel Feedback Queue Scheduling The figure above shows an example for multilevel feedback queue where Queue 1 uses RR with Quantum = 8ms, Queue 2 uses RR with Quantum = 16 and Queue 3 uses FCFS. Queue 1 Queue 2
  • 35. Multilevel Feedback Queue Scheduling Process Arrival time CPU burst time I/O CPU burst time A 0 5 6 0 B 3 4 2 3 C 4 2 3 4 D 7 5 2 7 E 14 3 2 4 Assume Queue 1 with Quantum =4 ms and Queue 2 with Quantum = 7ms, both use Round Robin. Queue_1 : ABCDBCEED Queue_2 : AD A B C D B C E A D E idle D 0 4 8 10 14 17 21 24 25 26 30 34 41 idle B C idle E A D 0 8 10 13 24 26 32 34 CPU I/O
  • 36. Algorithm Evaluation  How to select CPU-scheduling algorithm for an OS?  Determine criteria, then evaluate algorithms  Takes a particular predetermined workload and defines the performance of each algorithm for that workload  Consider 5 processes arriving at time 0, in the order given, with the length of the CPU burst given in ms.  Consider the FCFS, SJF and RR(quantum = 10 ms) scheduling algorithms for this set of processes. Which algorithm would give the minimum average waiting time?
  • 37. Algorithm Evaluation  For each algorithm, calculate minimum average waiting time  Simple and fast, but requires exact numbers for input, applies only to those inputs  FCFS is (0+10+39+42+49)/5=28ms:  Non-preemptive SJF is (10+32+0+3+20)/5=13ms:  RR is (0+32+20+23+40)/5=23ms: