Scheduling
Scheduling
https://www.isical.ac.in/~mandar/courses.html#os
ISI Scheduling 1 / 27
Background: mode switch, process switch
1. Scheduling
2. Possible context-switch
3. Return to user mode
Kernel mode
User mode
ISI Scheduling 2 / 27
Preemptive vs. non-preemptive scheduling
ISI Scheduling 3 / 27
Terminology / recap
Reference: Section 3.2, 6.1
Process states
running, ready, waiting, etc.
typically alternates between CPU bursts and I/O bursts
time-sharing / multiprogramming: to maximize CPU utilization
multiple processes are kept in memory simultaneously
when one process is waiting, another process executes
Properties:
Non-preemptive
Unsuitable for time-sharing systems (∵ each user should get a
share of the CPU at regular intervals)
Average waiting time is not minimal
Convoy effect: many processes may have to wait for one long
process to finish
Example: 1 CPU-bound proc. + many I/O bound procs.
ISI Scheduling 5 / 27
First-come first-served
Example:
Gantt chart:
P1 P2 P3
ISI Scheduling 6 / 27
Shortest job first
Reference: Section 6.3.2
Method:
1. When the CPU is available, assign it to the process with the
shortest next CPU burst.
2. Break ties on a FCFS basis.
Properties:
Optimal in terms of average waiting time
Suitable for job scheduling in a batch system
(use time limit specified by user at time of submission)
Length of the next CPU request is generally not known
Example:
ISI Scheduling 8 / 27
FCFS
P1 P2 P3 P4
FCFS
0 5 10 15 20 25
P1 P1 P1 P1
P2 P2 P2
P3 P3
P4
Ready Queue
ISI Scheduling 9 / 27
Shortest job first
P1 P2 P4 P3
SJF
0 5 10 15 20 25
P3 (9) P4 (5)
P3 (9)
Ready Queue
ISI Scheduling 10 / 27
Shortest remaining time first
Turnaround time for P1
P1 P2 P4 P1 P3
SRTF
P1
P3
Waiting times
P4
0 5 10 15 20 25
P3 (9) P1 (7)
Ready Queue
P3 (9)
ISI Scheduling 11 / 27
Priority scheduling
Reference: Section 6.3.3
Method:
1. Compute a priority for each process.
Internal priorities: computed using time limits, memory
requirements, ratio of avg. I/O burst to avg. CPU burst, etc.
External priorities: computed on the basis of external political /
administrative factors
Properties:
Can be preemptive or non-preemptive (cf. SJF)
ISI Scheduling 12 / 27
Priority scheduling
ISI Scheduling 13 / 27
Priority scheduling
ISI Scheduling 13 / 27
Priority scheduling
Properties:
Starvation / indefinite blocking: if high priority processes keep
arriving, low priority process may have to wait indefinitely for CPU.
Priority scheduling with aging: priority may be increased in
proportion to waiting time to prevent starvation
ISI Scheduling 14 / 27
Round robin
Reference: Section 6.3.4
Time quantum (or time slice): maximum interval of time between two
invocations of the scheduler
a process can be allocated the CPU for one quantum at one time
usually between 10–100ms
Method:
1. Maintain a FIFO queue of ready processes.
2. Allocate CPU to first process from queue; set timer for 1 time
quantum.
3. If running process releases CPU, or timer expires:
preempt current process and switch context to the next process in
the ready queue;
add previously running process to tail of ready queue.
ISI Scheduling 15 / 27
Round robin
Example:
Ready processes Burst time
P1 24
P2 3
P3 3
Time quantum = 4ms
P1 P2 P3 P1
...
0 5 10 15 20
Properties:
Suitable for time-sharing systems (∵ every process gets the CPU
for q time units after waiting for (n − 1)q time units)
Duration of time quantum:
large time quantum ⇒ RR → FCFS
small time quantum ⇒ context-switching overhead ↑
ISI Scheduling 17 / 27
Multilevel queue
Reference: Section 6.3.5
Method:
1. Partition the queue into several separate queues; assign a fixed
priority value to each.
2. Assign each process to some fixed queue, based on its properties.
Example: system procs. / interactive procs. / interactive editing
procs. / batch procs. / student procs.
3. Select a queue based on:
fixed priority (starvation possible), OR
priority-based proportional time slicing, e.g.,
50% of time servicing Q0 ,
30% of time servicing Q1 ,
20% of time servicing Q2 .
4. Select a job from the queue using a suitable scheduling algorithm
(e.g. FCFS, RR).
ISI Scheduling 18 / 27
Multilevel queue
Scheduling algorithm 1
Queue1
Queue2
Schedule
.. across
queues
Properties:
Preemptive
ISI Scheduling 19 / 27
Multilevel feedback queue
Reference: Section 6.3.6
Example:
3 queues, Q0 , Q1 , Q2
scheduling policies:
Q0 = RR (quantum = 8ms) Q1 = RR (quantum = 16ms) Q2 = FCFS
on entry to ready queue, processes assigned to Q0
on exit from Q0 , process is placed at tail of Q1
on exit from Q1 , process is placed at tail of Q2
OPTIONAL : if process waits too long in Q2 , promote it to Q1
ISI Scheduling 20 / 27
Scheduling criteria I
Reference: Section 6.2
ISI Scheduling 21 / 27
Scheduling criteria II
ISI Scheduling 22 / 27
Commonly ignored / simplified details
Kernel mode
User mode
... Pi Pj Pk ...
Process switches are NOT instantaneous.
z }| {
Kernel mode
User mode
Pj Pk
ISI Scheduling 23 / 27
Real-time scheduling
ISI Scheduling 24 / 27
Real-time scheduling
Reference: Section 6.6
ISI Scheduling 26 / 27
Real-time scheduling
Priority inversion
High priority process may have to wait for resource held by a low
priority process
Priority inheritance: processes that are accessing resources
required by high priority process inherit the high priority until they
release the resource
ISI Scheduling 27 / 27