CH 5 Process Scheduling STD
CH 5 Process Scheduling STD
CH 5 Process Scheduling STD
Process Scheduling
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 5: CPU Scheduling
OBJECTIVES
To introduce CPU scheduling, which is the basis for
multiprogrammed operating systems
To describe various CPU-scheduling algorithms
Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne ©2013
5.1 Basic Concepts
Maximum CPU utilization
obtained with
multiprogramming
Most processes exhibit the
following behavior:
CPU burst followed by I/O
burst
CPU–I/O Burst Cycle –
Process execution consists
of a cycle of CPU
execution and I/O wait
CPU burst distribution is of
main concern
Figure 5.1 Alternating sequence of CPU and I/O bursts
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne ©2013
Operating Systems
{
printf(“Enter first integer: “); (i)I/O________
cycle
scanf(“%d”, &b);
(ii) ________
y = b+(b*b)+1; CPU burst
Figure 5.2
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013
Operating Systems
6
Operating Systems
– Job accepted
• Put on HOLD and placed in queue
– Job state changes from NEW to READY
• Indicates job waiting for CPU
– Job state changes from READY to RUNNING
• When selected for CPU and processing
– Job state changes from RUNNING to WAITING
• Requires unavailable resources
– Job state changes to TERMINATED
Chapter 2
7
Operating Systems
(Continued Next)
8
CPU Scheduler
Whenever the CPU becomes idle, the operating system must
select one of the processes in the ready queue to be executed.
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne ©2013
Nonpreemptive Scheduling
Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013
Preemptive scheduling
Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne ©2013
Dispatcher
Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Dispatch Latency
event Response to event
Response interval
Process made
available
Real-time
Interrupt Dispatch latency
Process
Processing
Execution
conflicts dispatch
Chapter 2
time
13
Operating Systems
Example:
Concurrent
Execution of
Processes
(1/3)
Chapter 2
14
Operating Systems
100
101
102
103
104
105
Dispatcher
15
Operating Systems
Notes:
16
Operating Systems
17
Chapter 2 Operating Systems
18
Operating Systems
Process A
Process B
Process C
Dispatcher
Instruction Cycles
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70
19
Operating Systems
Answer:
4 6
Process A
6 4
Process B
6 6 4
Process C
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70
Dispatcher
Chapter 2
20
Operating Systems
Answer:
4 6
Process A
6 4
Process B
6 6 4
Process C
Dispatcher
Instruction Cycles
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70
21
Operating Systems
x y z
5.2 Scheduling Criteria
Operating System Concepts – 9th Edition 5.23 Silberschatz, Galvin and Gagne ©2013
EXERCISE (DIY!!)
The table below shows the process’s location at memory for processes P, Q, R and
Dispatcher. A single processor is used to run all processes. Process P request for I/O at
instruction cycle 4 and wait up to 25 instruction cycles. Then, process R request for I/O at
instruction cycle 23 and wait up to 24 instruction cycles. All processes run from 0 to 100
instruction cycles with three different indicators for running, ready and blocked state (unit
in milliseconds). Assume that the maximum CPU burst for each process is 6 instruction
cycles. Calculate the following questions [8m]:
Process Addresses
P 3000-3050
Q 5000-5014
R 2000-2020
Dispatcher 200-205
Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne ©2013
Optimization Criteria for Scheduling
Operating System Concepts – 9th Edition 5.25 Silberschatz, Galvin and Gagne ©2013
5.3 Scheduling Algorithm
Algorithm types
First –come, First-serve (FCFS)
Shortest-Job-First Scheduling (SJF)
Round-Robin Scheduling (RR)
Priority Scheduling
Multilevel Queue Scheduling
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 5.27 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
P2 P3 P1
0 3 30
Operating System Concepts – 9th Edition 5.28 Silberschatz, Galvin and Gagne ©2013
Operating Systems
FCFS: Example 1
Three processes arrived in order Job A, Job B, Job C at time t0
with different CPU burst. Calculate the average waiting time and
average turn around time. Assume that FCFS algorithm applied.
0 15 17 18
(Continued Next)
29
Operating Systems
Solution:
FCFS: Example 2
Three processes arrived in order Job C, Job B, Job A at time t0
with different CPU burst time. Calculate the average waiting
time and average turn around time. Assume that FCFS
algorithm applied.
Process CPU Burst Time
Job C 1
Job B 2
Job A 15
Exercise 1:
0 +14 +15
• Average Waiting Time = = 9.67 ms
3
Chapter 2
FCFS:
• Good for batch systems
• Unacceptable in interactive systems
– Unpredictable waiting/turnaround time
• Disadvantages
– Average waiting/turnaround time varies; seldom
minimized
– Convoy effect whereby short processes queue
behind long processes;
• Results in lower CPU and device utilization
Chapter 2
34
Shortest-Job-First (SJF)/SJN
None pre-emptive
Also known as Shortest Job Next (SJN)
Associate with each process the length of its next CPU
burst/cycle time
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
How do we know what is the length of the next CPU request
Could ask the user
what if the user lies?
Operating System Concepts – 9th Edition 5.35 Silberschatz, Galvin and Gagne ©2013
Example of SJF/SJN
P4 P1 P3 P2
0 3 9 16 24
Waiting
Operating Time
System = Turnaround
Concepts – 9th Edition time time – Burst
5.36 time Silberschatz, Galvin and Gagne ©2013
Solutions
P4 P1 P3 P2
0 3 9 16 24
P1 6 9–0=9 9–6=3
P2 8 24 – 0 = 24 24 – 8 = 16
P3 7 16 – 0 = 16 16 – 7 = 9
P4 3 3–0=3 3–3=0
52/4 = 13 28/4 = 7
Operating System Concepts – 9th Edition 5.37 Silberschatz, Galvin and Gagne ©2013
Operating Systems
SJF/SJN: Example 3
38
Operating Systems
Solution:
• We can see:
- Job B finishes in its given time, (2)
- Job D finishes in its given time plus the time waited
for Job B to run, (4+2)
- Job A, time given plus B’s time, (5+4+2)
Chapter 2
40
Operating Systems
Waited time
(2) + (4 + 2) + (5 + 4 + 2) + (6 + 5 + 4 + 2) = 9.0
4
• As we can see:
- The time 1st job (B) appears 4 times; The 2nd job (D)
appears 3 times ; The 3rd job (A) appears 2 times and
n = number of job in queue; tj (j=1,2,3,…,n)
SJN / SJF:
• Easy implementation in batch environment
– CPU time requirement known in advance
• Optimal algorithm
– All jobs are available at same time.
– CPU estimation are available and more accurate.
42
Operating Systems
Exercise 2:
Job D 4
2 Job X 3
Job Y 1
43
Operating Systems
Solution 2:
A5 A5 A5 A5 A5 C6
B2 C6 C6 C6 C6
Processes in
C6 D4 D4 D4 system to use
D4 X3 X3 CPU
Y1 Note: JobCPU
Chapter 2
44
Operating Systems
10 + 0 +15 + 6 +1+ 0
Chapter 2
P1 P2 P4 P1 P3
0 1 5 10 17 25
• SRT : Example 4
47
Operating Systems
Here Job A is preempted by Job B because Job B has less CPU time remaining.
Here Job B is preempted by Job C because Job C has less CPU time
remaining.
Now Job B can resume because Job C has finished.
49
Operating Systems
50
Operating Systems
(Continued Next)
51
Operating Systems
7.0 B D
10.0 D -
(Continued Next)
52
Operating Systems
SRT:
• Often used in batch environments
– Short jobs given priority
54
Round Robin (RR)
Preemptive. Used extensively in interactive systems.
Each process gets a small unit of CPU time (time quantum q).
After this time has elapsed, the process is preempted and
added to the end of the ready queue (FCFS).
• Efficiency
– Depends on time quantum size
• In relation to average CPU burst
57
Example of RR with Time Quantum = 4
• RR : Example 6
Job C 9 2.0
Time Slice =
Job D 5 3.0 4 milliseconds
(Continued Next)
59
Operating Systems
Time:
0 1 2 3 4 5 6 7 8 12 16 20 24 25 26
A 8 A7 A 6 A 5 A 4 A4 A4 A4
B4 B4 B4 B4
C9 C9 C9 C9 C5 C5 C5 C1 C1
D5 D5 D5 D5 D1 D1 D1
Chapter 2
60
Operating Systems
20 + 7 + 24 + 22 73
Average Turnaround Time = = =18.25
Chapter 2
4 4
(Continued Next)
61
Operating Systems
12 + 3+15+17 47
Average Waiting Time = = =11.75
4 4
62
Time Quantum and Context Switch Time
Operating System Concepts – 9th Edition 5.63 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With The Time Quantum
Operating System Concepts – 9th Edition 5.64 Silberschatz, Galvin and Gagne ©2013
Operating Systems
• RR : Example 7
Chapter 2
65
Operating Systems
• Case (a)
- There is no context switch
- Job A runs to completion since CPU burst end shortly
before time quantum expires
- No different between RR and FCFS policies
• Case (b)
- There is 1 context switch for the rest of Job A.
Chapter 2
(Continued Next)
66
Operating Systems
• Case (c)
- There is 10 context switches for 2nd cycle of Job A.
- The job is preempted every time the time quantum expires
(Continued Next)
67
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
▪ Nonpreemptive
SJF is simply of priority scheduling where it uses the inverse
of the next CPU burst time as its priority –
▪ The smaller the CPU burst, the higher the priority.
If two jobs having the same priority are READY, it works on a
FIRST COME, FIRST SERVED basis.
Problem Starvation – low priority processes may never
execute.
Solution Aging – as time progresses increase the priority of
the process.
Operating System Concepts – 9th Edition 5.68 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling
◼ Consider the following five processes and their burst time
Process CPU Burst Priority Turnaround Waiting time
time
P1 10 3 16 – 0 = 16 16 – 10 = 6
P2 1 1 1–0=1 1–1=0
P3 2 4 18 – 0 = 18 18 – 2 = 16
P4 1 5 19 – 0 = 19 19 – 1 = 18
P5 5 2 6–0=6 6–5=1
P2 P5 P1 P3 P4
0 1 6 16 18 19
Average waiting time = 8.2 msec
Operating System Concepts – 9th Edition 5.69 Silberschatz, Galvin and Gagne ©2013
Operating Systems
70
Operating Systems
Exercise 3:
Five processes Job A, Job B, Job C, Job D and Job E arrived
at time t0 with different CPU burst and priority level. Calculate
the average waiting time and average turnaround time.
Assume the following scheduling algorithm applied separately.
Job C 2 4
Job D 1 5
Job E 5 2
71
Combining Priority Scheduling and RR
P4 7 1
P5 3 3
25
Operating System Concepts – 9th Edition 5.72 Silberschatz, Galvin and Gagne ©2013
Combining Priority Scheduling and RR
25
Process Burst Time Priority Completion time Turnaround time Waiting time
P1 4 3 25 25 – 0 = 25 25 – 4 = 21
P2 5 2 16 16 – 0 = 16 16 – 5 = 11
P3 8 2 20 20 – 0 = 20 20 – 8 = 12
P4 7 1 7 7–0=7 7–7=0
P5 3 3 27 27 – 0 = 27 27 – 3 = 24
Waiting
Operating Time
System = Turnaround
Concepts – 9th Edition time time – Burst
5.73 time Silberschatz, Galvin and Gagne ©2013
Multilevel Queue
Hybrid environment. 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 5.74 Silberschatz, Galvin and Gagne ©2013
Separate Queue For Each Priority
Operating System Concepts – 9th Edition 5.75 Silberschatz, Galvin and Gagne ©2013
Operating Systems
(Continued Next)
76
Operating Systems
• Good environment
Chapter 2
77
Operating Systems
Q1
(High Priority)
Q2
(Low Priority
J2 J4
J1
Chapter 2
J5
J3
Q1 J3 J2
(High Priority)
Q2 J5 J4 J1
(Low Priority
Chapter 2
79
Operating Systems
• High-priority jobs
– Initial priority favorable
• Treated like all other jobs afterwards
• Quantum interrupt
– Job preempted
• Moved to next lower queue
• May have priority increased
Chapter 2
• Good environment
– Jobs handled by cycle characteristics (CPU or I/O)
– Interactive systems
80
Operating Systems
Q1
(High Priority)
J2 J4 3 4
J1 4 2
Chapter 2
5 3
J5
J3
Q1 J3 J2
(High Priority)
Q2 J2
J2 J5 J4 J1 Job CPU Burst
(Low Priority 1 5
5 2 10
3 4
4 2
Chapter 2
5 3
82
Operating Systems
83
Operating Systems
Q1 J3 J2
(High Priority)
Q2 J2
J2 J5 J4 J1 Job CPU Burst
(Low Priority) 1 9
10 2 15
3 4
4 2
Chapter 2
5 3
84
Operating Systems
Case 4: Aging
85
Operating Systems
Case 4: Aging
(FCFS) CPU
Q1
(High Priority)
5 5
Case 4: Aging
(FCFS) CPU
Q1 J2
(High Priority)
5 5
Job 2 is running
87
Operating Systems
Case 4: Aging
(FCFS) CPU
Q1 J2
(High Priority)
5 5
J3 J4
t50 → Next new jobs arrived: J3, J4 (low priority), J2 still running
88
Operating Systems
Case 4: Aging
(FCFS) CPU
Q1 J2
(High Priority)
J5 5 5
Case 4: Aging
(FCFS) CPU
Q1 J5 J1 J2
(High Priority)
5 5
Operating System Concepts – 9th Edition 5.91 Silberschatz, Galvin and Gagne ©2013
Operating Systems
92
Multilevel Feedback Queue
Operating System Concepts – 9th Edition 5.93 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 • Processes in queue Q2 are run on an
FCFS basis but are run only when
If it does not finish in 8 milliseconds, job is queues Q0 and Q1 are empty.
moved to queue Q1 • This scheduling algorithm gives highest
At Q1 job is again served FCFS and priority to any process with a CPU burst
receives 16 additional milliseconds of 8 ms or less.
Operating System Concepts – 9th Edition 5.94 Silberschatz, Galvin and Gagne ©2013
Operating Systems
MFQ: Exercise 9
Five processes Job A, Job B, Job C, Job D and Job E arrived at time
t0 with different CPU burst and priority level. Assume that MFQ
Scheduling algorithm is applied with 2 levels and the priority range 1
Highest. By given an initial time quantum for the first level is 5 CPU
bursts and the last level using FCFS, calculate the average waiting
time and average turnaround time.
Job D 1 5
Job E 5 2
95
Process CPU Burst Priority Operating Systems
Job A 10 5 0 3
Job B 1 0 1
Job C 2 0 4
Job D 1 0 5
Job E 5 0 2
5 4 3 2 1
Job D Job C Job A Job E Job B CPU
Q0 JB JE JA JC JD
time quantum = 5
0 1 6 11 13 14
Q1 JA
FCFS
14 19
Chapter 2
96
Operating Systems
Q0 JB JE JA JC JD
time quantum = 5
0 1 6 11 13 14
Q1 JA
FCFS
14 19
97
Operating Systems
EXERCISE (DIY!!)
The table below shows five jobs A, B, C, D and E arrived at different times and with different
CPU bursts. The Multilevel Feedback Queue (MFQ) scheduling is used with three queues (Qi).
Given an initial time, the quantum for the Q0 is 5, Q1 is 10 and the last queue uses FCFS.
Calculate the following questions [12m]:
i. What is the maximum value of CPU burst time that the jobs get at the Q1 level? [2]
ii. Which job(es) will finish at level Q1? [2]
iii. At the Q1 level, what is the second job that gets the CPU, and at what time does it
Chapter 2
occur? [2]
iv. What is the Waiting Time of Process B? [2]
v. What is the Average Turnaround time? [2]
vi. What is the Average Waiting time? [2]
98
Operating Systems
Summary
• Processor Manager allocates CPU among all users
• Job Scheduler
– Assigns job to READY queue
• Based on characteristics
• Process Scheduler
– Instant-by-instant allocation of CPU
• Scheduling algorithm is unique
– Characteristics, objectives, and applications
Chapter 2
99
End of Chapter 5
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013