copyright ©: nahrstedt, angrave, abdelzaher1 scheduling

27
Copyright ©: Nahrstedt, Angrave, Abdelzaher 1 Scheduling

Upload: carmella-simon

Post on 13-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher 1

Scheduling

Page 2: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

2

Content of This Lecture Why CPU scheduling? Basic scheduling algorithms

FIFO (FCFS) Shortest job first Round Robin Priority Scheduling

Goals: Understand how your program is

executed on the machine together with other programs

Page 3: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

3

Review: State Process Model

Page 4: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

4

OS Representation of Process via Process Control Block (PCB)

Page 5: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

5

Process Scheduling

Deciding which process/thread should occupy the resource (CPU, disk, etc)

CPU

Process 1 Process 2 Process 3

I want to play Whose turn is it?

Page 6: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

6

Context Switch

Switch CPU from one process to another Performed by scheduler It includes:

save PCB state of the old process; load PCB state of the new process; Flush memory cache; Change memory mapping (TLB);

Context switch is expensive (1-1000 microseconds) No useful work is done (pure overhead) Can become a bottleneck

Need hardware support

Page 7: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

7

When should the scheduler be called?

A new process is admitted The running process exits The running process is blocked I/O interrupt (some processes will be

ready) Clock interrupt (every 10 milliseconds)

Page 8: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

8

Preemptive vs. Non-preemptive scheduling

Non-preemptive scheduling: The running process keeps the CPU

until it voluntarily gives up the CPU process exits switches to blocked state Transition 3 is only voluntary

Preemptive scheduling: The running process can be

interrupted and must release the CPU (can be forced to give up CPU)

Running Terminated

Ready Blocked

1

4

3

Page 9: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

9

Scheduling Objectives

Fairness (equitable shares of CPU) Priority (highest priority first) Efficiency (make best use of equipment) Encouraging good behavior (can’t take

advantage of the system) Support for heavy loads (degrade gracefully) Adapting to different environments

(interactive, real-time, multi-media)

Page 10: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

10

Performance Criteria

Efficiency: keep resources as busy as possible Throughput: # of processes that complete in unit time Waiting Time

Total amount of time spent by the process waiting in ready queue

Initial Waiting Time Amount of time spent by the process waiting in ready queue

before it starts executing Response Time

amount of time from when a job is admitted until it completes Proportionality:

Assign CPU proportionally to given application weight Meeting Deadlines:

example, complete by 5pm Dec. 15th 2009!

Page 11: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

11

Process Profiles

I/O – Bound Does too much I/O to keep CPU busy

CPU – Bound Does too much computation to keep I/O busy

Process Mix Scheduling should load balance between I/O bound

and CPU-bound processes Ideal would be to run all equipment at 100%

utilization but that would not necessarily be good for response time

Page 12: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

12

Simple Processor Scheduling Algorithms

Batch systems First Come First Serve (FCFS) Shortest Job First

Interactive Systems Round Robin Priority Scheduling …

Page 13: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

13

First Come First Serve (FCFS)

Process that requests the CPU FIRST is allocated the CPU FIRST.

Also called FIFO Used in Batch Systems Implementation

FIFO queue A new process enters the tail of the queue The scheduler selects the process at the head of the queue.

Quiz: Is it Preemptive or Non-preemptive?

Page 14: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

14

First Come First Serve (FCFS)

Process that requests the CPU FIRST is allocated the CPU FIRST.

Also called FIFO Used in Batch Systems Implementation

FIFO queue A new process enters the tail of the queue The scheduler selects the process at the head of the queue.

Quiz: Is it Preemptive or Non-preemptive? once a process has CPU, it runs to completion (non-

preemptive)!

Page 15: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

15

FCFS Example

Process Duration Order Arrival Time

P1 24 1 0

P2 3 2 0

P3 4 3 0The final schedule:

0

P1 (24)

24 27

P2 (3) P3 (4)

P1 waiting time: 0P2 waiting time: 24P3 waiting time: 27

The average waiting time: (0+24+27)/3 = 17

Does the execution order affect the average waiting time?

