Beruflich Dokumente
Kultur Dokumente
5.2
Basic Concepts
Remember the benefit of multiprogramming?
z z
We have several processes in memory When one blocks for I/O we can switch the CPU to another ready process
Attempts p to maximize CPU utilization Question: If we have many ready-to-run processes, which one to
5.3
Initial CPU burst + Zero or more CPU-I/O burst pairs + One final CPU burst
5.4
For scheduling we will consider the length of the next CPU burst Length of CPU bursts varies (of course) Typically a few long bursts, many short ones
Operating System Concepts 7th Edition, Feb 2, 2005 5.5 Silberschatz, Galvin and Gagne 2005
CPU Scheduler
Selects from among the processes in memory that are ready to
1. Switches from running to waiting state (e.g. system call, I/O) 2. S Switches tc es from o running u g to ready eady state (e (e.g. g interrupt) te upt) 3. Switches from waiting to ready (e.g. I/O completes) 4. Terminates
We W MUST perform f scheduling h d li under d (1) and d (4) Under (2) and (3) we can:
z z
non-preemptive preemptive
5.6
CPU Scheduler
Non-preemptive means process continues until termination or I/O Preemptive means that the process can be forced off the CPU
(enabled in time time-shared shared systems as clock interrupts interrupts, time slices)
z
Preemption could lead to complications: what if the process was updating a data shared with another process? E.g.,
Process P1 is in middle of updating pdating a variable, ariable shared with ith process P2 If P1 is preempted; P2 starts t t executing ti P2 accesses the shared variable, which is in inconsistent state
Non-preemptive OS examples: Windows 3.x, MacOS before OS X Preemptive OS examples: Windows 95 and later, Solaris, Linux
5.7
Dispatcher
Once a process is selected by the short-term scheduler, it is
saves state of currently running process (to its PCB) loads state of selected process (from its PCB) starts timer (if necessary) switches to user mode
context switch
jumps to the proper location in the selected process to restart it ( (resume it)
The dispatch latency is time it takes for the dispatcher to stop one
5.8
Dispatch Latency
5.9
Scheduling Criteria
What measures to use to select a scheduling algorithm? CPU utilization % of time CPU is running user processes
On heavily-loaded computers want utilization 90%-100% Throughput # of processes that complete their execution per time unit z depends on process length z short processes: maybe 10 per second Turnaround time lifespan of process z Time from process submission to completion z Includes execution time, I/O time, waiting on ready queue Waiting time time spent on in the ready queue Response time time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) z Important I t t for f interactive i t ti processes
z
good
good g good
5.10
P2 27
P3 30 Waiting Time 0 24 27
Average A waiting i i time: i W Wait(P i (P1) + W Wait i (P2) + Wait W i (P3) / 3 = Average turnaround time: (24+ 27 + 30)/3 = 27
Operating System Concepts 7th Edition, Feb 2, 2005
P2 0 3
P3 6
P1 30
Waiting time for P1 = 6; P2 = 0; P3 = 3 Turnaround T d ti time f for P1 = 30; P2 = 3; P3 = 6 Average waiting time: (6 + 0 + 3)/3 = 3 Average turnaround time: (30 + 3 + 6)/3 = 13 Much better than previous case Convoy effect delays short process behind long process
For example p 1 CPU-bound, many y I/O-bound p processes z Results in lower CPU and device utilization than letting shorter processes execute first.
z
Operating System Concepts 7th Edition, Feb 2, 2005 5.12 Silberschatz, Galvin and Gagne 2005
Non-preemptive once CPU given to the process it cannot be preempted t d until til completes l t it its CPU b burst t preemptive if a new process arrives with CPU burst length less than remaining time of current executing process, preempt
SJF is optimal
z
Gives minimum average waiting time for a given set of processes (why?)
5.13
Burst Time 7 4 1 4
P1 0 3 7
P3 8
P2 12
P4 16
Arrival Time should be considered -> Wait(Pi) = StartTime(Pi) - ArrivalTime(Pi) Average waiting time = Wait(P1) + Wait (P2) + Wait (P3) + Wait (P4) / 4
Burst Time 7 4 1 4
P1 0 2
P2 4
P3 5
P2 7
P4 11
P1 16
= (11 - 2) + (5 4) + (4 4) + (7 - 5) = (9 + 1 + 0 +2) / 4 = 3
5.15
exponential averaging
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 4. D fi :
n =1 = t n + (1 ) n .
5.16
5.17
n+1 = n z Recent history does not count =1 z n n+1 1 = tn z Only the actual last CPU burst counts If we expand the formula, we get: n+1 = tn+(1 (1 - ) tn -1 1+ +(1 - )j tn -j + +(1 - )n +1 0
z
5.18
Priority Scheduling
A priority number is associated with each process Process with the highest priority is selected Priority: usually integer In UNIX and textbook, smallest integer highest priority Can be:
Preemptive z Non-preemptive SJF is a special case of priority scheduling. Why? z Priority is the predicted next CPU burst time z Larger the CPU burst the lower the priority Problem: starvation (indefinite blocking) z It is possible that a low priority process never executes Solution: aging z As time progresses, progresses increase the priority of the process
z
5.19
1/n of the CPU time in chunks of at most q time units at once. N process waits No it more th than (n-1) 1)q time ti units. it
If q= then RR becomes what scheduling algorithm?
z z
q FCFS q small q must be large with respect to context switch, otherwise overhead is too high
5.20
P1 0 20
P2 37
P3 57
P4 77
P1
P3 97 117
P4
P1
P3
P3
5.21
5.22
5.23
Multilevel Queue
Ready queue is partitioned into separate queues:
foreground RR background FCFS Fixed priority scheduling; (i (i.e., e serve all from foreground then from background). Possibility of starvation. Time slice each queue gets a certain amount of CPU time g its p processes; ; i.e., , 80% to which it can schedule amongst foreground in RR 20% to background in FCFS
5.24
5.25
parameters:
z z z z z
number of queues scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service
5.26
Q0 RR with time q quantum 8 milliseconds Q1 RR time quantum 16 milliseconds Q2 FCFS A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds job is moved to queue Q1. milliseconds, At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to q queue Q2.
Scheduling
z
5.27
5.28
End of Chapter 5