Lecture 3
Scheduling
• Intro to scheduling
• Preemption
• Scheduling Algorithms
– ?
– ?
– ?
• Job scheduling
– Selects processes from the process pool
(usually on disk) and loads them into memory
• Also known as long-term scheduler
• Process scheduling
– Selects processes from the ready queue.
• Short term scheduler
• Thread scheduling
– Schedules threads from within a process
Why schedule processes?
- Increase Efficiency
• The reason scheduling works is because of the CPU-I/O burst cycle.
• When processes run, it has been observed that:
– CPU has a burst of activity
– Then it waits for I/O
• The Durations of the CPU burst have been measured extensively
160
140
120
110
100
80
60
40
20
0
8 16 24
Burst Duration (ms)
32 40
[Silberschatz2005]
Reminder-
How Operating Systems work
• Interrupts
• Dual Mode.
• Timers
• Non-preemptive Scheduling: Once the system has assigned a CPU to a process, the system cannot remove that CPU from that process
• Preemptive Scheduling: The system can remove the running process from the
CPU
• What do we want from a scheduling algorithm?
• It is desirable to maximize CPU utilization and throughput, and to minimize turnaround time, waiting time and response time.
• CPU Utilization – use of the CPU.
• Throughput – number of processes completed per time unit.
• Turnaround time – interval from submission to completion of a process.
• Waiting time – time spent ready to run but not running.
• Response time – the time from submission of a request until the first response is produced.
– normally used for measuring an interactive system.
• Question…..
– Think of some different ways in which we could chose which process to execute next?
• First come First Served
• SJF non preemptive / preemptive
• Priority
• Round robin
• Uses a FIFO queue
• Non preemptive
• An Example FCFS
Process Burst Time
(ms)
P1 30
P2
P3
4
2
Processes arrive at time 0
In the order P1, P2, P3
0
Process Burst Time
(ms)
P1
P2
P3
30
4
2
Gant chart
P1 P2 P3
30 34 36
Average wait time = (0+30+34)/3 = 21.33
Turnaround time = (30+34+36)/3 = 33.33
Process Burst Time
(ms)
P3 2
P2
P1
4
30
P3 P2
0 2 6
P1
Average wait time = (0+2+6)/3 = 2.67
Turnaround time = (2+6+36)/3 = 14.67
36
• Average wait time is very variable
– If the burst times vary greatly
• Convoy effect
• Low device utilisation
– If a CPU bound process is active for long periods, other devices are not being used
• Assign CPU to the process that has the smallest next CPU-burst time
• Non Preemptive or Preemptive
Process Arrival Time
(ms)
Burst Time
(ms)
P1
P2
0
0
8
5
P3
P4
2
2
7
2
Example: SJF
Non Preemptive
Process Arrival Time
(ms)
P1
P2
P3
0
0
2
P4 2
Burst Time
(ms)
8
5
7
2
P3 P1
0
P2
5
P4
7 14 22
• Average Wait Time = (14+0+5+3)/4 = 5.5
Process Arrival Time
(ms)
P1 0
P2 0
P3
P4
2
2
Burst Time
(ms)
8
5
7
2
Using SJF Preemptive
What is the average wait time?
P2 P4 P2 P3 P1
0 2 4 7 14
• Average wait time = (14+2+5+0)/3 = 5.25
22
• Optimal with respect to average waiting time
• How does scheduler know the length of the next CPU-burst time?
• Intro to scheduling
• Preemption
• Scheduling Algorithms
– FCFS
– SJF