Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.4810/EECE.5730 Operating Systems

Similar presentations


Presentation on theme: "EECE.4810/EECE.5730 Operating Systems"— Presentation transcript:

1 EECE.4810/EECE.5730 Operating Systems
Instructor: Dr. Michael Geiger Spring 2019 Lecture 18: Scheduling

2 Operating Systems: Lecture 18
Lecture outline Announcements/reminders Program 2 due 3/20 Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) Today’s lecture Review Deadlock Scheduling Scheduling metrics Scheduling algorithms 7/9/2019 Operating Systems: Lecture 18

3 Operating Systems: Lecture 18
Review: Deadlock Cyclical wait for resources, which prevents involved threads from making progress Necessary conditions Limited resource: not enough to serve all threads simultaneously Hold and wait: threads hold resources while waiting to acquire other resources No preemption: thread system can’t force one thread to give up resources Cyclical chain of requests 7/9/2019 Operating Systems: Lecture 18

4 Review: Deadlock prevention
Eliminate one of four necessary conditions Increase resources to decrease waiting Eliminate hold and wait: move resource acquisition to beginning If can’t (atomically) get all resources, release everything Phase 1a: acquire all resources Phase 1b: while (!done) {work} Phase 2: release all resources 7/9/2019 Operating Systems: Lecture 18

5 Review: Banker’s Algorithm
Similar to reserving all resources at beginning, but with more concurrency State maximum resource needs in advance (without acquiring) May block when thread attempts to acquire resource General structure Phase 1a: state maximum resource need Phase 1b: while (!done) { acquire some resource (block if not safe) work } Phase 2: release all resources 7/9/2019 Operating Systems: Lecture 18

6 Operating Systems: Lecture 18
Scheduling basics Multiprogramming maximizes CPU utilization Process execution is a cycle of CPU execution and I/O wait CPU burst followed by I/O burst CPU burst distribution is of main concern 7/9/2019 Operating Systems: Lecture 18

7 Operating Systems: Lecture 18
CPU Scheduler Short-term scheduler selects from among the processes in ready queue, and allocates the CPU to one of them Queue may be ordered in various ways CPU scheduling decisions may take place when a process: Switches from running to waiting state Switches from running to ready state Switches from waiting to ready Terminates Scheduling under 1 and 4 is nonpreemptive Currently running process “chooses” to give up CPU All other scheduling is preemptive Scheduler forces current process to give up CPU 7/9/2019 Operating Systems: Lecture 18

8 Operating Systems: Lecture 18
Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to restart that program Dispatch latency – time it takes for the dispatcher to stop one process and start another running 7/9/2019 Operating Systems: Lecture 18

9 Operating Systems: Lecture 18
Scheduling Criteria Several possible, often conflicting goals Want to maximize CPU utilization: keep CPU as busy as possible Throughput: rate at which processes complete per time unit Fairness: ensure CPU shared (relatively) equally Want to minimize Turnaround time: amount of time to execute a particular process, from arrival to completion (includes waiting time) Sometimes called latency or response time … … although our text defines response time as time to first “response” (output) from program, not completion Waiting time: amount of time a process has been waiting in the ready queue Starvation: Thread/process does not get access to resources Want to avoid, not just minimize! 7/9/2019 Operating Systems: Lecture 18

10 First-Come, First-Served (FCFS) Scheduling
Sometimes called first-in, first-out (FIFO) Schedule tasks in the order they arrive Continue running them until they complete or give up the processor—no preemptions Threads can call yield() or block Major benefit: simplest scheduling algorithm On what workloads is FCFS particularly bad? Always happens to me -- you go to the store to buy a carton of milk, you get stuck behind someone with a huge basket? And insists on paying in pennies. Feature -- gives you time to read the National Enquirer. Computer science professor has space alien's baby. 7/9/2019 Operating Systems: Lecture 18

11 FCFS Scheduling (continued)
Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive at time 0 in the order: P1 , P2 , P3 What is the waiting time for each process? What is the turnaround time for each process? What are the average waiting and turnaround times? 7/9/2019 Operating Systems: Lecture 18

12 FCFS Scheduling (continued)
Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive at time 0 in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: ( )/3 = 17 Average turnaround time: ( )/3 = 27 7/9/2019 Operating Systems: Lecture 18

13 FCFS Scheduling (continued)
Suppose that the same processes arrive at time 0 in the order: P2 , P3 , P1 What is the waiting time for each process? What is the turnaround time for each process? What are the average waiting and turnaround times? 7/9/2019 Operating Systems: Lecture 18

