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.
Two Levels:
Non-Preemptive / Cooperative: CPU scheduling decisions that take place when a process:
Preemptive: CPU scheduling decisions that take place when a process:
Issues with Preemption:
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.
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: 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.
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.
| 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.
| Process | Arrive Time | Burst Time |
|---|---|---|
| P_1 | 0 | 8 |
| P_2 | 0.4 | 4 |
| P_3 | 1 | 1 |
TODO Draw Gantt chart
Associate each process with the length of its execution time.
Two Schemes:
Difficulty: It’s hard to get next process length
- Can predict with exponential averaging, or ask user to provide length.
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.
| Process Time | Arrive Time | Burst Time |
|---|---|---|
| P_1 | 0 | 8 |
| P_2 | 0.4 | 4 |
| P_3 | 1 | 1 |
TODO Gantt Chart
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
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:
- Big alpha makes predictions adapt quickly, but follow outliers.
- Small alpha makes predictions adapt slowly, but ignore outliers.
CPU allocated to process with highest priority.
On Aging:
- Aging: To prevent starvation of low priority processes, where processes gain priority over time.
Note — SJF is actually an example of priority scheduling where priority is determined by predicted CPU burst time!
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
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
| 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
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 Quantum — q is typically 10—100 ms.
- q must be large with respect to context switch, otherwise overhead is too high.
- But, a q that is too big will just result in FIFO behavior.
Note — Typically yields higher average turnaround than SJF, but better response time.
- The turnaround time varies with the time quantum. (The best turnaround times occur when 80% of CPU bursts are shorter than q)
Let q = 4
| Process | Burst Time |
|---|---|
| P_1 | 24 |
| P_2 | 3 |
| P_3 | 3 |
TODO Gantt Chart
Let q = 6
| Process | Burst Time |
|---|---|
| p_1 | 24 |
| p_2 | 3 |
| p_3 | 3 |
TODO Gantt Chart
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.
Similar to multilevel scheduling, but addresses starvation and fairness by:
If a process takes too long, we lower the priority, which increases the quantum.