CPU Scheduling

Basic Concept

Maximum CPU utilization is obtained with multiprogramming

CPU-I/O Burst Cycle: Process execution consists of a cycle of CPU execution and I/O wait.

Scheduling

Two Levels:

Non-Preemptive / Cooperative: CPU scheduling decisions that take place when a process:

  1. Switches from running to waiting (e.g., hits I/O)
  2. Terminates

Preemptive: CPU scheduling decisions that take place when a process:

  1. Switching from running to ready (e.g., time slice expires)
  2. Switched from waiting to ready

Issues with Preemption:

Example: Data Racing

Suppose two processes, p2 and p1, which are a producer and consumer.

What if p2 is mid-generation of data when execution switches to p1?

Operating systems must be able to handle preemption to have processes with higher priority and prevent data racing.

Scheduling Algorithms

Non-Preemptive: Once a CPU-burst starts it must run to completion and cannot be interrupted.

Preemptive: A running process can be interrupted when (1) a higher-priority process arrives, or (2) its time slice expires.

Dispatcher

Dispatcher: Gives control of CPU to the process selected by the short-term scheduler. This involves:

Dispatch latency: Time it takes for dispatcher to stop one process and start another.

Scheduling Criteria

CPU Utilization: How much the CPU is kept busy.

Throughput: # of processes that complete their execution per time unit

Turnaround Time: Amount of time to execute a particular process.

Burst + Wait

Waiting Time: Amount of time a process has been waiting in the ready queue.

Response Time: Amount of time it takes from when a request was submitted until the first response is produced, not output.

Wait = Response when non-preemptive.

Scheduling Algorithm Optimization Criteria

FCFS

Example: Non-Preemptive FCFS
ProcessArrive TimeBurst Time
P_1024
P_203
P_303

TODO Draw Gantt chart

Waiting time for P_1 = 0, P_2 = 24, P_3 = 27

We can see that waiting time = response time.

Example: Non-Preemptive FCFS
ProcessArrive TimeBurst Time
P_108
P_20.44
P_311

TODO Draw Gantt chart

Shorted Job First (SJF)

Associate each process with the length of its execution time.

Two Schemes:

  1. Non-Preemptive: Once CPU starts, it cannot be stopped until it completes its quota.
  2. Preemptive: If a new process arrives with less work than the remaining time of the currently executing process, preempt.

Difficulty: It’s hard to get next process length

Example: Non-Preemptive SJF

Suppose all these processes arrive at time 0.

Process TimeBurst Time
P_16
P_28
P_37
P_43

SJF will schedule the shortest burst-time processes first: P_4, P_1, P_3, P_2

TODO Gantt Chart

Note: ART = AWT IIF non-preemptive (we do every process in single blocks, no interruptions) TODO ^ Stop repeating myself and just say ART = AWT with reasoning in a single good location, rather than lightly rehashing in random places like this.

Example: Non-Preemptive SJF
Process TimeArrive TimeBurst Time
P_108
P_20.44
P_311

TODO Gantt Chart

Example: Preemptive SJF (SRT)

Preemptive SJF is called shortest-remaining-time-first.

ProcessArrival TimeBurst Time
P_108
P_214
P_329
P_435

TODO Gantt Chart

SJF Prediction Formula

For long term scheduling, total CPU time is measured between process creation and destruction

For short term, CPU burst changes for each arrival / departure period.

\boxed{ S_{n+1} + \alpha T_n + ( 1 - \alpha ) S_n } \\~\\ \small \textit{where $S_n$ is last estimate of CPU time,} \\ \textit{$T_n$ is last observed CPU time, and} \\ \textit{$\alpha$: controls relative weight of $T_n$ v.s. $S_n$}

Note\alpha is commonly 0.5

More on \alpha:

Priority Scheduler

CPU allocated to process with highest priority.

On Aging:

Note — SJF is actually an example of priority scheduling where priority is determined by predicted CPU burst time!

Example: Non-Preemptive Priority

Assume all arrived at time 0.

ProcessPriorityBurst Time
P_138
P_214
P_349
P_425

TODO Gantt chart

Example: Non-Preemptive Priority

Assume all arrived at time 0.

ProcessPriorityBurst Time
P_1310
P_212
P_341
P_455
P_523

TODO Gantt chart

Example: Preemptive Priority
ProcessArrival TimePriorityBurst Time
P_10310
P_2412
P_3241
P_4655
P_5323

TODO Gantt chart

Round Robin

Each process gets a small unit of CPU time (time quantum q). After the quantum elapses, the process as preempted and added to the end of the ready queue.

More on Quantumq is typically 10—100 ms.

Note — Typically yields higher average turnaround than SJF, but better response time.

Example: Round Robin

Let q = 4

ProcessBurst Time
P_124
P_23
P_33

TODO Gantt Chart

Example: Round Robin

Let q = 6

ProcessBurst Time
p_124
p_23
p_33

TODO Gantt Chart

Multilevel Scheduling

Multilevel scheduling maintains a separate queue of process at each priority level, and within each level, processes are scheduled using round-robin.

We could use aging to prevent starvation.

Multilevel Feedback

Similar to multilevel scheduling, but addresses starvation and fairness by:

If a process takes too long, we lower the priority, which increases the quantum.