Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 153 Design of Operating Systems Spring 2015 Lecture 10: Scheduling.

Similar presentations


Presentation on theme: "CS 153 Design of Operating Systems Spring 2015 Lecture 10: Scheduling."— Presentation transcript:

1 CS 153 Design of Operating Systems Spring 2015 Lecture 10: Scheduling

2 Announcements l Project 1 due Friday u TAs will send out instructions for how to submit l Mid-term coming up soon u 4/27 and 4/29 review, 5/1 exam 2

3 3 Scheduling Overview l Scheduler runs when we context switching among processes/threads on the ready queue u What should it do? Does it matter? l Making this decision is called scheduling l Now, we’ll look at: u The goals of scheduling u Starvation u Various well-known scheduling algorithms u Standard Unix scheduling algorithm

4 4 Multiprogramming l In a multiprogramming system, we try to increase CPU utilization and job throughput by overlapping I/O and CPU activities u Doing this requires a combination of mechanisms and policy l We have covered the mechanisms u Context switching, how and when it happens u Process queues and process states l Now we’ll look at the policies u Which process (thread) to run, for how long, etc. l We’ll refer to schedulable entities as jobs (standard usage) – could be processes, threads, people, etc.

5 5 Scheduling Goals l Scheduling works at two levels in an operating system 1. To determine the multiprogramming level – the number of jobs loaded into primary memory »Moving jobs to/from memory is often called swapping »Long term scheduler: infrequent 2. To decide what job to run next to guarantee “good service” »Good service could be one of many different criteria »Short term scheduler: frequent »We are concerned with this level of scheduling »Is scheduler a thread always running in kernel space?

6 6 Scheduling l The scheduler (aka dispatcher) is the module that manipulates the queues, moving jobs to and from them l The scheduling algorithm determines which jobs are chosen to run next and what queues they wait on l In general, the scheduler runs: u When a job switches from running to waiting u When an interrupt occurs u When a job is created or terminated l The scheduler runs inside the kernel (for kernel-level threads). Therefore, kernel has to be entered before scheduler can run.

7 Preemptive vs. Non- preemptive scheduling l We’ll discuss scheduling algorithms in two contexts u In preemptive systems the scheduler can interrupt a running job (involuntary context switch) u In non-preemptive systems, the scheduler waits for a running job to explicitly block (voluntary context switch) 7