14 FCFS Scheduling (continued)
Suppose that the processes arrive at time 0 in the order: P2 , P3 , P1 The Gantt chart for the schedule is: Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: ( )/3 = 3 Average turnaround time: ( )/3 = 13 Much better than previous case Convoy effect - short process behind long process Consider one CPU-bound and many I/O-bound processes 7/9/2019 Operating Systems: Lecture 18

15 Shortest Job First (SJF)
Always do the task that has the shortest remaining amount of work to do Suppose we have five tasks arrive one right after each other, but the first one is much longer than the others Which completes first in FCFS? Next? Which completes first in SJF? Next? Need to be careful: optimal wrt average response time. Recall: only preemptive schedulers. 7/9/2019 Operating Systems: Lecture 18

16 SJF Scheduling (continued)
Associate with each process the length of its next CPU burst Use these lengths to schedule the process with the shortest time SJF is “optimal” – gives minimum average waiting time for a given set of processes Is SJF optimal by all metrics? Difficulty: knowing length of the next CPU burst 7/9/2019 Operating Systems: Lecture 18

17 Operating Systems: Lecture 18
FIFO vs. SJF Now you can see why SJF improves average response time – it runs short jobs first. Effect on the short jobs is huge; effect on the long job is small. 7/9/2019 Operating Systems: Lecture 18

18 Operating Systems: Lecture 18
SJF Example ProcessAr Time Burst Time P P P P What is the waiting time and turnaround time for each process? What are the average waiting and turnaround times? 7/9/2019 Operating Systems: Lecture 18

19 Operating Systems: Lecture 18
SJF Example ProcessAr Time Burst Time P P P P SJF scheduling chart Average waiting time = ( ) / 4 = 7 Average turnaround time = ( ) / 4 = 13 7/9/2019 Operating Systems: Lecture 18

20 Determining Length of Next CPU Burst
Can only estimate the length – should be similar to the previous one Then pick process with shortest predicted next CPU burst Can be done by using the length of previous CPU bursts, using exponential averaging Commonly, α set to ½ Preemptive version: shortest-remaining-time-first (SRTF) or shortest time to completion first (STCF) 7/9/2019 Operating Systems: Lecture 18

21 Operating Systems: Lecture 18
STCF example Now we add the concepts of varying arrival times and preemption to the analysis ProcessA arri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 What are the waiting and turnaround times for each process? What are the average waiting and turnaround times? 7/9/2019 Operating Systems: Lecture 18

