Home > CS4310: Operating Systems > CPU SchedulingCPU Scheduling
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:
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.
Example:
Q: A non-preemptive algorithm makes a scheduling decision whenever… (True/False)
New process arrives in the RL
Process changes from blocked to ready
process requests an
New process arrives in RL
Non-Preemptive: Once a CPU-burst is started, the CPU cannot be interrupted until the CPU burst has been completed.
Preemptive: TODO
We use a Gantt chart
Dispatcher: Gives control of CPU to the process selected by the short-term scheduler. This involvs:
Dispatch latency: Time it takes for dispatcher to stop one process and start another.
CPU Utilization: How much CPU is kept busy.
Throughput: # of processes completing 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 = Reponse when non-preemptive.
Process | Arrive Time | Burst Time |
---|---|---|
P_1 | 0 | 24 |
P_2 | 0 | 3 |
P_3 | 0 | 3 |
TODO Gantt Chart
Waiting time for P_1 = 0, P_2 = 24, P_3 = 27
Average Waiting Time:
Average Turnaround Time:
Average Process Time:
We can see that waiting time = response time.
Process | Arrive Time | Burst Time |
---|
Suppose we get these three processes:
Process | Arrive Time | Burst Time |
---|---|---|
P_1 | 0 | 24 |
P_2 | 0 | 3 |
P_3 | 0 | 3 |
If we order the processes on the ready line as P_2, P_3, P_1; we get
Waiting time for P_1 = 6, $P_2 = 0, $P_3 = 3
Average Waiting Time:
Average Turnaround Time:
Average Process Time:
Convoy Effect: Short processes behind long processes.
This is the basis of SJF
Associate each process with the lenght 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.
Process Time | Burst Time | Waiting Time | Turnaround Time |
---|---|---|---|
P_1 | |||
P_2 | |||
P_3 | |||
P_4 |
All processes arrive at time 0.
SJF will pick the lowest burst-time processes, scheduling: 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)
For long term scheudling, total CPU measrues betwene process creatio nand 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.
Example: Applying Prediction Formula TODO
Predict S_3 based on following TODO
CPU allocated to process with highest priority.
On Aging:
- Aging: To prevent starvation of low priority processes, where processes gain priority over time.
SJF is actually an example of priority scheduling where priority is determined by predicted CPU burst time.
TODO Non-preemptive scheduler