0% found this document useful (0 votes)
62 views29 pages

ch6 Cpu Scheduling

This document discusses CPU scheduling in operating systems. It begins by introducing CPU scheduling and its role in multiprogrammed operating systems. It then describes various CPU scheduling algorithms and criteria for evaluating them. Finally, it examines the scheduling algorithms used by several operating systems and provides examples of first-come, first-served (FCFS) and shortest-job-first (SJF) scheduling. The key objectives are to maximize CPU utilization and throughput while minimizing wait times.

Uploaded by

Ayesha Rahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views29 pages

ch6 Cpu Scheduling

This document discusses CPU scheduling in operating systems. It begins by introducing CPU scheduling and its role in multiprogrammed operating systems. It then describes various CPU scheduling algorithms and criteria for evaluating them. Finally, it examines the scheduling algorithms used by several operating systems and provides examples of first-come, first-served (FCFS) and shortest-job-first (SJF) scheduling. The key objectives are to maximize CPU utilization and throughput while minimizing wait times.

Uploaded by

Ayesha Rahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 6: CPU Scheduling

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 6: CPU Scheduling
 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms

Operating System Concepts – 9th Edition 6.2 Silberschatz, Galvin and Gagne ©2013
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
 To examine the scheduling algorithms of several operating systems

Operating System Concepts – 9th Edition 6.3 Silberschatz, Galvin and Gagne ©2013
Basic Concepts
 The objective of multiprogramming is to
have some process running all time to
maximize CPU utilization.
 Several processes are kept in memory at
one time . When one process has to wait
the OS takes the CPU away from that
process and gives the CPU to another
process.
 Scheduling of this kind is the fundamental
function of OS.
 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 and so
on. Eventually final CPU burst ends with
terminate system call.
 CPU burst distribution is of main