22 Operating Systems: Lecture 18
STCF example Now we add the concepts of varying arrival times and preemption to the analysis ProcessA arri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF (STCF) Gantt Chart Average waiting time = [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5 Average turnaround time = [17 + (5-1) + (26-2) + (10 – 3)]/4 = 13 7/9/2019 Operating Systems: Lecture 18

23 Operating Systems: Lecture 18
Priority Scheduling Priority number (integer) associated with process Can be preemptive or non-preemptive CPU allocated to the process with the highest priority (smallest integer = highest priority) SJF is priority scheduling where priority is inverse of predicted next CPU burst time Potential problems? Solution? Problem: starvation for low-priority jobs Solution: aging  priority increases longer job stays in queue 7/9/2019 Operating Systems: Lecture 18

24 Priority Scheduling Example
ProcessA arri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Average waiting time = 8.2 Average turnaround time = 12 P2 P5 7/9/2019 Operating Systems: Lecture 18

25 Operating Systems: Lecture 18
Round Robin (RR) Each task gets resource for a fixed period of time (time quantum) On order of ms If task doesn’t complete, it goes back in line If n processes in ready queue and time quantum is q, each process gets 1/n of the CPU time in chunks of <= q time units at once. No process waits more than (n-1)q time units. Need to pick a time quantum What if time quantum is too long? Degenerates to FCFS What if time quantum is too short? Context switching overhead causes delays Want tradeoff—quantum long enough to dwarf switching time How can we impose priority in round robin? Add task to ready queue multiple times  multiple quanta Can we combine best of both worlds? RR approximates SJF by moving long tasks to the end of the line. 7/9/2019 Operating Systems: Lecture 18

26 Round Robin (continued)
Approximation depends on granularity 7/9/2019 Operating Systems: Lecture 18

27 Operating Systems: Lecture 18
RR example (quantum = 4) Process Burst Time P1 24 P2 3 P3 3 The Gantt chart is: Typically, higher average turnaround than SJF, but better response q should be large compared to context switch time q usually 10ms to 100ms, context switch < 10 usec 7/9/2019 Operating Systems: Lecture 18

28 Time Quantum and Context Switch Time
7/9/2019 Operating Systems: Lecture 18

29 Operating Systems: Lecture 18
Round Robin vs. FCFS Assuming zero-cost time slice, is Round Robin always better than FCFS? When would FCFS be better? Same-sized jobs Time-slicing for no reason Every single job finishes late! Show of hands! What’s the worst case for RR? Same sized jobs – then you are time-slicing for no purpose. Worse, this is nearly pessimal for average response time. 7/9/2019 Operating Systems: Lecture 18

30 Operating Systems: Lecture 18
Round Robin vs. FIFO Everything is fair, but average response time in this case is awful – everyone finishes very late! In fact, this case is exactly when FIFO is optimal, RR is poor. On the other hand, if we’re running streaming video, RR is great – everything happens in turn. SJF maximizes variance. But RR minimizes it. 7/9/2019 Operating Systems: Lecture 18

31 Operating Systems: Lecture 18
Example Consider following processes with the length of the CPU burst time given in milliseconds: Process Burst Priority P P P P P Assume processes arrive at same time, in order P1  P5 What is turnaround time of each process for: FCFS Round Robin (quantum = 1) SJF Non-preemptive priority 7/9/2019 Operating Systems: Lecture 18

32 Operating Systems: Lecture 18
Solution FCFS RR SJF Priority P1 1  10 1  19 10  19 7  16 P2 11 11 2  2 1  1 P3 12  13 3  7 3  4 17  18 P4 14  14 4  4 19  19 P5 15  19 5  14 5  9 2  6 Each entry shows start/end time under given scheduling algorithm Start at beginning of given time step, end at end of given time step So process with burst time of 1 (i.e., P2, P4) will appear to start and end in same “cycle” 7/9/2019 Operating Systems: Lecture 18

33 Operating Systems: Lecture 18
Example 2 Process Burst Priority Arrival time P1 10 1 P2 3 4 P3 7 2 P4 P5 5 6 Now consider processes with different arrival times Assume all times in ms; process can start 1 ms after it arrives (e.g., process arriving at time 0 can start at time 1) What is turnaround time of each process for: FCFS SJF STCF (remember, this scheme is preemptive!) RR (time quantum: 1 ms; assume ready queue ordered based on arrival order) Non-preemptive priority 7/9/2019 Operating Systems: Lecture 18

34 Operating Systems: Lecture 18
Example solution FCFS SJF STCF RR Priority Proc St End TT P1 1 10 17 26 P2 11 13 3 2 24 P3 14 20 18 4 8 9 23 21 15 P4 7 5 P5 22 12 16 19 7/9/2019 Operating Systems: Lecture 18

35 Operating Systems: Lecture 18
Example solution (2) Detailed STCF schedule: Time 4 5 Process (time) P2 (1-3) P3 (1) P4 (1) P3 (2-7) P5 (1-5) P1 (1-10) Detailed RR schedule: Time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Proc P1 P2 P3 P4 P5 17 18 19 20 21 22 23 24 25 26 7/9/2019 Operating Systems: Lecture 18

36 Operating Systems: Lecture 18
Real-time scheduling Focus thus far on average wait time/turnaround time Real-time systems require tasks to meet deadlines Video or audio output Control of physical systems Requires worst-case analysis How do we schedule for deadlines in life? 7/9/2019 Operating Systems: Lecture 18

37 Earliest Deadline First Scheduling (EDF)
Priorities assigned according to deadlines Preempt current job if new job arrives with earlier deadline Optimal: will meet all deadlines if possible 7/9/2019 Operating Systems: Lecture 18

38 Operating Systems: Lecture 18
Final notes Next time Memory management (likely starting Lec. 21) Reminders: Program 2 due 3/20 Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) 7/9/2019 Operating Systems: Lecture 18

39 Operating Systems: Lecture 18
Acknowledgements These slides are adapted from the following sources: Silberschatz, Galvin, & Gagne, Operating Systems Concepts, 9th edition Anderson & Dahlin, Operating Systems: Principles and Practice, 2nd edition Chen & Madhyastha, EECS 482 lecture notes, University of Michigan, Fall 2016 7/9/2019 Operating Systems: Lecture 18


Download ppt "EECE.4810/EECE.5730 Operating Systems"

Similar presentations


Ads by Google