Professional Documents
Culture Documents
Question: dispatcher can choose any thread on the ready queue to run;
how to decide and which to choose?
CPU Scheduling
n Scheduling algorithms
Arvind Krishnamurthy n FCFS (or FIFO)
n Priority scheduling
n Lottery scheduling
n Round Robin
n CPU burst distribution n CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.
memory, etc.) 0 24 27 30
n fair : share CPU among users in some equitable way n Waiting time for P1 = 0; P2 = 24; P3 = 27
n Average waiting time: (0 + 24 + 27)/3 = 17
1
FCFS scheduling (cont’d) Shortest-Job-First (SJF)
Suppose that the processes arrive in the order n Associate with each process the length of its next burst
P2 , P3 , P1 . n Use these lengths to schedule the process with the shortest time
n The Gantt chart for the schedule is:
n Two schemes:
n nonpreemptive – once given CPU it cannot be preempted until
P2 P3 P1
completes its CPU burst.
n preemptive – if a new process arrives with CPU burst length less
0 3 6 30
than remaining time of current executing process, preempt.
n Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
SJF is optimal but unfair
n
n
n Much better than previous case.
n pros: gives minimum average response time
n FCFS Pros: simple; Cons: short jobs get stuck behind long jobs n cons: long-running jobs may starve if too many short jobs;
n difficult to implement (how do you know how long it will take)
P1 P3 P2 P4 P1 P2 P3 P2 P4 P1
0 3 7 8 12 16 0 2 4 5 7 11 16
n T2 acquires lock L.
n SJF is a priority scheduling where priority is the predicted n Scene 1: T1 tries to acquire L, fails, spins. T2 never gets to run.
next CPU burst time. n Scene 2: T1 tries to acquire L, fails, blocks. T3 enters system at high
priority. T2 never gets to run.
n Problem: low priority processes may never execute. n “Priority donation”
n Solution: aging – as time progresses increase the priority of the n Thread’s priority scales w/ priority of dependent threads
process. n Works well with explicit dependencies
2
Lottery Scheduling Lottery Scheduling Example
n Problem: this whole priority thing is really ad hoc. n Add or delete jobs (& their tickets) affects all jobs proportionately
n How to ensure that processes will be equally penalized under load? short job: 10 tickets + long job: 1 ticket
That system doesn’t have a bad screw case?
n Lottery scheduling #short jobs/ % of CPU each % of CPU each
n give each process some number of tickets #long jobs short job gets long job gets
n each scheduling event, randomly pick ticket 1/1 91% 9%
n run winning process 0/2 NA 50%
n to give P n% of CPU, give it ntickets * n% 2/0 50% NA
10 / 1 10% 1%
n How to use?
1 / 10 50% 5%
n Approximate priority: low-priority, give few tickets, high-priority give
many
n Easy priority inversion:
n Approximate SJF: give short jobs more tickets, long jobs fewer. If job n Donate tickets to process you’re waiting on.
has at least 1, will not starve
n Its CPU% scales with tickets of all waiters.
n q too small ⇒ throughput suffers. Spend all your time context n Typically, higher average turnaround than SJF, but better
switching, not getting any real work done response.
3
Multilevel feedback queue Example of Multilevel Feedback
n Three queues:
n Q0 – quantum 8 ms
n Q1 – quantum 16 ms
n Q2 – FCFS
n Scheduling
n Multilevel-feedback-queue scheduler defined by the n A new job enters queue Q0 which is served FCFS
following parameters: n When it gains CPU, job receives 8 milliseconds
n number of queues, scheduling algorithms for each queue, when to
upgrade/downgrade a process, which queue a process will enter n If it does not finish in 8 milliseconds, job is moved to queue Q1
* Note – the JVM does not specify whether threads are time-
sliced or not.