Page 16: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

16

Problems with FCFS

Non-preemptive Does not minimize AWT Cannot utilize resources in parallel:

Assume 1 process CPU bounded and many I/O bounded processes

result: Convoy effect, low CPU and I/O Device utilization

Why?

Page 17: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

17

Why Convoy Effects with FCFS?

Consider n-1 jobs in system that are I/O bound and 1 job that is CPU bound:

1. I/O bound jobs pass quickly through the ready queue and suspend themselves waiting for I/O.

2. CPU bound job arrives at head of queue and executes until completion.

3. I/O bound jobs re-join ready queue and wait for CPU bound job to complete.

4. I/O devices idle until CPU bound job completes. 5. When CPU bound job completes, the ready I/O-

bounded processes quickly move through the running state and become blocked on I/O events again. CPU becomes idle.

Page 18: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

18

Interactive Scheduling Algorithms

Usually preemptive Time is sliced into quanta (time intervals) Scheduling decision is also made at the beginning of each

quantum Performance Criteria

Average response time Average initial waiting time Average waiting time Fairness (or proportional resource allocation)

Representative algorithms: Round-robin Priority-based …

Page 19: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

19

Round-robin

One of the oldest, simplest, most commonly used scheduling algorithm

Select process/thread from ready queue in a round-robin fashion (take turns)

Problems:• Does not consider priority• Context switch overhead

Preemption after quantum expiration

Page 20: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

20

Round-robin: Example

Process Duration Order Arrival Time

P1 3 1 0

P2 4 2 0

P3 3 3 0

0

Suppose time quantum is: 1 unit, P1, P2 & P3 never block

P1 P2 P3

10

P1 P2 P3 P1 P2 P3 P2

P1 waiting time: 4P2 waiting time: 6P3 waiting time: 6

The average waiting time (AWT): (4+6+6)/3 = 5.33

Note that Initial Waiting Time of P1 is zero, P2’s IWT is 1, and P3’s IWT is 2

Page 21: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

21

Choosing the Time Quantum

Time slice too large FIFO behavior Poor initial waiting time

Time slice too small Too many context switches (overheads) Inefficient CPU utilization

Heuristic: 70-80% of jobs block within time-slice

Typical time-slice 10-100 ms (depends on job priority)

Page 22: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

22

Shortest Job First (SJF)

Schedule the job with the shortest computation time first

Scheduling in Batch Systems Two types:

Non-preemptive Preemptive

Optimal if all jobs are available simultaneously: Gives the best possible AWT (average waiting time)

Page 23: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

23

Non-preemptive SJF: Example

Process Duration Order Arrival Time

P1 6 1 0

P2 8 2 0

P3 7 3 0

P4 3 4 0

0 3

P4 (3) P1 (6)

9

P3 (7)

16

P4 waiting time: 0P1 waiting time: 3P3 waiting time: 9P2 waiting time: 16

The total time is: 24The average waiting time (AWT): (0+3+9+16)/4 = 7

P2 (8)

24

Page 24: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

24

Comparing to FCFS

Process Duration Order Arrival Time

P1 6 1 0

P2 8 2 0

P3 7 3 0

P4 3 4 0

Page 25: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

25

Comparing to FCFS

Process Duration Order Arrival Time

P1 6 1 0

P2 8 2 0

P3 7 3 0

P4 3 4 0

0 6

P4 (3)P1 (6)

14

P3 (7)

21

P1 waiting time: 0P2 waiting time: 6P3 waiting time: 14P4 waiting time: 21

The total time is the same.The average waiting time (AWT): (0+6+14+21)/4 = 10.25(comparing to 7)

P2 (8)

24

Page 26: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

26

Preemptive SJF

Shortest job runs first. A job that arrives and is shorter than

the running job will preempt it

Page 27: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher

27

A Problem with Preemptive SJF

Starvation A job may keep getting preempted by shorter ones Example

Process A with computation time of 1 hour arrives at time 0 But every 1 minute, a short process with computation time

of 2 minutes arrives Result of SJF: A never gets to run

What’s the difference between starvation and a deadlock?