csce 311 -operating systems scheduling

24
CSCE 311 - Operating Systems Scheduling Qiang Zeng, Ph.D.

Upload: others

Post on 02-Feb-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCE 311 -Operating Systems Scheduling

CSCE 311 - Operating SystemsScheduling

Qiang Zeng, Ph.D.

Page 2: CSCE 311 -Operating Systems Scheduling

Resource Allocation Graph describing the traffic jam

CSCE 311 - Operating Systems 2

Page 3: CSCE 311 -Operating Systems Scheduling

Conditions for Deadlock

CSCE 311 - Operating Systems 3

Mutual Exclusion

• A process cannot access a resource that has been allocated to another process

Hold-and-Wait

• a process may hold allocated resources while awaiting assignment of others

No Pre-emption

• no resource can be forcibly removed from a process holding it

Circular Wait

• a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

Page 4: CSCE 311 -Operating Systems Scheduling

Dealing with Deadlock

• Three general approaches exist for dealing with deadlock:

CSCE 311 - Operating Systems 4

• adopt a policy that eliminates one of the conditions• E.g., numbering resources, and request from low to high

Prevent Deadlock

• make the appropriate dynamic choices based on the current state of resource allocation

• E.g., banker’s algorithm

Avoid Deadlock

• attempt to detect the presence of deadlock and take action to recover

Detect Deadlock and Recover

Page 5: CSCE 311 -Operating Systems Scheduling

Outline

• Scheduling policy: what to do next – when there are multiple threads in the ready queue;– more generally, multiple packets to send, or web

requests to serve• Metrics to evaluate a scheduling policy• Uniprocessor scheduling policies• Multiprocessor scheduling policies

Some of the slides are courtesy of Dr. Thomas Anderson

CSCE 311 - Operating Systems 5

Page 6: CSCE 311 -Operating Systems Scheduling

Example

• You manage a restaurant, and the customers complain that they wait forever and starve. What will you do?

• You manage a web site, that suddenly becomes wildly popular. Do you?– Buy more hardware?– Turn away some users? – Implement a different scheduling policy?

CSCE 311 - Operating Systems 6

Page 7: CSCE 311 -Operating Systems Scheduling

Non-preemptive vs Pre-emptive

• A preemptive scheduling means that the scheduler can take resources (e.g., CPU) away from the process– Recall timer interrupts and CPU time slice

• A non-preemptive scheduling means a process occupies the resources until it voluntarily relinquishes the resources

CSCE 311 - Operating Systems 7

Page 8: CSCE 311 -Operating Systems Scheduling

Metrics to evaluate a scheduler

• Response time– Time elapsed from the time of submission to the first

response• Throughput

– # of tasks can be done per unit of time?• Wait time

– Time spent on waiting in the ready queue• Turnaround time

– Tfinish - Tstart• Predictability (low variance)

– How consistent is the performance over time?• Fairness

CSCE 311 - Operating Systems 8

Page 9: CSCE 311 -Operating Systems Scheduling

First Come First Serve (FCFS)

• Schedule tasks in the order they arrive– Continue running them until they complete or give up

the processor• Easy to implement; very small overhead due to

scheduling• Average wait time can be large if small jobs wait

behind long ones – You have a basket, but wait behind one with a cart

CSCE 311 - Operating Systems 9

Page 10: CSCE 311 -Operating Systems Scheduling

Example

CSCE 311 - Operating Systems 10

10/6/16, 3:22 PMCPU Scheduling in OS | Operating System Tutorial | Studytonight

Page 2 of 5http://www.studytonight.com/operating-system/cpu-scheduling

Scheduling AlgorithmsScheduling Algorithms

We'll discuss four major scheduling algorithms here which are following :

1. First Come First Serve(FCFS) Scheduling

2. Shortest-Job-First(SJF) Scheduling

3. Priority Scheduling

4. Round Robin(RR) Scheduling

5. Multilevel Queue Scheduling

First Come First Serve(FCFS) SchedulingFirst Come First Serve(FCFS) Scheduling

Jobs are executed on first come, first serve basis.

Easy to understand and implement.

Poor in performance as average wait time is high.

Shortest-Job-First(SJF) SchedulingShortest-Job-First(SJF) Scheduling

Best approach to minimize waiting time.

Actual time taken by the process is already known to processor.

Impossible to implement.

