Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 7-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 7 Scheduling.

Similar presentations


Presentation on theme: "Slide 7-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 7 Scheduling."— Presentation transcript:

1 Slide 7-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 7 Scheduling

2 Slide 7-2 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Model of Process Execution Ready List Ready List Scheduler CPU Resource Manager Resource Manager Resources Preemption or voluntary yield AllocateRequest Done New Process job “Ready” “Running” “Blocked”

3 Slide 7-3 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Scheduler as CPU Resource Manager Scheduler Process Units of time for a time-multiplexed CPU ReleaseReady to run Dispatch Release Ready List

4 Slide 7-4 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 The Scheduler Ready Process Enqueuer Ready List Ready List Dispatcher Context Switcher Context Switcher Process Descriptor Process Descriptor CPU From Other States Running Process

5 Slide 7-5 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Process/Thread Context R1 R2 Rn... Status Registers Functional Unit Left Operand Right Operand Result ALU PC IR Ctl Unit

6 Slide 7-6 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Context Switching CPU New Thread Descriptor Old Thread Descriptor

7 Slide 7-7 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Invoking the Scheduler Need a mechanism to call the scheduler Voluntary call –Process blocks itself –Calls the scheduler Involuntary call –External force (interrupt) blocks the process –Calls the scheduler

8 Slide 7-8 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Voluntary CPU Sharing yield(p i.pc, p j.pc) { memory[p i.pc] = PC; PC = memory[p j.pc]; } p i can be “automatically” determined from the processor status registers yield(*, p j.pc) { memory[p i.pc] = PC; PC = memory[p j.pc]; }

9 Slide 7-9 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 More on Yield yield(*, p j.pc);... yield(*, p i.pc);... yield(*, p j.pc);... p i and p j can resume one another’s execution Suppose p j is the scheduler: // p_i yields to scheduler yield(*, p j.pc); // scheduler chooses p k yield(*, p k.pc); // p k yields to scheduler yield(*, p j.pc); // scheduler chooses...

10 Slide 7-10 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Voluntary Sharing Every process periodically yields to the scheduler Relies on correct process behavior –Malicious –Accidental Need a mechanism to override running process