concern

Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
 Short-term scheduler selects from among the processes in ready queue,
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
 Scheduling under 1 and 4 is non preemptive or cooperative
 All other scheduling is preemptive ( Preemption means that a process may
be forcibly removed from the CPU even if it does not want to release the CPU.
 Once a process gets CPU, the simplest approach is to allow the process to
continue using CPU until it voluntarily yields CPU. This is called a non-
preemptive approach.

Operating System Concepts – 9th Edition 6.5 Silberschatz, Galvin and Gagne ©2013
Dispatcher
 Dispatcher is the module 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
 The dispatcher should be as fast as possible, since it invokes during every
process switch.
 Dispatch latency –The time it takes for the dispatcher to stop one process
and start another running

Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne ©2013
Scheduling Criteria
 Many criteria have been suggested for comparing CPU scheduling
algorithms.
 CPU utilization – keep the CPU as busy as possible (maximum)
 Throughput – # of processes that complete their execution per time unit, for
long process may be one process/hour, ten process /second. (maximum)
 Arrival time: time at which process enters in the ready queue or the ready
state.
 Burst time: Time required by a process to get execute on CPU.
 Completion/finish time: point of time at which a process get completed.
 Turnaround time – Interval from the time of submission of a process to the
time of completion. (job time+ waiting time) OR Finish time – Arrival time
 Waiting time – amount of time a process has been waiting in the ready
queue (minimum)
W. T = Finish time - burst time – arrival time
Response time – amount of time it takes from when a request was submitted
until the first response is produced, not output (for time-sharing environment)

Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria

 Max CPU utilization


 Max throughput
 Min turnaround time
 Min waiting time
 Min response time

Operating System Concepts – 9th Edition 6.8 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

• Gantt chart: We need some way to represent the state of


the system and any processes in it and how it changes over
time. Gantt charts are used for this purpose.

Process Burst Time


P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 51/3=17

Operating System Concepts – 9th Edition 6.9 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
 The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect - short process behind long process wait for the one big
process to get off the CPU. This effect result in lower CPU & device
utilization.

Operating System Concepts – 9th Edition 6.10 Silberschatz, Galvin and Gagne ©2013
FCFS Example 2.
 Assume the following processes :
Process A.T Burst Time Finish Wait. Time T.A.T
time
A 0.00 3 3 0 3-0=3
B 1.001 6 9 9-6-1.001=1.999 9-1.001=7.999
C 4.001 4 13 13-4- 13-4.001=8.99
4.001=4.999
D 6.001 2 15 15-2- 15-
6.001=6.999 6.001=8.999
A B C D
0 3 9 13 15

A.W.T= (0+1.999+4.999+6.999)/4=3.499
Throughput= 15/4=3.75time units /job
A.T.A.T= (3+7.999+8.99+8.99)/4=7.249

Operating System Concepts – 9th Edition 6.11 Silberschatz, Galvin and Gagne ©2013
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 time
 SJF is optimal – gives minimum average waiting time for a given set of
processes
 Non preemptive scheduling algorithm
 If two processes have the same CPU burst then FCFS is used to break tie.
 The difficulty is knowing the length of the next CPU request
 SJF favors short jobs over long ones. In extreme cases, the constant
arrival of small jobs can lead to starvation of a long job.

Operating System Concepts – 9th Edition 6.12 Silberschatz, Galvin and Gagne ©2013
Example of SJF
ProcessA rBurst Time W.T T.,A.R
P10. 6 3 9 9
P2 2.0 8 16 24 24
P34.0 7 9 16 16
P4 5.0 3 0 3 3

 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24
 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
 A.T.A.T = (9+24+16+3)/4=

 Throuput=24/4=6

Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne ©2013
SJF Example 2
Process A.T Burst Finish Wait. Time T.A.T
Time time
P1 0 7 7 0 7
P2 2 4 13 7 11
P3 4 2 9 3 5
P4 5 4 17 8 12

P1 P3 P2 P4
0 7 9 13 17

A.W.T=(0+7+3+8)/4=4.5
A.T.A.T=(7+11+5+12)/4=8.75

Operating System Concepts – 9th Edition 6.14 Silberschatz, Galvin and Gagne ©2013
Shortest Remaining Time First
 If a new process arrives with a shorter CPU burst than remaining CPU burst
of the currently executing process, it replaces currently executing process.
 It is preemptive version of SJF.

Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne ©2013
Example of Shortest-remaining-time-first
 Now we add the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeT Burst Time W.T T.A.T
P1 0 8-1=7 17-8=9 17-
0=17
P2 1 4-1=3-1=2 5-4-1=0 4
P3 2 9 26-9-2=15 24
P4 3 5 10-5-3=2 7
 Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26

 Average waiting time =[(9+0+15+2)]/4 = 26/4 = 6.5 msec

Operating System Concepts – 9th Edition 6.16 Silberschatz, Galvin and Gagne ©2013
Example of Shortest-remaining-time-first
Process A.T Burst Finish Wait. Time T.A.T
Time time
P1 0 7-2=5 16 16-7=9 16
P2 2 4-2=2 7 1 5
P3 4 1 5 0 1
P4 5 4 11 2 6

P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16

Operating System Concepts – 9th Edition 6.17 Silberschatz, Galvin and Gagne ©2013
H.W of Shortest-remaining-time-first
Process A.T Burst Finish Wait. Time T.A.T
Time time
A 0.0 3
B 1.001 6
C 4.001 4
D 6.001 2

Operating System Concepts – 9th Edition 6.18 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
 A priority number (integer) is associated with each process

 The CPU is allocated to the process with the highest priority (smallest integer 
highest priority)
 Preemptive
 Non preemptive

 SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time, larger the CPU burst the lower the priority and vice versa.
 Equal priority processes are scheduled in FCFS order.

 Problem  indefinite blocking or Starvation – low priority processes may


never execute

 Solution  Aging – as time progresses increase the priority of the process

Operating System Concepts – 9th Edition 6.19 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling (non Preemptive)
ProcessAarri Burst TimeTPriority W.T
P1 10 3 6
P2 1 1 0
P3 2 4 16
P4 1 5 18
P5 5 2 1

 Assume that all processes are arrived at time 0.


 Priority scheduling Gantt Chart

 Average waiting time = (6+0+16+18+1)/5= 8.2 msec

Operating System Concepts – 9th Edition 6.20 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling (Preemptive)
ProcessAarri Arrival Time Burst Time Priority W.T
P1 0 10 3
P2 2 1 1

P3 4 2 2
P4 6 1 4
P5 8 5 5

 Priority scheduling Gantt Chart


P1 P2 P1 P3 P1 P4 P5
0 2 3 4 6 13 14 19

 Average waiting time =

Operating System Concepts – 9th Edition 6.21 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)

 Each process gets a small unit of CPU time (time quantum q),
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.
 Timer interrupts every quantum to schedule next process
 Performance
 q large  FIFO
 q small  q must be large with respect to context switch,
otherwise overhead is too high

Operating System Concepts – 9th Edition 6.22 Silberschatz, Galvin and Gagne ©2013
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
 The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

 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 usec

Operating System Concepts – 9th Edition 6.23 Silberschatz, Galvin and Gagne ©2013
Time Quantum and Context Switch Time

Operating System Concepts – 9th Edition 6.24 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With The Time Quantum

80% of CPU bursts


should be shorter than q

Operating System Concepts – 9th Edition 6.25 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue
 Ready queue is partitioned into separate queues, eg:
 foreground (interactive)
 background (batch)
 Process permanently in a given queue
 Each queue has its own scheduling algorithm:
 foreground – RR
 background – FCFS
 Scheduling must be done between the queues:
 Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
 Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR
 20% to background in FCFS

Operating System Concepts – 9th Edition 6.26 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling

Operating System Concepts – 9th Edition 6.27 Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue

 A process can move between the various queues; aging can be


implemented this way
 Multilevel-feedback-queue scheduler defined by the following
parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will enter
when that process needs service

Operating System Concepts – 9th Edition 6.28 Silberschatz, Galvin and Gagne ©2013
Example of Multilevel Feedback Queue

 Three queues:
 Q0 – RR with time quantum 8 milliseconds
 Q1 – RR 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

Operating System Concepts – 9th Edition 6.29 Silberschatz, Galvin and Gagne ©2013

You might also like