Operating Systems: Lecture 3: Process Scheduling Algorithms
Operating Systems: Lecture 3: Process Scheduling Algorithms
Lec 3
Operating Systems
CPU Scheduling
How is the OS to decide which of several tasks to take off a queue? Scheduling: deciding which threads are given access to resources from moment to moment.
Operating Systems 2
Lec 3
These are unrealistic but simplify the problem Does fair mean fairness among users or programs?
If I run one compilation job and you run five, do you get five times as much CPU?
Often times, yes!
Goal: dole out CPU time to optimize some desired parameters of the system.
What parameters?
Operating Systems 3
Lec 3
Lec 3
Operating Systems
Lec 3
Operating Systems
Lec 3
Operating Systems
Maximize Throughput
Jobs per second Throughput related to response time, but not identical
Minimizing response time will lead to more context switching than if you maximized only throughput
Minimize overhead (context switch time) as well as efficient use of resources (CPU, disk, memory, etc.)
Fairness
Share CPU among users in some equitable way Not just minimizing average response time
Lec 3
Operating Systems
Draw the Gantt Chart and compute Average Waiting Time and Average Completion Time.
Operating Systems 8
Lec 3
P1
0
P2 P3
24 27 30
Waiting Time
P1: 0 P2: 24 P3: 27
Completion Time:
P1: 24 P2: 27 P3: 30
Lec 3
Lec 3
Operating Systems
10
P2 P3
0 3 6
P1
30
Waiting Time
P1: 0 P2: 3 P3: 6
Completion Time:
P1: 3 P2: 6 P3: 30
Average Waiting Time: (0+3+6)/3 = 3 (compared to 17) Average Completion Time: (3+6+30)/3 = 13 (compared to 27)
Operating Systems 11
Lec 3
Lec 3
Operating Systems
12
Lec 3
Operating Systems
13
After quantum expires, the process is preempted and added to the end of the ready queue Suppose N processes in ready queue and time quantum is Q ms:
Each process gets 1/N of the CPU time In chunks of at most Q ms What is the maximum wait time for each process?
Lec 3
Operating Systems
14
After quantum expires, the process is preempted and added to the end of the ready queue Suppose N processes in ready queue and time quantum is Q ms:
Each process gets 1/N of the CPU time In chunks of at most Q ms What is the maximum wait time for each process? No process waits more than (n-1)q time units Lec 3 Operating Systems 15
After quantum expires, the process is preempted and added to the end of the ready queue Suppose N processes in ready queue and time quantum is Q ms:
Each process gets 1/N of the CPU time In chunks of at most Q ms What is the maximum wait time for each process? No process waits more than (n-1)q time units
Lec 3
Operating Systems
16
After quantum expires, the process is preempted and added to the end of the ready queue Suppose N processes in ready queue and time quantum is Q ms:
Each process gets 1/N of the CPU time In chunks of at most Q ms What is the maximum wait time for each process? No process waits more than (n-1)q time units
Lec 3
Operating Systems
17
P1 P2 P3 P1 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30
Lec 3
Operating Systems
18
P1 P2 P3 P1 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30
Completion Time:
P1: 30 P2: 7 P3: 10
Lec 3
Lec 3
Operating Systems
20
Waiting Time: A process can finish before the time quantum expires, and release the CPU.
P1: (68-20)+(112-88) = 72 P2: (20-0) = 20 P3: (28-0)+(88-48)+(125-108) = 85 P4: (48-0)+(108-68) = 88 P1: 125 P2: 28 P3: 153 P4: 112
Completion Time:
Lec 3
Average Waiting Time: (72+20+85+88)/4 = 66.25 Average Completion Time: (125+28+153+112)/4 = 104.5
Operating Systems 21
RR Summary
Pros and Cons:
Better for short jobs (+) Fair (+) Context-switching time adds up for long jobs (-)
The previous examples assumed no additional time was needed for context switching in reality, this would add to wait and completion time without actually progressing a process towards completion. Remember: the OS consumes resources, too!
Lec 3
22
Both FCFS and RR finish at the same time But average response time is much worse under RR!
Bad when all jobs are same length
Also: cache state must be shared between all jobs with RR but can be devoted to each job with FIFO
Total time for RR longer even for zero-cost context switch!
Operating Systems 23
Lec 3
Lec 3
Operating Systems
24
Scheduling
The performance we get is somewhat dependent on what kind of jobs we are running (short jobs, long jobs, etc.) If we could see the future, we could mirror best FCFS Shortest Job First (SJF) a.k.a. Shortest Time to Completion First (STCF):
Run whatever job has the least amount of computation to do
Shortest Remaining Time First (SRTF) a.k.a. Shortest Remaining Time to Completion First (SRTCF):
Preemptive version of SJF: if a job arrives and has a shorter time to completion than the remaining time on the current job, immediately preempt CPU
These can be applied either to a whole program or the current CPU burst of each program
Idea: get short jobs out of the system Big effect on short jobs, only small effect on long ones Result: better average response time
Lec 3
Operating Systems
25
Scheduling
But, this is hard to estimate We could get feedback from the program or the user, but they have incentive to lie! SJF/SRTF are the best you can do at minimizing average response time
Provably optimal (SJF among non-preemptive, SRTF among preemptive) Since SRTF is always at least as good as SJF, focus on SRTF
Lec 3
Operating Systems
26
Scheduling
But, this is hard to estimate We could get feedback from the program or the user, but they have incentive to lie! SJF/SRTF are the best you can do at minimizing average response time
Provably optimal (SJF among non-preemptive, SRTF among preemptive) Since SRTF is always at least as good as SJF, focus on SRTF
Lec 3
Operating Systems
27
Example: SRTF
A or B
C C I/O
A,B: both CPU bound, run for a week C: I/O bound, loop 1ms CPU, 9ms disk I/O If only one at a time, C uses 90% of the disk, A or B could use 100% of the CPU With FIFO: Once A and B get in, the CPU is held for two weeks What about RR or SRTF?
Easier to see with a timeline
Lec 3
Operating Systems
28
Example: SRTF
A or B
C C I/O
A,B: both CPU bound, run for a week C: I/O bound, loop 1ms CPU, 9ms disk I/O
Lec 3
Operating Systems
29
But even non-malicious users have trouble predicting runtime of their jobs
Bottom line, cant really tell how long job will take
However, can use SRTF as a yardstick for measuring other policies, since it is optimal
Lec 3
Operating Systems
30
Lec 3
Operating Systems
31
n +1 = t n + (1 ) n .
1. t n = actual length of n th CPU burst 2. n +1 = predicted value for the next CPU burst 3. , 0 1 4. Define :
32
33
If we expand the formula, we get: n+1 = tn+(1 - ) tn -1 + +(1 - )j tn -j + +(1 - )n +1 0 Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor
Operating Systems 34
Lec 3
Priority Scheduling
A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority)
Preemptive (if a higher priority process enters, it receives the CPU immediately) Nonpreemptive (higher priority processes must wait until the current process finishes; then, the highest priority ready process is selected)
SJF is a priority scheduling where priority is the predicted next CPU burst time Problem Starvation low priority processes may never execute Solution Aging as time progresses increase the priority of the process
Operating Systems 35
Lec 3
Priority Inversion
Consider a scenario in which there are three processes, one with high priority (H), one with medium priority (M), and one with low priority (L). Process L is running and successfully acquires a resource, such as a lock or semaphore. Process H begins; since we are using a preemptive priority scheduler, process L is preempted for process H. Process H tries to acquire Ls resource, and blocks (because it is held by L). Process M begins running, and, since it has a higher priority than L, it is the highest priority ready process. It preempts L and runs, thus starving high priority process H. This is known as priority inversion. What can we do?
Operating Systems 36
Lec 3
Priority Inversion
Process L should, in fact, be temporarily of higher priority than process M, on behalf of process H. Process H can donate its priority to process L, which, in this case, would make it higher priority than process M. This enables process L to preempt process M and run. When process L is finished, process H becomes unblocked. Process H, now being the highest priority ready process, runs, and process M must wait until it is finished. Note that if process Ms priority is actually higher than process H, priority donation wont be sufficient to increase process Ls priority above process M. This is expected behavior (after all, process M would be more important in this case than process H).
Operating Systems 37
Lec 3
Lec 3
Operating Systems
38
Scheduling Details
Result approximates SRTF
CPU bound jobs drop rapidly to lower queues Short-running I/O bound jobs stay near the top
Lec 3
Operating Systems
39
Scheduling Details
It is apparent that scheduling is facilitated by having a good mix of I/O bound and CPU bound programs, so that there are long and short CPU bursts to prioritize around. There is typically a long-term and a short-term scheduler in the OS. We have been discussing the design of the short-term scheduler. The long-term scheduler decides what processes should be put into the ready queue in the first place for the short-term scheduler, so that the short-term scheduler can make fast decisions on a good mix of a subset of ready processes. The rest are held in memory or disk
Why else is this helpful?
Lec 3
Operating Systems
40
Scheduling Details
It is apparent that scheduling is facilitated by having a good mix of I/O bound and CPU bound programs, so that there are long and short CPU bursts to prioritize around. There is typically a long-term and a short-term scheduler in the OS. We have been discussing the design of the short-term scheduler. The long-term scheduler decides what processes should be put into the ready queue in the first place for the short-term scheduler, so that the short-term scheduler can make fast decisions on a good mix of a subset of ready processes. The rest are held in memory or disk
This also provides more free memory for the subset of ready processes given to the short-term scheduler.
Lec 3
Operating Systems
41
Fairness
What about fairness?
Strict fixed-policy scheduling between queues is unfair (run highest, then next, etc.)
Long running jobs may never get the CPU In Multics, admins shut down the machine and found a 10-year-old job
Must give long-running jobs a fraction of the CPU even when there are shorter jobs to run
Tradeoff: fairness gained by hurting average response time!
Could increase priority of jobs that dont get service (as seen in the multilevel feedback example)
This was done in UNIX Ad hoc with what rate should priorities be increased? As system gets overloaded, no job gets CPU time, so everyone increases in priority Interactive processes suffer Operating Systems 42
Lec 3
Lottery Scheduling
Yet another alternative: Lottery Scheduling
Give each job some number of lottery tickets On each time slice, randomly pick a winning ticket On average, CPU time is proportional to number of tickets given to each job over time
Lec 3
Operating Systems
43
What if there are too many short jobs to give reasonable response time
In UNIX, if load average is 100%, its hard to make progress Log a user out or swap a process out of the ready queue (long term scheduler)
Lec 3
Operating Systems
44
What if there are too many short jobs to give reasonable response time
In UNIX, if load average is 100%, its hard to make progress Log a user out or swap a process out of the ready queue (long term scheduler)
Lec 3
Operating Systems
45
Queuing Models
Mathematical Approach for handling stochastic workloads
Implementation / Simulation
Build system which allows actual algorithms to be run against actual data. Most flexible / general.
Lec 3
Operating Systems
46
Conclusion
Scheduling: selecting a waiting process from the ready queue and allocating the CPU to it When do the details of the scheduling policy and fairness really matter?
When there arent enough resources to go around
Most scheduling algorithms work fine in the linear portion of the load curve, and fail otherwise Argues for buying a faster X when utilization is at the knee of the curve
Lec 3 Operating Systems 47
RR scheduling:
Give each thread a small amount of CPU time when it executes, and cycle between all ready threads Better for short jobs, but poor when jobs are the same length
SJF/SRTF:
Run whatever job has the least amount of computation to do / least amount of remaining computation to do Optimal (average response time), but unfair; hard to predict the future
Lottery Scheduling:
Give each thread a number of tickets (short tasks get more) Every thread gets tickets to ensure forward progress / fairness
Priority Scheduing:
Preemptive or Nonpreemptive Priority Inversion
Lec 3 Operating Systems 48