CS 444CS 544 Operating Systems Scheduling 1312007 Prof

  • Slides: 23
Download presentation
CS 444/CS 544 Operating Systems Scheduling 1/31/2007 Prof. Searleman jets@clarkson. edu

CS 444/CS 544 Operating Systems Scheduling 1/31/2007 Prof. Searleman jets@clarkson. edu

CS 444/CS 544 l Spring 2007 CPU Scheduling Reading assignment: l Chapter 5 HW#3:

CS 444/CS 544 l Spring 2007 CPU Scheduling Reading assignment: l Chapter 5 HW#3: done in lab 2 -2 -2007 HW#4 posted, due: 2 -7 -2007 Help for Lab 1, 6 -7 pm tonight in ITL/COSI

Benefits of Concurrency l Hide latency of blocking I/O without additional complexity l l

Benefits of Concurrency l Hide latency of blocking I/O without additional complexity l l Without concurrency l Block whole process l Manage complexity of asynchronous I/O (periodically checking to see if it is done so can finish processing) Ability to use multiple processors to accomplish the task Servers often use concurrency to work on multiple requests in parallel User Interfaces often designed to allow interface to be responsive to user input while servicing long operations

Scheduling l l l CPU or “short term” scheduler selects process from ready queue

Scheduling l l l CPU or “short term” scheduler selects process from ready queue (every 10 msec or so) “dispatcher” does the process switching “long-term” scheduler controls “degree of multiprogramming” (number of processes in memory); selects a good “job mix” – I/O-bound, CPU-bound, interactive, batch, high priority, background vs. foreground, real-time “non-preemptive” (cooperative) vs. “preemptive”

Performance Measures l l Throughput: #processes/time unit Turnaround time: time completed – time submitted

Performance Measures l l Throughput: #processes/time unit Turnaround time: time completed – time submitted Waiting time: sum of times spent in ready queue Response time: time from submission of a request until the first response is produced l l l Variation of response time (predictability) CPU utilization Disk (or other I/O device) utilization

I/O-bound & CPU-bound Device 1 P 1 CPU time quantum Device 2 P 2

I/O-bound & CPU-bound Device 1 P 1 CPU time quantum Device 2 P 2 CPU

I/O-bound & CPU-bound P 1: CPU-bound Device 1 idle CPU idle Turnaround time for

I/O-bound & CPU-bound P 1: CPU-bound Device 1 idle CPU idle Turnaround time for P 1

I/O-bound & CPU-bound P 2: I/O-bound Device 2 idle CPU idle Turnaround time for

I/O-bound & CPU-bound P 2: I/O-bound Device 2 idle CPU idle Turnaround time for P 2

I/O-bound & CPU-bound Schedule 1: non-preemptive, P 1 selected first Turnaround time for P

I/O-bound & CPU-bound Schedule 1: non-preemptive, P 1 selected first Turnaround time for P 1 Turnaround time for P 2 Without P 1

I/O-bound & CPU-bound Schedule 2: non-preemptive, P 2 selected first Turnaround time for P

I/O-bound & CPU-bound Schedule 2: non-preemptive, P 2 selected first Turnaround time for P 1 Turnaround time for P 2

I/O-bound & CPU-bound How does the OS know whether a process is I/O-bound or

I/O-bound & CPU-bound How does the OS know whether a process is I/O-bound or CPU-bound? - can monitor the behavior of a process & save the info in the PCB - example: how much CPU time did the process use in its recent time quanta? (a small fraction => I/O intensive; all of the quantum => CPU intensive) The nature of a typical process changes from I/Obound to CPU-bound and back as it works through its Input/Process/Output Cycle

Preemptive vs. Non-Preemptive t 0 ready: P 1, P 2 t 1 ready: P

Preemptive vs. Non-Preemptive t 0 ready: P 1, P 2 t 1 ready: P 2 blocked: P 1

Preemptive vs. Non-Preemptive: must continue to run P 1 at t 3 Preemptive: can

Preemptive vs. Non-Preemptive: must continue to run P 1 at t 3 Preemptive: can choose between P 1 & P 2 at t 3 t 2 ready: P 1 blocked: P 2 t 3 ready: P 2 running: P 1

