39
Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling

Embed Size (px)

DESCRIPTION

Chapter 5: CPU Scheduling. Outline. Why scheduling? Scheduling Criteria Scheduling Algorithms Real-Time Scheduling Thread Scheduling Example: Solaris Algorithm Evaluation. Process Execution. Process execution consists of alternating cycles of CPU execution and I/O wait - PowerPoint PPT Presentation

Citation preview

Chapter 5: CPU Scheduling

Outline Why scheduling? Scheduling Criteria Scheduling Algorithms Real-Time Scheduling Thread Scheduling Example: Solaris Algorithm Evaluation

Process Execution Process execution

consists of alternating cycles of CPU execution and I/O wait

Long-term scheduler gets a good mix of CPU-bound & I/O-bound jobs

Given that, how to scheudle ready processes? – Shor-term scheduling: focus of this chapter

Why scheduling is necessary?

Maximize CPU utilization via multiprogramming, while minimizing response time and/or maximizing throughput → Efficient CPU scheduling required

CPU Scheduler

Decide which process to execute next among the ready processes in memory

Order ready processes according to the specified policy such as FCFS (First Come First Serve)

Preemptive vs. nonpreemptive scheduling Preemptive scheduling:

A hihger priority process can preempt a currently running low priority process to avoid priority inversion

Most real-time scheduling algorithms, e.g., EDF & RM More details about RT scheduling will be discussed

later Nonpreemptive scheduling:

A higher priority process waits a currently running process to finish regardless of the priority

e.g., FCFS Others such as I/O & networking devices are not

preemptive

Dispatcher

Gives control of the CPU to the process selected by the short-term scheduler Context switch between the two user

processes Context switch to user mode Jump to the proper location in the user

program to restart it

→ Dispatch latency : Time for dispatcher to stop one process and start another

Scheduling Criteria

CPU utilization: #CPU cycles used for computation/#total CPU cycles per unit time

Throughput: # of processes that complete the execution per unit time

Response time: Amount of time it takes from when a request was submitted until the first response is produced

Scheduling Criteria

Waiting time Amount of time a process has been waiting

in the ready queue Response time = waiting time + execution

time

Optimization Criteria

1. Maximize CPU utilization2. Maximize throughput3. Minimize response time4. Minimize waiting time

Sometimes they may conflict with each other

e.g., system can be overloaded while trying to maximize CPU utilization; response time may significantly increase as a result

First-Come, First-Served (FCFS) Scheduling

Process Burst Time (Execution 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

FCFS Scheduling (Cont.)

Suppose that processes arrive in the order P2 , P3 , P1

Gantt chart:

Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect: A short process behind a long

process suffer long wating/response time

P1P3P2

63 300

Shortest-Job-First (SJF) Schedule the process with the shortest

execution time Two schemes:

Nonpreemptive Once the CPU is given to a process it cannot be

preempted until completes its execution Preemptive

Shortest-Remaining-Time-First (SRTF) If a new process arrives with exec time shorter

than the remaining time of the currently running process executing process, preempt

Shortest-Job-First (SJF)

SJF is optimal: It minimizes average waiting time for a given set of processes

Process Arrival Time Exec Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

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

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Example of Preemptive SJFProcess Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

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

P1 P3P2

42 110

P4

5 7

P2 P1

16

Hard to implement SJF

Don’t know the exact exec time when a process arrives

Can only estimate it by using the length of previous exec time (Take moving average) Doesn’t always work

Prediction of the Length of the Next CPU Burst

Priority Scheduling

A priority number (integer) is associated with each process

The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive nonpreemptive

Priority Scheduling

SJF is a priority scheduling where priority is determined based on the exec time

Problem: Starvation Low priority processes may never execute or suffer

very long waiting time Solution: Aging

As time progresses increase the priority of the process

How to support fairness while preferring high priority processes? Hard problem

Round Robin (RR)

Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds

After the time quantum, preempt the process and add to the end of the ready queue

Round Robin

If there are n processes in the ready queue and the time quantum is q, then 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

Example of RR with Time Quantum = 20

Process Burst TimeP1 53 P2 17 P3 68 P4 24

Gantt chart:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Time Quantum and Context Switch Time

Multilevel Queue

Ready queue is partitioned into separate queues, e.g., foreground (interactive) &background (batch)

Each queue can have its own scheduling algorithm foreground – RR background – FCFS

Multilevel Queue

Scheduling must be done between the queues Fixed priority scheduling Serve all foreground process first Serve background process only if the foreground

queue is empty Possibility of starvation

Time slice Each queue gets a certain amount of CPU time which

it can schedule amongst its processes, e.g., 80% to foreground in RR 20% to background in FCFS

Multilevel Queue Scheduling

Multilevel Feedback Queue

A process can move between queues Aging can be implemented this way Defined by the following parameters:

number of queues scheduling algorithms for each queue method used to determine when to

demote/upgrade a process method used to determine which queue a

process will enter when that process needs service

Multilevel Feedback Queues

Example of Multilevel Feedback Queue

Example: Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS A new job enters queue Q0 . When it gets

CPU, it receives 8 ms. If it does not finish in 8 milliseconds, it is moved to Q1.

At Q1 job receives 16 ms. If it does not complete within 16ms, it is preempted and moved to queue Q2.

Real-Time Scheduling

Hard real-time systems A deadline miss may cause catastrophic

results, e.g., air traffic control, flight control, engine control, etc.

Soft real-time systems A deadline miss does not cause

catastrophic results Meet as many deadlines as possible, e.g.,

video streaming

Earliest Deadline First

Optimal Dynamic scheduling algorithm Schedule the task with the earliest

absolute deadline Advantage: 100% utilization bound Disadvantage: Domino effect under

overload

Rate Monotonic Scheduling

Optimal fixed priority scheduling algorithm

Assign the highest priority to the task with the shortest period

Advantage: Easy to implement Disadvantage

Low utilization bound, i.e., 69% Blind to deadlines T1

T2

RMS schedules

T1 first

RMS schedules

T1 first

Multiple Processor Scheduling

Asymmetric multiprocessing One processor, called master, makes all

scheduling decisions Symmetric multiprocessing (SMP)

Each processor is self-scheduling Need to avoid for two processors to

schedule one process at the same time Supported by most OS – Linux, Solaris, Mac

OS X, Windows XP, Windows 2000 … Load balancing: Push vs. pull migration

Thread Scheduling Local Scheduling (process contention scope)

How the threads library decides which thread to put onto an available LWP

Threads belonging to one process compete for a LWP Global Scheduling (system contention scope)

How the kernel decides which kernel thread to run next

Assign priority POSIX Thread API

Select scheduling algorithm Assign priority to threads

Example: Solaris 2 Scheduling• Assign the highest priority to RT

threads• Apply fixed priority among thread groups

Algorithm Evaluation

Deterministic model Queuing model

Little’s Formula: Queue length n = average arrival rate * average waiting time

Simulation Implementation

5.15

End of Chapter 5: Any Questions?