35
Silberschatz, Galvin and Gagne ©2013 perating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

Embed Size (px)

Citation preview

Page 1: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Chapter 6a: CPU Scheduling

Page 2: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.2 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Chapter 6: CPU Scheduling

Basic Concepts

Scheduling Criteria

Scheduling Algorithms

Page 3: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.3 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Objectives

Introduce CPU scheduling Basis for multiprogrammed operating systems

Describe various CPU-scheduling algorithms

Discuss evaluation criteria for selecting a CPU-scheduling algorithm

Page 4: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.4 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Basic Concepts

Maximum CPU utilization with multiprogramming

CPU–I/O Burst Cycle Process execution = cycles of

CPU execution and I/O waits

CPU burst followed by I/O burst

CPU bursts are main concern

Page 5: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.5 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Histogram of CPU-burst Times

Page 6: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.6 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

CPU Scheduler

Short-term scheduler selects among processes in ready queue, allocates CPU

Queue ordered in various ways Depends on algorithm

Page 7: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.7 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

CPU Scheduler

Scheduling decisions may take place when process:

1. Switches from running to waiting state

2. Switches from running to ready state

3. Switches from waiting to ready

Terminates

1 and 4 are nonpreemptive Process voluntarily yields CPU

2 and 3 are preemptive Process preempted; CPU reallocated

Page 8: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.8 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Dispatcher

Dispatcher gives control of CPU to process selected by short-term scheduler; this involves: Switching context

Switching to user mode

Jumping to proper location in user program to restart

Dispatch latency – time dispatcher takes to stop one process and start another running

Page 9: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.9 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Chapter 6: CPU Scheduling

Basic Concepts

Scheduling Criteria

Scheduling Algorithms

Page 10: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.10 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Scheduling Criteria

CPU utilization – keep CPU busy as possible

Throughput – # of processes that complete execution per time unit

Turnaround time – amount of time to execute a particular process

Page 11: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.11 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Scheduling Criteria

Waiting time –time process has been waiting in ready queue

Response time – amount of time from when request submitted until first response is produced Does not include device output

Page 12: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.12 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Scheduling Algorithm Optimization Criteria

Maximize CPU utilization

Maximize throughput

Minimize turnaround time

Minimize waiting time

Minimize response time

Page 13: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.13 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Chapter 6: CPU Scheduling

Basic Concepts

Scheduling Criteria

Scheduling Algorithms

Page 14: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.14 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

First- Come, First-Served (FCFS) Scheduling

Simplest algorithm

Process that requests CPU first is allocate CPU first

Easily manage by FIFO queue Process enters ready queue at tail CPU allocated to process at head Running process removed from queue Process runs to completion

Page 15: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.15 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

First- Come, First-Served (FCFS) Scheduling

Process Burst Time

P1 24

P2 3

P3 3

Suppose processes arrive in order: P1 , P2 , P3

Gantt Chart for schedule:

Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

P P P1 2 3

0 24 3027

Page 16: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.16 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

FCFS Scheduling (Cont.)

Suppose processes arrive in order:

P2 , P3 , P1

Gantt chart schedule:

Waiting time for P1 = 6; P2 = 0; P3 = 3

Average waiting time: (6 + 0 + 3)/3 = 3

Much better than previous case

P 1

0 3 6 30

P 2 P 3

Page 17: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.17 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

FCFS Scheduling (Cont.)

Convoy effect - short process behind long process E.g., one CPU-bound and many I/O-bound processes

All I/O bound processes wait in ready queue for CPU-bound to give up CPU

Lowers CPU and device utilization Compared to if shorter processes were allowed to go first

Page 18: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.18 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Shortest-Job-First (SJF) Scheduling

Each process knows length of next CPU burst CPU available => schedule process with smallest burst

Ties scheduled in FCFS

SJF is optimal – gives minimum average waiting time Difficulty: knowing length of next CPU request

Could ask user

Could make mathematical predictions E.g., .1 1 nnn t

Page 19: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.19 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Example of SJF