11 Slide 7-11 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Involuntary CPU Sharing Interval timer –Device to produce a periodic interrupt –Programmable period IntervalTimer() { InterruptCount--; if(InterruptCount <= 0) { InterruptRequest = TRUE; InterruptCount = K; } SetInterval(programmableValue) { K = programmableValue: InterruptCount = K; }

12 Slide 7-12 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Involuntary CPU Sharing (cont) Interval timer device handler –Keeps an in-memory clock up-to-date (see Chap 4 lab exercise) –Invokes the scheduler IntervalTimerHandler() { Time++; // update the clock TimeToSchedule--; if(TimeToSchedule <= 0) { ; TimeToSchedule = TimeSlice; }

13 Slide 7-13 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Contemporary Scheduling Involuntary CPU sharing – timer interrupts –Time quantum determined by interval timer – usually fixed size for every process using the system –Sometimes called the time slice length

14 Slide 7-14 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Choosing a Process to Run Mechanism never changes Strategy = policy the dispatcher uses to select a process from the ready list Different policies for different requirements Ready Process Enqueue Ready List Ready List Dispatch Context Switch Context Switch Process Descriptor Process Descriptor CPU Running Process

15 Slide 7-15 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Policy Considerations Policy can control/influence: –CPU utilization –Average time a process waits for service –Average amount of time to complete a job Could strive for any of: –Equitability –Favor very short or long jobs –Meet priority requirements –Meet deadlines

16 Slide 7-16 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Optimal Scheduling Suppose the scheduler knows each process p i ’s service time,  p i  -- or it can estimate each  p i  : Policy can optimize on any criteria, e.g., –CPU utilization –Waiting time –Deadline To find an optimal schedule: –Have a finite, fixed # of p i –Know  p i  for each p i –Enumerate all schedules, then choose the best

17 Slide 7-17 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 However... The  (p i ) are almost certainly just estimates General algorithm to choose optimal schedule is O(n 2 ) Other processes may arrive while these processes are being serviced Usually, optimal schedule is only a theoretical benchmark – scheduling policies try to approximate an optimal schedule

18 Slide 7-18 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Model of Process Execution Ready List Ready List Scheduler CPU Resource Manager Resource Manager Resources Preemption or voluntary yield AllocateRequest Done New Process job “Ready” “Running” “Blocked”

19 Slide 7-19 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Talking About Scheduling... Let P = {p i | 0  i < n} = set of processes Let S(p i )  {running, ready, blocked} Let  (p i ) = Time process needs to be in running state (the service time) Let W(p i ) = Time p i is in ready state before first transition to running (wait time) Let T TRnd (p i ) = Time from p i first enter ready to last exit ready (turnaround time) Batch Throughput rate = inverse of avg T TRnd Timesharing response time = W(p i )

20 Slide 7-20 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Simplified Model Ready List Ready List Scheduler CPU Resource Manager Resource Manager Resources AllocateRequest Done New Process job “Ready” “Running” “Blocked” Simplified, but still provide analysis result Easy to analyze performance No issue of voluntary/involuntary sharing Preemption or voluntary yield

21 Slide 7-21 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Estimating CPU Utilization Ready List Ready List Scheduler CPU Done New Process System p i per second Each p i uses 1/  units of the CPU Let = the average rate at which processes are placed in the Ready List, arrival rate Let  = the average service rate  1/  = the average  (p i )

22 Slide 7-22 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Estimating CPU Utilization Ready List Ready List Scheduler CPU Done New Process Let = the average rate at which processes are placed in the Ready List, arrival rate Let  = the average service rate  1/  = the average  (p i ) Let  = the fraction of the time that the CPU is expected to be busy  = # p i that arrive per unit time * avg time each spends on CPU  = * 1/  = /  Notice must have <  (i.e.,  < 1) What if  approaches 1?

23 Slide 7-23 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Nonpreemptive Schedulers Ready List Ready List Scheduler CPU Done New Process Try to use the simplified scheduling model Only consider running and ready states Ignores time in blocked state: –“New process created when it enters ready state” –“Process is destroyed when it enters blocked state” –Really just looking at “small phases” of a process Blocked or preempted processes

24 Slide 7-24 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 First-Come-First-Served i  (p i ) p0p0 T TRnd (p 0 ) =  (p 0 ) = 350 W(p 0 ) =

25 Slide 7-25 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 First-Come-First-Served i  (p i ) p0p0 p1p1 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = = 475 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) =

26 Slide 7-26 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 First-Come-First-Served i  (p i ) p0p0 p1p1 p2p2 p3p3 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = = 475 T TRnd (p 2 ) = (  (p 2 ) +T TRnd (p 1 )) = = 950 T TRnd (p 3 ) = (  (p 3 ) +T TRnd (p 2 )) = = 1200 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) = 350 W(p 2 ) = T TRnd (p 1 ) = 475 W(p 3 ) = T TRnd (p 2 ) =

27 Slide 7-27 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 FCFS Average Wait Time i  (p i ) p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = = 475 T TRnd (p 2 ) = (  (p 2 ) +T TRnd (p 1 )) = = 950 T TRnd (p 3 ) = (  (p 3 ) +T TRnd (p 2 )) = = 1200 T TRnd (p 4 ) = (  (p 4 ) +T TRnd (p 3 )) = = 1275 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) = 350 W(p 2 ) = T TRnd (p 1 ) = 475 W(p 3 ) = T TRnd (p 2 ) = 950 W(p 4 ) = T TRnd (p 3 ) = 1200 W avg = ( )/5 = 2974/5 = Easy to implement Ignores service time, etc Not a great performer

28 Slide 7-28 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Predicting Wait Time in FCFS In FCFS, when a process arrives, all in ready list will be processed before this job Let  be the service rate Let L be the ready list length W avg (p) = L*1/  1/  L  Compare predicted wait with actual in earlier examples

29 Slide 7-29 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Shortest Job Next i  (p i ) p4p4 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 4 ) = 0 750

30 Slide 7-30 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Shortest Job Next i  (p i ) p1p1 p4p4 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = = 200 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 1 ) = 75 W(p 4 ) =

31 Slide 7-31 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Shortest Job Next i  (p i ) p1p1 p3p3 p4p4 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = = 200 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 1 ) = 75 W(p 3 ) = 200 W(p 4 ) =

32 Slide 7-32 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Shortest Job Next i  (p i ) p0p0 p1p1 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = = 800 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = = 200 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 0 ) = 450 W(p 1 ) = 75 W(p 3 ) = 200 W(p 4 ) =

33 Slide 7-33 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Shortest Job Next i  (p i ) p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = = 800 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = = 200 T TRnd (p 2 ) =  (p 2 )+  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = = 1275 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 0 ) = 450 W(p 1 ) = 75 W(p 2 ) = 800 W(p 3 ) = 200 W(p 4 ) =

