CPU Scheduling Chapter 6 Advanced Operating System.

40
CPU Scheduling Chapter 6 Advanced Operating System

Transcript of CPU Scheduling Chapter 6 Advanced Operating System.

Page 1: CPU Scheduling Chapter 6 Advanced Operating System.

CPU Scheduling

Chapter 6 Advanced Operating System

Page 2: CPU Scheduling Chapter 6 Advanced Operating System.

2

CPU Scheduling

What is CPU Scheduling? Determining which processes run

when there are multiple run able processes

Why is it important? It can have a big effect on resource

utilization and the overall performance of the system

Page 3: CPU Scheduling Chapter 6 Advanced Operating System.

3

CPU Scheduling Long-term scheduling

Deals with creating a new process Controls degree of multiprogramming

May be FCFS or priority based Interactive systems tend to accept users unless the

system is swamped Medium-term scheduling

Deals with swapping processes in/out Short-term scheduling

What process should we run next? Invoked on:

clock or I/O interrupt system call, signal

Page 4: CPU Scheduling Chapter 6 Advanced Operating System.

4

Short-term Criteria Criteria

Response Time – Start to first output Important in interactive systems

Turnaround Time – Start to finish Deadlines Predictability – Time doesn’t depend on what else is

going on Throughput – Jobs finished/unit time Processor Utilization Fairness – Processes treated equally Enforcing Priorities Balancing Resources – Try to keep all system

resources busy

Page 5: CPU Scheduling Chapter 6 Advanced Operating System.

5

How do processes behave? CPU/IO burst cycle

A process will run for a while (the CPU burst), perform some IO (the IO burst), then run for a while more (the

next CPU burst) IO Bound processes

P rocesses that perform lots of IO operations. Each IO operation is followed by a short CPU burst to p

rocess the IO, then more IO happens CPU bound processes

Processes that perform lots of computation and do little IO .

Tend to have a few long CPU bursts

Page 6: CPU Scheduling Chapter 6 Advanced Operating System.

6

How do processes behave? Scheduler will typically do

switch the CPU to another process when one process does IO

The IO will take a long time, and don't want to leave the CPU idle while wait for the IO to finish

What are possible process states? Running - process is running on CPU - Ready ready to run, but not actually running on the C

PU Waiting - waiting for some event like IO to happen .

Page 7: CPU Scheduling Chapter 6 Advanced Operating System.

7

Scheduling Priorities

Are some processes more important than others? Don’t want to starve low-priority processes

Decision Mode Will we suspend the currently active process if it can

continue? No: Nonpreemptive Yes: Preemptive Some systems (Win 3.1, early Mac) used cooperative

multitasking (processes voluntarily give up the CPU) Preemption incurs more O.S. overhead, but prevents

monopolizing the processor Also helps with infinite loops

Page 8: CPU Scheduling Chapter 6 Advanced Operating System.

8

How to evaluate scheduling algorithm? CPU Utilization :

Keep CPU utilization as high as possible. Throughput:

number of processes completed per unit time. Turnaround Time :

mean time from submission to completion of process . Waiting Time:

Amount of time spent ready to run but not running. Response Time :

Time between submission of requests and first response to the request .

Scheduler Efficiency: The scheduler doesn't perform any useful work, so any time

it takes is pure overhead. So, need to make the scheduler v ery efficient.

Page 9: CPU Scheduling Chapter 6 Advanced Operating System.

9

First Come First Served

Processes queued in order of arrival Runs until finished or blocks on I/O Tends to penalize short processes

Have to wait for earlier long processes Favors processor-bound processes

I/O processes block quickly

Page 10: CPU Scheduling Chapter 6 Advanced Operating System.

10

First Come First ServedProcess Burst Time

P1 24 P2 3 P3 3

Suppose that the processes arrive 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: (0 + 24 + 27)/3 = 17

P1 P2 P3

24 27 300

Page 11: CPU Scheduling Chapter 6 Advanced Operating System.

11

First Come First ServedSuppose that the processes arrive 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: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process

P1P3P2

