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
Process Arrive Time Burst Time
P_1 0 24
P_2 0 3
P_3 0 3

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
Process Arrive Time Burst Time
P_1 0 8
P_2 0.4 4
P_3 1 1

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 Time Burst Time
P_1 6
P_2 8
P_3 7
P_4 3

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 Time Arrive Time Burst Time
P_1 0 8
P_2 0.4 4
P_3 1 1

TODO Gantt Chart

Example: Preemptive SJF (SRT)

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

Process Arrival Time Burst Time
P_1 0 8
P_2 1 4
P_3 2 9
P_4 3 5

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.

Process Priority Burst Time
P_1 3 8
P_2 1 4
P_3 4 9
P_4 2 5

TODO Gantt chart

Example: Non-Preemptive Priority

Assume all arrived at time 0.

Process Priority Burst Time
P_1 3 10
P_2 1 2
P_3 4 1
P_4 5 5
P_5 2 3

TODO Gantt chart

Example: Preemptive Priority
Process Arrival Time Priority Burst Time
P_1 0 3 10
P_2 4 1 2
P_3 2 4 1
P_4 6 5 5
P_5 3 2 3

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

Process Burst Time
P_1 24
P_2 3
P_3 3

TODO Gantt Chart

Example: Round Robin

Let q = 6

Process Burst Time
p_1 24
p_2 3
p_3 3

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.