05 Scheduling
05 Scheduling
Shortest-Job-First (SJR)
FSFC Scheduling (cont.)
Scheduling
Suppose that the processes arrive in the order ■ Associate with each process the length of its
P2 , P3 , P1 . next CPU burst. Use these lengths to
■ The Gantt chart for the schedule is: schedule the process with the shortest time.
■ Two schemes:
P2 P3 P1 ■ nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst.
0 3 6 30 ■ preemptive – if a new process arrives with CPU
■ Waiting time for P1 = 6; P2 = 0; P3 = 3 burst length less than remaining time of current
■ Average waiting time: (6 + 0 + 3)/3 = 3 executing process, preempt. This scheme is know
■ Much better than previous case. as the
■ Convoy effect short process behind long process Shortest-Remaining-Time-First (SRTF).
■ Favors CPU-bound processes: I/O processes have ■ SJF is optimal – gives minimum average
to wait waiting time for a given set of processes.
Example of Non-Preemptive SJF Example of Preemptive SJF
Process Arrival Time Burst Time Process Arrival Time Burst Time
P1 0.0 7 P1 0.0 7
P2 2.0 4 P2 2.0 4
P3 4.0 1 P3 4.0 1
P4 5.0 4 P4 5.0 4
■ SJF (non-preemptive) ■ SJF (preemptive)
P1 P3 P2 P4
P1 P2 P3 P2 P4 P1
0 3 7 8 12 16
■ Average waiting time = (0 + 6 + 3 + 7)/4 - 4 ■
0 2
Average 4 5
waiting
11 16
time7 = (9 + 1 + 0 +2)/4 - 3
Examples of Exponential
Priority Scheduling
Averaging
■ α =0 ■ A priority number (integer) is associated with each
■ τn+1 = τn
process
■ Recent history does not count.
■ The CPU is allocated to the process with the
■ α =1 highest priority (smallest integer ≡ highest
■ τn+1 = tn
priority).
■ Only the actual last CPU burst counts.
■ Preemptive
■ If we expand the formula, we get: ■ nonpreemptive
τn+1 = α tn+(1 - α)α tn -1 + …
+(1 - α )j α tn -j + …
■ SJF is a priority scheduling where priority is the
+(1 - α )n +1 τ0 predicted next CPU burst time.
■ Since both α and (1 - α) are less than or ■ Problem ≡ Starvation – low priority processes may
equal to 1, each successive term has less never execute.
weight than its predecessor. ■ Solution ≡ Aging – as time progresses increase the
priority of the process.
Example of RR with Time
Round Robin (RR)
Quantum = 20
■ Each process gets a small unit of CPU time (time
Process Burst Time
quantum), usually 10-100 milliseconds. After this P1 53
time has elapsed, the process is preempted and P2 17
added to the end of the ready queue. P3 68
■ If there are n processes in the ready queue and P4 24
■ The Gantt chart is:
the time quantum is q, then each process gets 1/n
of the CPU time in chunks of at most q time units P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
at once. No process waits more than (n-1)q time
0 20 37 57 77 97 117 121 134 154 162
units. ■ Typically, higher average turnaround than SJF, but better
■ Performance response.
q should be large compared to context switch time
■ q large ⇒ FIFO
■
process
■ method used to determine when to demote a
process
■ method used to determine which queue a process
■ A new process enters queue Q0 which is served in RR ■ Multiprocess may be any one of the
■ When it gains CPU, the process receives 8 milliseconds following architectures:
■ If it does not finish in 8 milliseconds, the process is moved to queue Q1
■ At Q1 job is again served in RR and receives 16 additional milliseconds ■ Multicore CPUs
■ If it still does not complete, it is preempted and moved to queue Q2
■ Multithreaded cores
■ Heterogeneous multiprocessing
■ Two levels of scheduling: ■ If SMP, need to keep all CPUs loaded for
efficiency
■ The operating system
deciding which software ■ Load balancing attempts to keep workload
thread to run on a logical evenly distributed
CPU
■ Push migration – periodic task checks load on
each processor, and if found pushes task from
■ How each core decides
which hardware thread to overloaded CPU to other CPUs
run on the physical core. ■ Pull migration – idle processors pulls waiting
task from busy processor
■ When a thread has been running on one processor, the ■ Hard real-time systems – required to
cache contents of that processor stores the memory
accesses by that thread. complete a critical task within a guaranteed
■ A thread having affinity for a processor (i.e. amount of time.
“processor affinity”) ■ Soft real-time computing – requires that
■ Load balancing may affect processor affinity as a thread may critical processes receive priority over less
be moved from one processor to another to balance loads,
yet that thread loses the contents of what it had in the fortunate ones.
cache of the processor it was moved off of.
■ Soft affinity – the operating system attempts to keep a
thread running on the same processor, but no guarantees.
■ Hard affinity – allows a process to specify a set of
processors it may run on.
■ 32-level priority scheme ■ Task run-able as long as time left in time slice (active)
■ If no time left (expired), not run-able until all other tasks use their
■ Variable class is 1-15, real-time class is 16-31 slices
■ Priority 0 is memory-management thread ■ All run-able tasks tracked in per-CPU runqueue data structure
■ Queue for each priority ■ Two priority arrays (active, expired)
Tasks indexed by priority
■ If no run-able thread, runs idle thread ■