63 300

Page 12: CPU Scheduling Chapter 6 Advanced Operating System.

12

Shortest-Job-First (SJF) Shortest-Job-First (SJF ) can

eliminate some of the variance in Waiting and Turnaround time .

It is optimal with respect to average waiting time .

Big problem : how does scheduler figure out how long will it take the process to run?

Page 13: CPU Scheduling Chapter 6 Advanced Operating System.

13

Shortest-Job-First (SJF) 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 Two schemes:

nonpreemptive preemptive

Page 14: CPU Scheduling Chapter 6 Advanced Operating System.

14

Shortest-Job-First (SJF) Preemptive scheduler

Reruns scheduling decision when process becomes ready .

if a new process arrives with CPU burst length less than remaining time of current executing process, preempt.

If the new process has priority over running process, the CPU preempts the running process and executes the new process .

Non-preemptive scheduler Only does scheduling decision when running process

voluntarily gives up CPU . once CPU given to the process it cannot be

preempted until completes its CPU burst It allows every running process to finish its CPU burst .

Page 15: CPU Scheduling Chapter 6 Advanced Operating System.

15

Shortest-Job-First (SJF) Shortest Process Next

Select process with shortest expected running time (nonpreemptive)

Difficult to estimate required time Can estimate from previous runs

Tends to be less predictable Can starve long processes Short processes may still wait if a long process has

just started Shortest Remaining Time

Preemptive version of Shortest Process Next May switch processes when a new process arrives Still may starve long processes

Page 16: CPU Scheduling Chapter 6 Advanced Operating System.

16

Non-preemptive Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4 SJF (non-preemptive)

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

P1 P3 P2

73 160

P4

8 12

Page 17: CPU Scheduling Chapter 6 Advanced Operating System.

17

Preemptive Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4 SJF (preemptive)

Average waiting time = (9 + 1 + 0 +2)/4 = 3

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 18: CPU Scheduling Chapter 6 Advanced Operating System.

18

Priority Scheduling Each process is given a priority, then

CPU executes process with highest priority .

If multiple processes with same priority are runnable, use some other criteria - typically FCFS .

SJF is an example of a priority-based scheduling algorithm .

The priorities of a given process change over time .

Page 19: CPU Scheduling Chapter 6 Advanced Operating System.

19

Priority Scheduling Assume we have 5 processes

P1 (burst time 10, priority 3) P2 (burst time 1, priority 1) P3 (burst time 2, priority 3) P4 (burst time 1, priority 4) P5 (burst time 5, priority 2 )

Lower numbers represent higher priorities . What would a standard priority scheduler

do?

Page 20: CPU Scheduling Chapter 6 Advanced Operating System.

20

Priority Scheduling

Big problem with priority scheduling algorithms : starvation or blocking of low-priority processes .

Can use aging to prevent this - make the priority of a process go up the longer it stays runnable but isn't run .

Page 21: CPU Scheduling Chapter 6 Advanced Operating System.

21

Priority Scheduling What about interactive systems?

Cannot just let any process run on the CPU until it gives it up - must give response to users in a reasonable time .

Use an algorithm called round-robin scheduling .Similar to FCFS but with preemption .

Have a time quantum or time slice . Let the first process in the queue run until it expires its quantum (i.e . runs for as long as the time quantum), then run the next process in the queue .

Page 22: CPU Scheduling Chapter 6 Advanced Operating System.

22

Round Robin (RR) Each process gets a small unit of CPU time (time

quantum), usually 10-100 milliseconds. After this time has elapsed, the process is

preempted and added to the end of the ready queue.

If there are n processes in the ready queue Time quantum is q, Each process gets 1/n of the CPU time in chunks of at

most q time units at once. No process waits more than (n-1)q time units.

Performance q large FIFO q small q must be large with respect to context

switch, otherwise overhead is too high

Page 23: CPU Scheduling Chapter 6 Advanced Operating System.

23

Round Robin (RR)queue

dispatchprocessor

At timeout, process switching occurs.

At timeout, process switching occurs.

