Operating System:
Chap5 Process Scheduling
National Tsing-Hua University
2016, Fall Semester
Overview
Basic Concepts
Scheduling Algorithms
Special Scheduling Issues
Scheduling Case Study
Basic Concepts
The idea of multiprogramming:
Keep several processes in memory. Every time one process has to wait, another process takes over the use of the CPU
CPU-I/O burst cycle: Process execution consists of a cycle of CPU execution and I/O wait (i.e., CPU burst and I/O burst).
Generally, there is a large number of short CPU bursts, and a small number of long CPU bursts
A I/O-bound program would typically has many very short
CPU bursts
CPU – I/O Burst Cycle
Histogram of CPU-Burst Times
CPU Scheduler
new task
scheduler ready
running
waiting terminating
tasks signal
events
Selects from ready queue to execute (i.e.
allocates a CPU for the selected process)
Preemptive vs. Non-preemptive
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
Non-preemptive scheduling:
Scheduling under 1 and 4 (no choice in terms of scheduling)
The process keeps the CPU until it is terminated or switched to the waiting state
E.g., Window 3.x
Preemptive scheduling:
Preemptive Issues
Inconsistent state of shared data
Require process synchronization (Chap6)
incurs a cost associated with access to shared data
Affect the design of OS kernel
the process is preempted in the middle of critical changes (for instance, I/O queues) and the kernel (or the device driver) needs to read or modify the same structure?
Unix solution: waiting either for a system call to
complete or for an I/O block to take place before
Dispatcher
Dispatcher module gives control of the CPU to the process selected by scheduler
switching context
jumping to the proper location in the selected program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running
Scheduling time
Interrupt re-enabling time
Scheduling Algorithms
Scheduling Criteria
CPU utilization
theoretically: 0%~100%
real systems: 40% (light)~90% (heavy)
Throughput
number of completed processes per time unit
Turnaround time
submission ~ completion
Waiting time
total waiting time in the ready queue
Response time
Algorithms
First-Come, First-Served (FCFS) scheduling
Shortest-Job-First (SJF) scheduling
Priority scheduling
Round-Robin scheduling
Multilevel queue scheduling
Multilevel feedback queue scheduling
FCFS Scheduling
Process (Burst Time) in arriving order:
P1 (24), P2 (3), P3 (3)
The Gantt Chart of the schedule
Waiting time: P1 = 0, P2 = 24, P3 = 27
Average Waiting Time (AWT): (0+24+27) / 3 = 17
FCFS Scheduling
Process (Burst Time) in arriving order:
P2 (3), P3 (3), P1 (24)
The Gantt Chart of the schedule
Waiting time: P1 = 6, P2 = 0, P3 = 3
Average Waiting Time (AWT): (6+0+3) / 3 = 3
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst
A process with shortest burst length gets the CPU first
SJF provides the minimum average waiting time (optimal!)
Two schemes
Non-preemptive – once CPU given to a process, it cannot be preempted until its completion
Preemptive – if a new process arrives with shorter burst
length, preemption happens
Non-Preemptive SJF Example
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Ready queue: t=0
P1 (7)
0
Schedule
Non-Preemptive SJF Example
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Ready queue: t=0
P1 (7)
P1
0 7
Schedule
Non-Preemptive SJF Example
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Ready queue: t=7
P1 0
P2 (4) P3 (1) P4 (4)
7 Schedule
P3
8
Non-Preemptive SJF Example
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Ready queue: t=8
P1 0
P2 (4) P4 (4)
7 Schedule
P3 8
P2
12
Non-Preemptive SJF Example
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Ready queue: t=12
P1 0
P2 P4
P3
7 8 12 16
Schedule
Wait time = completion time – arrival time – run time (burst time)
P4 (4)
Preemptive SJF Example
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Ready queue: t=0
P1 (7)
0
Schedule
P1
7
Preemptive SJF Example
Process Arrival Time Burst Time P1 0 7
P2 2 4 P3 4 1 P4 5 4
Ready queue: t=2
P1 0
P2 (4)
2 Schedule
P1 (5)
P2
6
Preemptive SJF Example
Process Arrival Time Burst Time P1 0 7
P2 2 4 P3 4 1 P4 5 4
Ready queue: t=4
P1 0
P2 (2)
P3 2
Schedule
P1 (5)
4
P3(1)
P2
5
Preemptive SJF Example
Process Arrival Time Burst Time P1 0 7
P2 2 4 P3 4 1 P4 5 4
Ready queue: t=5
P1 0
P2 (2) P4 (4) Schedule
P1 (5)
P3
2 4
P2
5
P2
7
Preemptive SJF Example
Process Arrival Time Burst Time P1 0 7
P2 2 4 P3 4 1 P4 5 4
Ready queue: t=7
P1 0
P4 (4) Schedule
P1 (5)
P3
2 4
P2
5
P2 7
P4
11
Preemptive SJF Example
Process Arrival Time Burst Time P1 0 7
P2 2 4 P3 4 1 P4 5 4
Ready queue: t=11
P1 0
Schedule
P1 (5)
P3
2 4
P2
5
P2 7
P4 P1
11 16
Wait time = completion time – arrival time – run time (burst time)
Approximate Shortest-Job-First (SJF)
SJF difficulty: no way to know length of the next CPU burst
Approximate SJF : the next burst can be
predicted as an exponential average of the measured length of previous CPU bursts
1 1
1
α ...
α ) 1
α ( α ) 1
α + ( − 1 + − 2 2 +
= t n t n − t n −
Commonly,
α =
n n
n α t ( 1 α ) τ τ
+ 1 = + − history
new one
Exponential predication of next CPU burst
2 4 6 8 10 12 burst length
time
CPU burst t
目目目目目目目目目目