New admit dispatch Ready (2) interrupt (3) I/O completed or event occurs (4) exit,

New admit dispatch Ready (2) interrupt (3) I/O completed or event occurs (4) exit, abort Terminated Running (1) block for I/O or wait for event Waiting • nonpreemptive (cooperative): (1) and (4) only • preemptive: otherwise

First Come First Serve (FCFS) l l l Also called First In First Out

First Come First Serve (FCFS) l l l Also called First In First Out (FIFO) Jobs scheduled in the order they arrive When used, tends to be non-preemptive l l l If you get there first, you get all the resource until you are done “Done” can mean end of CPU burst or completion of job Sounds fair l l All jobs treated equally No starvation (except for infinite loops that prevent completion of a job)

Process CPU burst P 1 18 FCFS Gantt chart P 2 2 P 3

Process CPU burst P 1 18 FCFS Gantt chart P 2 2 P 3 4 P 1 0 P 3 P 2 18 20 P 1, P 2, P 3 ready l average waiting time = (0 + 18 + 20)/3 = 12. 6 24

Problems with FCFS/FIFO l Can lead to poor overlap of I/O and CPU l

Problems with FCFS/FIFO l Can lead to poor overlap of I/O and CPU l l l If let first in line run till they are done or block for I/O then can get convoy effect While job with long CPU burst executes, other jobs complete their I/O and the I/O devices sit idle even though they are the “bottleneck” resource and should be kept as busy as possible Also, small jobs wait behind long running jobs (even grocery stores know that) l Results in high average turn-around time

Shortest Job First (SJF) l So if we don’t want short running jobs waiting

Shortest Job First (SJF) l So if we don’t want short running jobs waiting behind long running jobs, why don’t we let the job with the shortest CPU burst go next l l Can prove that this results in the minimum (optimal) average waiting time Can be preemptive or non-preemptive l Preemptive version called shortest-remaining-time first (SRTF)

Process CPU burst P 1 18 SJF Gantt chart 0 2 2 P 3

Process CPU burst P 1 18 SJF Gantt chart 0 2 2 P 3 4 P 1 P 3 P 2 6 P 1, P 2, P 3 ready l average waiting time = (0 + 2 + 6)/3 = 2. 6 24

SJF nonpreemptive Process Arrival time CPU burst P 1 P 2 P 3 P

SJF nonpreemptive Process Arrival time CPU burst P 1 P 2 P 3 P 4 7 4 1 4 P 1 0 2 P 1 ready 0 2 4 5 P 3 4 5 7 P 1 running P 2 , P 3 ready P 2 8 P 4 12 P 1 running P 2 , P 3 , P 4 ready P 1 running P 2 ready l average waiting time = (0 + 6 + 3 + 7)/4 = 4 16

SRTF preemptive P 2 P 1 0 2 P 1 ready P 1 running

SRTF preemptive P 2 P 1 0 2 P 1 ready P 1 running P 2 ready l CPU burst P 1 P 2 P 3 P 4 7 4 1 4 0 2 4 5 P 2 P 3 4 Process Arrival time 5 P 2 running P 1 , P 3 ready P 4 7 P 1 11 16 P 1 , P 4 ready P 3 completes P 1 , P 2 , P 4 ready average waiting time = (9 + 1 + 0 + 2)/4 = 3 P 1 ready

SRTF preemptive P 2 P 1 0 l l 2 Process turnaround Time waiting

SRTF preemptive P 2 P 1 0 l l 2 Process turnaround Time waiting Time P 1 P 2 P 3 P 4 16 – 0 = 16 7– 2=5 5– 4=1 11 – 5 = 6 9 1 0 P 2 P 3 4 5 P 4 7 2 P 1 11 average waiting time = (9 + 1 + 0 + 2)/4 = 3 average turnaround time = (16 + 5 + 1 + 6)/4 16

Problems with SJF l First, how do you know which job will have the

Problems with SJF l First, how do you know which job will have the shortest CPU burst or shortest running time? l l Can guess based on history but not guaranteed Bigger problem is that it can lead to starvation for long-running jobs l If you never got to the head of the grocery queue because someone with a few items was always cutting in front of you