Each process is allowed to execute for at most a quantum.

Each process is allowed to execute for at most a quantum.

Page 24: CPU Scheduling Chapter 6 Advanced Operating System.

24

Round Robin (RR) Process Burst Time

P1 53P2 17P3 68P4 24

The Gantt chart is:

Typically, higher average turnaround than SJF, but better response

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 25: CPU Scheduling Chapter 6 Advanced Operating System.

25

Other Algorithms Highest Response Ratio Next

Nonpreemptive, tries to get best average normalized turnaround time

Depends on Response Ratio W = time spent waiting S = expected service time RR = (W + S) / S

Select process with highest RR Feedback

Starts in high-priority queue, moves down in priority as it executes

Lower-priority queues often given longer time slices Can starve long processes

Move up processes if they wait too long

Page 26: CPU Scheduling Chapter 6 Advanced Operating System.

26

Comparison P1: arrives at time 0,

requires 3 units P2: arrives at time 2,

requires 6 units P3: arrives at time 4,

requires 4 units P4: arrives at time 6,

requires 5 units P5: arrives at time 8,

requires 2 units

Page 27: CPU Scheduling Chapter 6 Advanced Operating System.

27

Comparison Turnaround Time

Finish Time-Arrival time

Normalized Turnaround Time

Turnaround Time/Service Time

P1 P2 P3 P4 P5 MeanArrival 0 2 4 6 8

Serv Time 3 6 4 5 2FCFS

Finish 3 9 13 18 20Turn. 3 7 9 12 12 8.60

Norm T. 1.00 1.17 2.25 2.40 6.00 2.56RR (q=1)

Finish 4 18 17 20 15Turn. 4 16 13 14 7 10.80

Norm T. 1.33 2.67 3.25 2.80 3.50 2.71RR (q=4)

Finish 3 17 11 20 19Turn. 3 15 7 14 11 10.00

Norm T. 1.00 2.50 1.75 2.80 5.50 2.71SPN

Finish 3 9 15 20 11Turn. 3 7 11 14 3 7.60

Norm T. 1.00 1.17 2.75 2.80 1.50 1.84SRT

Finish 3 15 8 20 10Turn. 3 13 4 14 2 7.20

Norm T. 1.00 2.17 1.00 2.80 1.00 1.59HRRN

Finish 3 9 13 20 15Turn. 3 7 9 14 7 8.00

Norm T. 1.00 1.17 2.25 2.80 3.50 2.14Feedback (q=1)

Finish 4 20 16 19 11Turn. 4 18 12 13 3 10.00

Norm T. 1.33 3.00 3.00 2.60 1.50 2.29Feedback ( q=2^(i-1) )

Finish 4 17 18 20 14Turn. 4 15 14 14 6 10.60

Norm T. 1.33 2.50 3.50 2.80 3.00 2.63

Page 28: CPU Scheduling Chapter 6 Advanced Operating System.

28

Response Time If the system gets too slow to a user, they may

slow down or abort the operation Must balance response time with the cost

required Faster/more expensive hardware may be required Priorities that may penalize certain processes

Ranges 15 seconds or greater

Rules out conversational interaction. Most users will not wait this long If it cannot be avoided, allow the user to continue on to

something else Foreground/background threads

Page 29: CPU Scheduling Chapter 6 Advanced Operating System.

29

Response Time 4 to 15 seconds

Generally user loses track of the current operation in short-term memory

Ok after end of a major closure 2 to 4 seconds

Inhibits user concentration Bothers a user who is focused on the job Ok after end of a minor closure

1 to 2 seconds Important when information has to be remembered over several

responses Important limit for terminal activities

Less than 1 second For keeping user’s attention for thought-intensive activities Example: graphics

Less than 1/10 second Response to key presses or mouse clicks

Page 30: CPU Scheduling Chapter 6 Advanced Operating System.

30

Response Time User productivity tends to improve with a more rapid

response time Especially true for expert users Becomes very noticeable as response time drops below 1

second User time – “think time”, time user spends thinking

about the response System time – Time system takes to generate its

