Sie sind auf Seite 1von 29

Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling


Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Real Time Scheduling Thread Scheduling Operating Systems Examples Java Thread Scheduling Algorithm Evaluation

Operating System Concepts 7th Edition, Feb 2, 2005

5.2

Silberschatz, Galvin and Gagne 2005

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

choose? Does it make a difference?

Operating System Concepts 7th Edition, Feb 2, 2005

5.3

Silberschatz, Galvin and Gagne 2005

Alternating Sequence of CPU And I/O Bursts


Process execution consists of a cycle of CPU execution and I/O wait

Initial CPU burst + Zero or more CPU-I/O burst pairs + One final CPU burst

Operating System Concepts 7th Edition, Feb 2, 2005

5.4

Silberschatz, Galvin and Gagne 2005

Histogram of CPU CPU-burst Times

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

execute, and allocates the CPU to one of them


CPU scheduling decisions may take place when a process:

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

Allow the running process to continue Perform scheduling

non-preemptive preemptive

Operating System Concepts 7th Edition, Feb 2, 2005

5.6

Silberschatz, Galvin and Gagne 2005

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

Operating System Concepts 7th Edition, Feb 2, 2005

5.7

Silberschatz, Galvin and Gagne 2005

Dispatcher
Once a process is selected by the short-term scheduler, it is

(re)started ( ) by y the dispatcher p


z z z z z

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

process and start another running

Operating System Concepts 7th Edition, Feb 2, 2005

5.8

Silberschatz, Galvin and Gagne 2005

Dispatch Latency

Operating System Concepts 7th Edition, Feb 2, 2005

5.9

Silberschatz, Galvin and Gagne 2005

Scheduling Criteria
What measures to use to select a scheduling algorithm? CPU utilization % of time CPU is running user processes

To optimize good good

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

Operating System Concepts 7th Edition, Feb 2, 2005

5.10

Silberschatz, Galvin and Gagne 2005

FirstFirst -Come, First First-Served (FCFS) Scheduling


Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive at t=0 in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: P1 0 Process P1 P2 P3 Burst Time 24 3 3 24 Turnaround Time 24 27 30 (0 + 24 + 27)/3 = 17
5.11

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

Silberschatz, Galvin and Gagne 2005

FCFS Scheduling (Cont.)


Suppose that the processes arrive in the order P2 , P3 , P1
The Gantt chart for the schedule is:

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

ShortestShortest -JobJob-First (SJF) Scheduling


Associate with each process the length of its next CPU burst Schedule the process with the shortest next CPU burst Two versions:
z z

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


Known as Shortest-Remaining-Time-First (SRTF)

SJF is optimal
z

Gives minimum average waiting time for a given set of processes (why?)

Operating System Concepts 7th Edition, Feb 2, 2005

5.13

Silberschatz, Galvin and Gagne 2005

Example of Non Non-Preemptive SJF


Process P1 P2 P3 P4
SJF (non-preemptive)

Arrival Time 0.0 2.0 4.0 50 5.0

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

= ((0 0) + (8 - 2) + (7 - 4) + (12 - 5)) / 4 = (0 + 6 + 3 + 7) / 4 = 4


Operating System Concepts 7th Edition, Feb 2, 2005 5.14 Silberschatz, Galvin and Gagne 2005

Example of Preemptive SJF


Process P1 P2 P3 P4
SJF (preemptive)

Arrival Time 0.0 2.0 4.0 50 5.0

Burst Time 7 4 1 4

P1 0 2

P2 4

P3 5

P2 7

P4 11

P1 16

Average waiting time = Wait(P1) + Wait(P2) + Wait(P3) + Wait(P4)

= (11 - 2) + (5 4) + (4 4) + (7 - 5) = (9 + 1 + 0 +2) / 4 = 3

Operating System Concepts 7th Edition, Feb 2, 2005

5.15

Silberschatz, Galvin and Gagne 2005

Determining Length of Next CPU Burst


Can only estimate the length Can be done by y using g the length g of p previous CPU bursts, using g

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 .

Operating System Concepts 7th Edition, Feb 2, 2005

5.16

Silberschatz, Galvin and Gagne 2005

Prediction of the Length of the Next CPU Burst

Operating System Concepts 7th Edition, Feb 2, 2005

5.17

Silberschatz, Galvin and Gagne 2005

Examples of Exponential Averaging


=0

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

Since both and (1 - ) are less than or equal to 1, each

successive term has less weight than its predecessor

Operating System Concepts 7th Edition, Feb 2, 2005

5.18

Silberschatz, Galvin and Gagne 2005

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

Operating System Concepts 7th Edition, Feb 2, 2005

5.19

Silberschatz, Galvin and Gagne 2005

Round Robin (RR)


Designed for time-sharing systems Each process gets a small unit of CPU time q (time quantum),

usually 10-100 milliseconds milliseconds.


If quantum expires: preempt process, add to end of ready queue If process executes less than q: reschedule Ready queue is FIFO If there are n processes in the ready queue, each process gets

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

Operating System Concepts 7th Edition, Feb 2, 2005

5.20

Silberschatz, Galvin and Gagne 2005

Example of RR with Time Quantum = 20


Process P1 P2 P3 P4 The Gantt chart is: Burst Time 53 17 68 24

P1 0 20

P2 37

P3 57

P4 77

P1

P3 97 117

P4

P1

P3

P3

121 134 154 162

Typically, higher average turnaround than SJF, but better response

Operating System Concepts 7th Edition, Feb 2, 2005

5.21

Silberschatz, Galvin and Gagne 2005

Time Quantum and Context Switch Time

Smaller time quantum increases context switches.

Operating System Concepts 7th Edition, Feb 2, 2005

5.22

Silberschatz, Galvin and Gagne 2005

Turnaround Time Varies With The Time Quantum


Turnaround time varies with the time quantum.

Operating System Concepts 7th Edition, Feb 2, 2005

5.23

Silberschatz, Galvin and Gagne 2005

Multilevel Queue
Ready queue is partitioned into separate queues:

foreground (interactive) b k background d (b (batch) t h)


Each queue has its own scheduling algorithm
z z

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

Scheduling must be done between the queues


z z

Operating System Concepts 7th Edition, Feb 2, 2005

5.24

Silberschatz, Galvin and Gagne 2005

Multilevel Queue Scheduling

Operating System Concepts 7th Edition, Feb 2, 2005

5.25

Silberschatz, Galvin and Gagne 2005

Multilevel Feedback Queue


A process can move between the various queues; aging can be

implemented this way


Multilevel-feedback-queue scheduler defined by the following

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

Operating System Concepts 7th Edition, Feb 2, 2005

5.26

Silberschatz, Galvin and Gagne 2005

Example of Multilevel Feedback Queue


Three queues:
z z z

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

Operating System Concepts 7th Edition, Feb 2, 2005

5.27

Silberschatz, Galvin and Gagne 2005

Multilevel Feedback Queues

Operating System Concepts 7th Edition, Feb 2, 2005

5.28

Silberschatz, Galvin and Gagne 2005

End of Chapter 5

Das könnte Ihnen auch gefallen