Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Systems II Process Scheduling. 2 Review of Process States.

Similar presentations


Presentation on theme: "1 Computer Systems II Process Scheduling. 2 Review of Process States."— Presentation transcript:

1 1 Computer Systems II Process Scheduling

2 2 Review of Process States

3 3 CPU Scheduling n Question: Any process in the pool of ready processes is ready to run. Which one to pick to run next? n CPU scheduling -Selecting a new process to run from the Ready queue n Preemptive scheduling -Running process may be interrupted and moved to the Ready queue n Non-preemptive scheduling: -once a process is in Running state, it continues to execute until it terminates or blocks for I/O or system service

4 4 When Does a Scheduler Take Decisions? 1. Running - Blocked 2. Running - Terminate 3. Blocked - Ready 4. Running - Ready n Scheduling under 1 and 2: -nonpreemptive scheduling n Scheduling under 3 and 4: -premeptive scheduling

5 5 n Process Set: (numbers represent time units) n Processes requiring service: Motivating Example

6 6 What Are We Trying to Optimize? n System-oriented metrics: -CPU utilization: percentage of time the processor is busy -Throughput: number of processes completed per unit of time n User-oriented metrics: -Turnaround time: interval of time between submission and termination (including any waiting time). Appropriate for background jobs -Response time: for interactive jobs, time from the submission of a request until the response begins to be received -Missed time: sum of the periods spent in the Ready queue, while other processes run

7 7 Scheduling Criteria n Maximize -CPU utilization -Throughput n Minimize -Turnaround time -Missed time -Response time n Problem: mutually exclusive objectives -No one best way -Conflict between missed/response time and throughput

8 8 CPU-bound vs. I/O –bound Processes

9 9 Alternating CPU and I/O Bursts

10 10 Process Behavior n Observed property of processes -alternate between CPU execution and I/O wait n CPU-bound job: little I/O, long CPU bursts n I/O-bound job: lots of I/O, short CPU bursts n Problem: don’t know the bound type before running n An underlying assumption: -response time most important for I/O bound processes emacs Matrix multiplication

11 11 Long vs. Short Processes n Call a CPU-bound process a long process: - Spends long time on CPU computations, seldom waits for I/O n Call an I/O-bound process a short process: -Small amounts of computation are sandwiched between longer periods of waiting.

12 12 Scheduling Algorithms FCFS RR SPN / PSPN Priority

13 13 Scheduling Algorithms n Next we take a look at four scheduling algorithms and their amusing behavior 1. First Come First Served – FCFS 2. Round Robin – RR 3. Shortest Process Next – SPN, PSPN 4. Multi-Level Feedback (Priority) n Scheduling very ad hoc. “Try and See”

14 14 First Come First Served (FCFS or FIFO) n Simplest scheduling algorithm: -Run jobs in order that they arrive -Uniprogramming: run until done (non-preemptive) -Multiprogramming: put job at back of queue when blocks on I/O (we’ll assume this) n Advantage: simple n Disadvantages: ???

15 15 FCFS Example

16 16 Another FCFS Example Process Service Time P 1 100 P 2 2 P 3 1 n Suppose that processes arrive in the order: P 1, P 2, P 3 n FCFS Scheduling: n Missed time for P 1 = 0; P 2 = 100; P 3 = 102 n Average missed time: (0 + 100 + 102)/3 ~ 67 n Average turnaround time: (100+102+103)/3 ~ 101 P1P1 P2P2 P3P3 1001021030

17 17 What if … n … the processes arrive in the order P 2 (2), P 3 (1), P 1 (100) n The chart for the schedule is: n Missed time for P 1 = 3; P 2 = 0 ; P 3 = 2 n Average missed time: (3 + 0 + 2)/3 ~ 2 n Average turnaround time: (102+2+3)/3 ~ 36 n Long processes love FCFS; short processes hate it! P1P1 P3P3 P2P2 321030

18 18 FCFS Disadvantage Short processes may be stuck behind long processes