response Short response times are important User response time tends to decrease as system response

time decreases Web Pages – Loading a page in 3 seconds or less

increases the user’s attention User may abort after 10s or more

Page 31: CPU Scheduling Chapter 6 Advanced Operating System.

31

Multiprocessor Scheduling

Brief Review Loosely Coupled (cluster)

Separate main memory and I/O devices Functionally Specialized

Dedicated I/O processors Tightly Coupled

Share main memory, O.S.

Page 32: CPU Scheduling Chapter 6 Advanced Operating System.

32

Design Issues Assigning processes to processors

Are processes/threads assigned to processors? Who handles the assignment?

Master/Slave Single processor handles O.S. functions Tends to become a bottleneck

Peer O.S. can run on any processor More complicated operating system

Generally use simple schemes Overhead is a greater problem Threads add additional concerns

Generally response time is more important than CPU utilization

Page 33: CPU Scheduling Chapter 6 Advanced Operating System.

33

Process Scheduling Response time is much less sensitive to the algorithm

used Thread Scheduling

Load Sharing Gang Scheduling Dedicated Processor Assignment Dynamic scheduling

Load Sharing Select threads from a global queue Avoids idle processors Queue may be a bottleneck Doesn’t take advantage of local cache May not schedule interacting threads at the same time Widely used

Page 34: CPU Scheduling Chapter 6 Advanced Operating System.

34

Process Scheduling Gang Scheduling

Schedule related threads on processors to run at the same time

Less overhead since we schedule multiple processors at once

Interacting threads are more likely to be running and ready to interact

Have to allocate processors Dedicated Processor Assignment

Like gang scheduling, but never switch between applications

Best to have # threads = # processors Dynamic Scheduling

Vary the number of threads Job uses the processors in its partition Appears better than gang scheduling

Page 35: CPU Scheduling Chapter 6 Advanced Operating System.

35

Real-time Scheduling

Very common in embedded systems Require results be produced before

specified deadlines Hard real-time: missed deadlines result

in damage or death Soft real-time: missed deadlines may

result in lower performance, but can be tolerated

Most real-time systems are soft real-time

Page 36: CPU Scheduling Chapter 6 Advanced Operating System.

36

Real-Time Systems Features

Fast process/thread switch Small size/minimal functionality Ability to respond to interrupts quickly Multitasking with IPC tools like semaphores, signals, and

events Use of special sequential files that can accumulate data at

a fast rate Preemptive scheduling with priority Minimize time with interrupts off Primitives to delay tasks for a fixed amount of time,

pause/resume tasks Special alarms and timeouts

Short-term scheduler is critical Need to have priorities, preemption

Page 37: CPU Scheduling Chapter 6 Advanced Operating System.

37

Deadline Scheduling Importance is meeting deadlines, not mere speed in

handling events Use more task information:

Ready Time (when it is ready to start) Starting Deadline (begin by …) Completion Deadline (finish by …) Processing Time (time to execute) Resource Requirements Priority (relative importance) Subtask structure

Parts of a task may be optional Parts of a task may have lower priority

Generally best to schedule task with the earliest deadline If we don’t allow preemption, we may need to let the CPU

sit idle and wait for a task to arrive

Page 38: CPU Scheduling Chapter 6 Advanced Operating System.

38

Deadline Scheduling Execution profile of two periodic

tasks Process A

Arrives 0 2040 …

Execution Time 10 1010 …

End by 2040 60…

Process B Arrives 0 50

100 … Execution Time 25 25

25 … End by 50

100150 …

Scheduling of two periodic tasks

Page 39: CPU Scheduling Chapter 6 Advanced Operating System.

39

Deadline Scheduling Execution profile

of five aperiodic tasks

Scheduling of aperiodic tasks (no preemption)

Process

Arrival Time

Execution

Time

Starting Deadlin

e

A 10 20 110

B 20 20 20

C 40 20 50

D 50 20 90

E 60 20 70

Page 40: CPU Scheduling Chapter 6 Advanced Operating System.

40

Deadline Scheduling