8 8 Scheduling Goals l What are some reasonable goals for a scheduler? l Scheduling algorithms can have many different goals: u CPU utilization u Job throughput (# jobs/unit time) u Turnaround time (T finish – T start ) u Waiting time (Avg(T wait ): avg time spent on wait queues) u Response time (Avg(T ready ): avg time spent on ready queue) l Batch systems u Strive for job throughput, turnaround time (supercomputers) l Interactive systems u Strive to minimize response time for interactive jobs (PC)

9 9 Starvation Starvation is a scheduling “non-goal”: l Starvation is a situation where a process is prevented from making progress because some other process has the resource it requires u Resource could be the CPU, or a lock (recall readers/writers) l Starvation usually a side effect of the sched. algorithm u A high priority process always prevents a low priority process from running on the CPU u One thread always beats another when acquiring a lock l Starvation can be a side effect of synchronization u Constant supply of readers always blocks out writers

10 First In First Out (FIFO) l Schedule tasks in the order they arrive u Continue running them until they complete or give up the processor l Example: many cases in real life l On what workloads is FIFO particularly bad? u Imagine being at supermarket to buy a drink of water, but get stuck behind someone with a huge cart (or two!) »…and who pays in pennies! u Can we do better? 10

11 Shortest Job First (SJF) l Always do the task that has the shortest remaining amount of work to do u Often called Shortest Remaining Time First (SRTF) l Suppose we have five tasks arrive one right after each other, but the first one is much longer than the others u Which completes first in FIFO? Next? u Which completes first in SJF? Next? 11

12 FIFO vs. SJF Whats the big deal? Don’t they finish at the same time? 12

13 13 SJF Example – Turnaround Time ATT = (8 + (8+4)+(8+4+2))/3 = 11.33 ATT = (4 + (4+8)+(4+8+2))/3 = 10 ATT = (4+ (4+2)+(4+2+8))/3 = 8 ATT = (2 + (2+4)+(2+4+8))/3 = 7.33

14 14 SJF Example – Response Time ART = (0 + 8 + (8+4))/3 = 6.67 ART = (0 + 4 + (4+8))/3 = 5.33 ART = (0 + 4 + (4+2))/3 = 3.33 ART = (0 + 2 + (2+4))/3 = 2.67

15 SJF l Claim: SJF is optimal for average response time u Why? l For what workloads is FIFO optimal? u For what is it pessimal (i.e., worst)? l Does SJF have any downsides? u Does it work in a supermarket? 15

16 16 Shortest Job First (SJF) l Problems? u Impossible to know size of CPU burst »Like choosing person in line without looking inside basket/cart u How can you make a reasonable guess? u Can potentially starve l Flavors u Can be either preemptive or non-preemptive u Preemptive SJF is called shortest remaining time first (SRTF)

17 Round Robin l Each task gets resource for a fixed period of time (time quantum) u If task doesn’t complete, it goes back in line l Need to pick a time quantum u What if time quantum is too long? »Infinite? u What if time quantum is too short? »One instruction? 17

18 Round Robin 18

19 Round Robin vs. FIFO l Many context switches can be costly l Other than that, is Round Robin always better than FIFO? 19

20 Round Robin vs. FIFO Is Round Robin always fair? 20

21 Mixed Workload 21

22 Max-Min Fairness l How do we balance a mixture of repeating tasks: u Some I/O bound, need only a little CPU u Some compute bound, can use as much CPU as they are assigned l One approach: maximize the minimum allocation given to a task u Schedule the smallest task first, then split the remaining time using max-min 22

23 23 Priority Scheduling l Priority Scheduling u Choose next job based on priority »Airline checkin for first class passengers u Can implement SJF, priority = 1/(expected CPU burst) u Also can be either preemptive or non-preemptive l Problem? u Starvation – low priority jobs can wait indefinitely l Solution u “Age” processes »Increase priority as a function of waiting time »Decrease priority as a function of CPU consumption

24 24 More on Priority Scheduling l For real-time (predictable) systems, priority is often used to isolate a process from those with lower priority. Priority inversion is a risk unless all resources are jointly scheduled. x->Acquire() x->Release() x->Acquire() priority time priority time How can this be avoided? PHPH PLPL PHPH PLPL PMPM

25 Priority Inversion on Mars Pathfinder l P H = (Frequent) Bus Management l P M = (Long-Running) Communications l P L = (Infrequent and short) Data Gathering 25 x->Acquire() x->Release() priority time PHPH PLPL PMPM Killed by watchdog

26 26 Combining Algorithms l Scheduling algorithms can be combined u Have multiple queues u Use a different algorithm for each queue u Move processes among queues l Example: Multiple-level feedback queues (MLFQ) u Multiple queues representing different job types »Interactive, CPU-bound, batch, system, etc. u Queues have priorities, jobs on same queue scheduled RR u Jobs can move among queues based upon execution history »Feedback: Switch from interactive to CPU-bound behavior

27 27 Scheduling Summary l Scheduler (dispatcher) is the module (not a thread) that gets invoked when a context switch needs to happen l Scheduling algorithm determines which process runs, where processes are placed on queues l Many potential goals of scheduling algorithms u Utilization, throughput, wait time, response time, etc. l Various algorithms to meet these goals u FCFS/FIFO, SJF, Priority, RR l Can combine algorithms u Multiple-level feedback queues

28 28 Next class l Deadlock u Read Chapter 7 in book


Download ppt "CS 153 Design of Operating Systems Spring 2015 Lecture 10: Scheduling."

Similar presentations


Ads by Google