19 19 Round Robin (RR) n Solution to job monopolizing CPU? Interrupt it. -Run process for some “time quantum” (time slice) -When time is up or process blocks, move it to the back of the ready queue -Most systems do some flavor of this n Advantages: -Fair allocation of CPU across jobs -Low average waiting time when job lengths vary: -Assume P 1 (100), P 2 (2), P 3 (1) and time quantum = 1 -What is the average missed time and completion time? Add a timer 1 2 3 4 5 103 CPU P 2 P1P1 P3P3 P 1 P 2 P1P1

20 20 RR Example: Time Quantum = 1

21 21 RR Example: Time Quantum = 4

22 22 What if … n … the processes have similar sizes? Assume 2 processes of about 100 time units each: -What is the average missed time? -What is the average completion time? -How does it compare to FCFS for the same two processes? n Main interesting thing is the length of the time slice 1 2 3 4 5 199 200 CPU P2 P2 P1P1 P1P1 P2 P2 P1P1 P2 P2 P1P1 P1P1 P2 P2

23 23 Round Robin’s BIG Disadvantage Performance depends on the sizes of processes and the length of the time slice

24 24 RR Time Slice Tradeoffs (1) n Performance depends on the length of the timeslice n Context switching is not a free operation -If timeslice time is set too high (attempting to amortize context switch cost), you get FCFS (that is, processes will finish or block before their slice is up anyway) -If timeslice is set too low, all time is spent doing context switching between processes

25 25 RR Time Slice Tradeoffs (2) n Timeslice frequently set to ~100 milliseconds n Context switches typically cost < 1 millisecond -context switching is usually negligible (< 1% per timeslice in above example) unless you context switch too frequently and lose all productivity.

26 26 Shortest-Process-Next (SPN, PSPN) n Requires explicit information about the service-time requirements for each process. Use this information to schedule the process with the shortest time. n Nonpreemptive -Once CPU is given to a process, it cannot be preempted. n Preemptive -The PSPN preempts the current process when another process arrives, with a total service time requirement less than the remaining service time required by the current process.

27 27 SPN Example

28 28 Another SPN Example n 3 processes, P 1 (100), P 2 (2), P 3 (1) -Average completion = (1+3+103) / 3 = ~35 (vs ~101 for FCFS) n Provably optimal -Moving shorter process before longer process improves waiting time of short process more than harms missed time for long process CPU P1P1 P3P3 P2P2 1 3 103

29 29 PSPN Example ProcessArrival TimeBurst Time P 1 07 P 2 24 P 3 41 P 4 54 n STCF (preemptive) n Average missed time = (9 + 1 + 0 +2)/4 = 3 n Average completion time = ? P1P1 P3P3 P2P2 42 11 0 P4P4 57 P2P2 P1P1 16

30 30 PSPN is Optimal but Unfair n Gives minimum average missed time n Long jobs may starve if too many short jobs n Difficult to implement -how do you know how long a process takes ? n Solution -use the past to predict the future

31 31 Use the Past to Predict the Future … n Use the past to predict the future #1: -Long running job will probably take a long time more n Use the past to predict the future #2: -View job as sequence of alternating CPU and I/O jobs -If previous CPU pieces in the sequence have run quickly, future ones will too (usually). Priority must be given to interactive jobs. gcc sample pico

32 32 Use the Past to Predict the Future … n Example: Predict length of current CPU burst using length of previous burst -Record length of previous burst (0 when just created) -At scheduling event (unblock, block, exit, …) pick the smallest “past run length” off of Ready queue 91030 Pick 0 100 ms 91012100 9 time 9103100 Pick 12 ms 3 Pick

33 33 Approximate SPN – Exponential Average n ~SPN - exponential averaging n  = 0 -e n+1 = e n (recent history does not count) n  =1 - e n+1 = t n (only the actual last CPU burst counts) . t n nn e  e   1 1 :Define 4. 1, is a constant0, 3. burst CPU next the for value expected 2. burst CPU of length actual 1.     e n+1 th n nt

34 34 Exponential Average (2) n Expand the formula: e n+1 =  t n +(1 -  )  t n-1 + (1 -  ) 2  t n-2 … +(1 -  ) j  t n-j + … +(1 -  ) n+1 t 0 n Since both  and (1 -  ) are less than or equal to 1, each successive term has less weight than its predecessor (recent past counts more) .t nnn e  e   1 1