34 Slide 7-34 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Shortest Job Next i  (p i ) p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = = 800 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = = 200 T TRnd (p 2 ) =  (p 2 )+  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = = 1275 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 0 ) = 450 W(p 1 ) = 75 W(p 2 ) = 800 W(p 3 ) = 200 W(p 4 ) = 0 W avg = ( )/5 = 1525/5 = Minimizes wait time May starve large jobs Must know service times

35 Slide 7-35 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Priority Scheduling i  (p i ) Pri p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 4 )+  (p 2 )+  (p 1 ) )+  (p 3 ) = = 1275 T TRnd (p 1 ) =  (p 1 )+  (p 3 ) = = 375 T TRnd (p 2 ) =  (p 2 )+  (p 1 )+  (p 3 ) = = 850 T TRnd (p 3 ) =  (p 3 ) = 250 T TRnd (p 4 ) =  (p 4 )+  (p 2 )+  (p 1 )+  (p 3 ) = = 925 W(p 0 ) = 925 W(p 1 ) = 250 W(p 2 ) = 375 W(p 3 ) = 0 W(p 4 ) = 850 W avg = ( )/5 = 2400/5 = Reflects importance of external use May cause starvation Can address starvation with aging

36 Slide 7-36 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Deadline Scheduling i  (p i ) Deadline (none) p0p0 p1p1 p2p2 p3p3 p4p Allocates service by deadline May not be feasible p0p0 p1p1 p2p2 p3p3 p4p4 p0p0 p1p1 p2p2 p3p3 p4p4 575

37 Slide 7-37 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Preemptive Schedulers Ready List Ready List Scheduler CPU Preemption or voluntary yield Done New Process Highest priority process is guaranteed to be running at all times –Or at least at the beginning of a time slice Dominant form of contemporary scheduling But complex to build & analyze

38 Slide 7-38 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Round Robin (TQ=50) i  (p i ) p0p0 W(p 0 ) = 0 050

39 Slide 7-39 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Round Robin (TQ=50) i  (p i ) p0p0 W(p 0 ) = 0 W(p 1 ) = p1p1

40 Slide 7-40 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Round Robin (TQ=50) i  (p i ) p0p0 T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = p4p4 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3

41 Slide 7-41 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Round Robin (TQ=50) i  (p i ) p0p0 T TRnd (p 0 ) =  T TRnd (p 1 ) =  T TRnd (p 2 ) =  T TRnd (p 3 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 W avg = ( )/5 = 500/5 = Equitable Most widely-used Fits naturally with interval timer p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p2p2 p0p0 p2p2 p2p2 p2p2 p2p T TRnd _ avg = ( )/5 = 4350/5 = 870

42 Slide 7-42 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 RR with Overhead=10 (TQ=50) i  (p i ) p0p0 T TRnd (p 0 ) =  T TRnd (p 1 ) =  T TRnd (p 2 ) =  T TRnd (p 3 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 60 W(p 2 ) = 120 W(p 3 ) = 180 W(p 4 ) = 240 W avg = ( )/5 = 600/5 = Overhead must be considered p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p2p2 p0p0 p2p2 p2p2 p2p2 p2p T TRnd _ avg = ( )/5 = 5220/5 =

43 Slide 7-43 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Multi-Level Queues Ready List 0 Ready List 1 Ready List 2 Ready List 3 Scheduler CPU Preemption or voluntary yield Done New Process All processes at level i run before any process at level j At a level, use another policy, e.g. RR

44 Slide 7-44 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Contemporary Scheduling Involuntary CPU sharing -- timer interrupts –Time quantum determined by interval timer -- usually fixed for every process using the system –Sometimes called the time slice length Priority-based process (job) selection –Select the highest priority process –Priority reflects policy With preemption Usually a variant of Multi-Level Queues

45 Slide 7-45 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Bank Teller Simulation Tellers at the Bank T1T1 T1T1 T2T2 T2T2 TnTn TnTn … Model of Tellers at the Bank Customers Arrivals

46 Slide 7-46 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 Simulation Kernel Loop(2) void runKernel(int quitTime) { Event *thisEvent; // Stop by running to elapsed time, or by causing quit execute if(quitTime next; simTime = thisEvent->getTime(); // Set the time // Execute this event thisEvent->fire(); delete(thisEvent); }; }


Download ppt "Slide 7-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 7 Scheduling."

Similar presentations


Ads by Google