ProcessAril Time Burst Time

P1 0.0 6

P2 2.0 8

P3 4.0 7

P4 5.0 3

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

P 3

0 3 24

P 4 P 1

169

P 2

Page 20: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.20 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Example of Shortest-remaining-time-first

Add varying arrival times and preemption...

ProcessAarri Arrival TimeTBurst Time

P1 0 8

P2 1 4

P3 2 9

P4 3 5

Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec

P 4

0 1 26

P 1 P 2

10

P 3P 1

5 17

Page 21: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.21 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Priority Scheduling

Priority number associated with each process SJF => priority scheduling algorithm

Priority = inverse of next CPU burst time

CPU allocated to process with highest priority E.g., smallest number = highest priority

Can be Preemptive or Nonpreemptive

Page 22: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.22 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Priority Scheduling

Priority Scheduling may result in starvation Low priority processes may never execute

Aging is possible solution Process’ priority increases with time

Page 23: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.23 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Example of Priority Scheduling

ProcessAarri Burst TimeT Priority

P1 10 3

P2 1 1

P3 2 4

P4 1 5

P5 5 2

Average waiting time = 8.2 msec

Page 24: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.24 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Round Robin (RR)

Each process gets unit of CPU time (time quantum q), usually 10-100 milliseconds After time elapsed, process is preempted, added to end

of ready queue

n processes in ready queue, time quantum q, Each process gets 1/n of CPU time

CPU time in chunks of at most q time units

No process waits more than (n-1)q time units

Page 25: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.25 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Round Robin (RR)

Timer interrupts every quantum to schedule next process If process finishes before time quantum, next process starts

If process not finished, added to tail of queue

Performance q large FIFO

q small q must be large with respect to context switch, otherwise overhead is too high

Page 26: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.26 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Example of RR with Time Quantum = 4

Time quantum = 4 ms

Process Burst Time

P1 24

P2 3

P3 3

Average wait time: 5.66 ms (6 + 4 + 7) / 3 = 17 / 3 = 5.66

P P P1 1 1

0 18 3026144 7 10 22

P 2 P 3 P 1 P 1 P 1

Page 27: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.27 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Example of RR with Time Quantum = 4

Typically, higher average turnaround than SJF But better response time

q should be large compared to context switch time q usually 10ms to 100ms, context switch < 10 µs

Page 28: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.28 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Time Quantum and Context Switch Time

Page 29: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.29 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Turnaround Time Varies With The Time Quantum

80% of CPU bursts should be shorter than q

Page 30: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.30 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Multilevel Queue

Ready queue partitioned into separate queues, e.g., foreground (interactive)

background (batch)

Processes cannot switch between queues Queue assignment is permanent

Each queue has own scheduling algorithm, e.g., foreground – RR

background – FCFS

Page 31: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.31 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Multilevel Queue: Scheduling b/t Qs

Fixed priority scheduling Serve all foreground then background

Possibility of starvation

Time slice Each queue gets certain amount of CPU time

Time scheduled amongst processes, e.g., 80% to foreground in RR

20% to background in FCFS

Page 32: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.32 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Multilevel Queue Scheduling

Page 33: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.33 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Multilevel Feedback Queue

Processes can move between various queues Aging implemented this way

Multilevel-feedback-queue scheduler defined by: Number of queues

Scheduling algorithms for each queue

Method to determine when to upgrade a process

Method to determine when to demote a process

Method to determine which queue a process will enter

Page 34: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.34 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Example of Multilevel Feedback Queue

Three queues Q0 – RR; q = 8 ms

Q1 – RR; 1 = 16 ms

Q2 – FCFS

Page 35: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling

6.35 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 2nd Edition

Example of Multilevel Feedback Queue

New job enters Q0 , order is FCFS If process doesn’t finish in

8 ms, moved to Q1

Q1 order is FCFS

If job does not finish in 16 ms, moved to Q2

If no jobs in Q0 and Q1, jobs in Q2 processed FCFS