35 35 Multi-Level Feedback (FB) n Motivation -- look at PSPN: -Faster for a longer time slice (low switching overhead) -So grow the time slice for a long process, but decrease its priority to prevent starvation of others n Idea: Separate processes with different lengths (CPU bursts)

36 36 Multi-Level Feedback (FB) n Split the Ready list into a number of queues: queue 0, queue 1, queue 2, and so on. n Lower-numbered queues have higher priority. n When the current process is interrupted at the end of its quantum, a new process is selected from the front of the lowest-numbered queue that has any processes. n After a process has used a quanta in its queue, it is placed at the end of the next-higher numbered queue. n The word ‘‘feedback’’ in the name of this method refers to the fact that processes can move from one queue to another.

37 37 FB Example

38 38 FB Discussion n Attacks both efficiency and response time problems -Efficiency: long time quanta = low switching overhead -Response time: quickly run after becoming unblocked n Some problems -A user can insert I/O just to keep priority high. Can low priority processes starve? Solution: when skipped over, increase priority -What about when past does not predict future? For instance, a CPU bound (long) process switches to I/O bound (short). Solution: let past predictions “age” and count less towards current view of the world. Increase the priority of the process.

39 39 FB Variants n Let the quantum size depend on the queue. A queue numbered n could have a quantum of length 2 n q, where q is the ‘‘basic quantum’’ size. Therefore, the queues have quanta of sizes q, 2q, 4q, 8q, and so on. n Let a process in queue use several quanta before being demoted to the next queue n Promote a process to a higher-priority queue after it spends a certain amount of time waiting for service in its current queue. n Instead of granting absolute priority to low-numbered queues, grant slices of time to each queue, with lower-numbered queues receiving larger slices. n These variants can be used by themselves or in any combination.

40 40 FB Example with Doubling Quanta

41 41 Some problems n A user can insert I/O just to keep priority high n Can low priority processes starve? -ad hoc: when skipped over, increase priority n What about when past does not predict future? -For instance, a CPU bound process switches to I/O bound -want past predictions to “age” and count less towards current view of the world

42 42 FB Variants n Scheduler defined by the following parameters: -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 n Windows Operating System -32 priority levels -If a running process receives an interrupt, priority is lowered -If a process unblocks from a waiting queue, priority increases

43 43 Traditional UNIX Scheduling (1) n Uses multilevel feedback queues n 128 priorities possible (0-127) n One Round Robin queue per priority n At every scheduling event, the scheduler -picks the non-empty queue of highest priority -runs processes in round-robin n Scheduling events: -Clock interrupt -Process does a system call -Process gives up CPU (for instance, to do I/O)

44 44 Traditional UNIX Scheduling (2) n All processes assigned a baseline priority based on the type and current execution status: -swapper 0 -waiting for disk20 -waiting for lock35 -user-mode execution50 n At scheduling events, priorities are adjusted based on the amount of CPU used, the current load, and how long the process has been waiting

45 45 Linux - Lottery Scheduling n Problem: this whole priority thing is really ad-hoc -how to ensure that jobs will be equally penalized under load? n Lottery scheduling -give each process some number of tickets -at each scheduling event, randomly pick a ticket -run winning process -to give a process n% of CPU, give it total_tickets * n% n How to use? -approximate priority: low priority  give few tickets; high priority  give many tickets -approximates SRT: short processes  give more tickets; long processes  fewer tickets. If job has at least one ticket, it won’t starve

46 Summary n FCFS: -Advantage: simple -Disadvantage: short jobs can get stuck behind long ones n RR: -Advantage: better for short jobs -Disadvantage: poor when jobs are the same length n SPN / PSPN: -Advantage: optimal -Disadvantages: hard to predict the future; unfair to long running jobs n Multi-level feedback: -Advantage: approximates PSPN -Disadvantage: still a bit unfair to long running jobs


Download ppt "1 Computer Systems II Process Scheduling. 2 Review of Process States."

Similar presentations


Ads by Google