(http://www.studytonight.com/)

Tutorials (http://studytonight.com/library/) Q & A Forum (http://studytonight.com/studyroom/)

Tests (http://studytonight.com/tests/) HTML Course (http://studytonight.com/code/)

LogIn (http://www.studytonight.com/login) Suggest (http://www.studytonight.com/suggest)

(https://play.google.com/store/apps/details?id=com.studytonight.app) SignUp (http://www.studytonight.com/register)

Page 11: CSCE 311 -Operating Systems Scheduling

Shortest-Process-First (SPF) Scheduling

• Scheduler selects process with smallest time to finish

• Advantages: low average wait time• Disadvantages:– Potentially large variance in wait times

• Long task can be affected by short tasks once and again• Starvation

– Relies on estimates of time-to-completion, which can be inaccurate or unrealistic

CSCE 311 - Operating Systems 11

Page 12: CSCE 311 -Operating Systems Scheduling

Example

CSCE 311 - Operating Systems 12

Page 13: CSCE 311 -Operating Systems Scheduling

Round Robin

• Each task gets resource for a fixed period of time (time quantum, or CPU time slice)– If task doesn’t complete, it goes back in line

• Need to pick a time quantum– What if time quantum is too long?

• Becomes FIFO– What if time quantum is too short?

• Large overhead for context switch• Disadvantage

– many context switches bring a large overhead; – large wait time, as short tasks do not have priorities

• Advantage: fairness (not really!)

CSCE 311 - Operating Systems 13

Page 14: CSCE 311 -Operating Systems Scheduling

Round Robin = Fairness?

• Is Round Robin always fair?– No! See the next slides

CSCE 311 - Operating Systems 14

Page 15: CSCE 311 -Operating Systems Scheduling

CPU bound vs I/O bound

• If a process’s speed is mainly determined by the CPU speed, the process is CPU-bound– E.g., multiply matrix

• If a process’s speed is mainly determined by the I/O speed (i.e., most of the time the process blocks for I/O), the process is I/O-bound– E.g., emacs

CSCE 311 - Operating Systems 15

Page 16: CSCE 311 -Operating Systems Scheduling

Mixed Workload

I/O Bound

Tasks

CPU Bound

CPU Bound

Time

Issues I/O

Request

I/OCompletes

IssuesI/O

Request

I/OCompletes

CSCE 311 - Operating Systems 16

Page 17: CSCE 311 -Operating Systems Scheduling

CSCE 311 - Operating Systems 17

Solution: Multi-level Feedback Queue

Page 18: CSCE 311 -Operating Systems Scheduling

Multi-level Feedback Queue (MFQ)

• Used in Linux, Windows, MacOS, and Solaris– Each system adopts the algorithm with some little

modifications• First developed by Corbato et al; led to Turing

Award

CSCE 311 - Operating Systems 18

Page 19: CSCE 311 -Operating Systems Scheduling

MFQ

• Has a set of Round Robin queues– Each queue has a separate priority

• High priority queues have short time slices, while low priority queues have long time slices

• Scheduler picks the first thread in highest-priority non-empty queue

• When a process is scheduled out, it is inserted back into queues following the two rules– If time slice expires, task drops one level– If the process relinquishes the slice due to I/O, it is kept in the

current priority queue• Optionally, for a process in the base level queue that

becomes I/O bound, it can be promoted to the next-higher queue

CSCE 311 - Operating Systems 19

Page 20: CSCE 311 -Operating Systems Scheduling

MFQ

Priority

1

Time Slice (ms)

Time SliceExpiration

New or I/O Bound Task

2

4

3

80

40

20

10

Round Robin Queues

CSCE 311 - Operating Systems 20

Page 21: CSCE 311 -Operating Systems Scheduling

Multiprocessor Scheduling

• What would happen if we used MFQ on a multiprocessor?– Contention for scheduler spinlock– Poor CPU cache reuse (we will discuss cache in

future lectures)

CSCE 311 - Operating Systems 21

Page 22: CSCE 311 -Operating Systems Scheduling

Per-Processor Affinity Scheduling

• Each processor has its own MFQ– Each MFQ is protected by a per-processor spinlock

• When the system puts threads back on the ready list, they are put back the MFQ where they had most recently run

• But idle processors can steal work from other processors

CSCE 311 - Operating Systems 22

Page 23: CSCE 311 -Operating Systems Scheduling

Per-Processor Multi-level Feedbackwith Affinity Scheduling

Processor 3Processor 2Processor 1

CSCE 311 - Operating Systems 23

Page 24: CSCE 311 -Operating Systems Scheduling

Summary

• Scheduling policy: what to do next, when there are multiple threads ready to run

• Response time, throughput, wait time• Uniprocessor scheduling policies– FIFO, Shortest Job First – round robin– multilevel feedback as approximation of optimal

• Multiprocessor scheduling policies– Per-Processor Affinity Scheduling

CSCE 311 - Operating Systems 24