Você está na página 1de 50

CPU Scheduling

Source: Operating System Concepts by by Silberschatz, Galvin and Gagne

CSC 4103: Operating System

5.1

BB Karki, LSU

CPU Scheduling
CPU scheduling is fundamental to multiprogramming OS Deals with problem of deciding which of processes in the ready queue is to be allocated the CPU. What we cover: Basic concepts Scheduling criteria Scheduling algorithms Multiple-processor scheduling Thread scheduling Real-time scheduling Algorithm evaluation OS examples

CSC 4103: Operating System

5.2

BB Karki, LSU

CPU-I/O Burst Cycle

Process execution consists of a

cycle of CPU execution and I/O wait.


Process alternates between these

two states.
Process always begins and ends

with a CPU burst.

CSC 4103: Operating System

5.3

BB Karki, LSU

Histogram of CPU-Burst Times


Durations of CPU bursts have a typical frequency curve

(exponential or hyper-exponential):
many short bursts an I/O bound program a few long bursts a CPU bound program.

CSC 4103: Operating System

5.4

BB Karki, LSU

CPU Scheduler
CPU scheduler (also called short-term scheduler) selects from among

the processes in ready queue, and allocates the CPU to one of them.
CPU scheduling decision may take place when a process: 1. Switches from running to waiting state. 2. Switches from running to ready state. 3. Switches from waiting to ready. 4. Terminates. Scheduling under 1 and 4 is nonpreemptive. Once the CPU is allocated to a process, the process keeps the CPU until it switches to waiting state or terminates. All other scheduling is preemptive. Incurs a cost.
CSC 4103: Operating System 5.5 BB Karki, LSU

Dispatcher

Dispatcher gives control of the CPU to the scheduled

process. This involves:


switching context switching to user mode jumping to the proper location in the user program to restart

that program Dispatch latency time the dispatcher takes to stop one

process and start another running.

CSC 4103: Operating System

5.6

BB Karki, LSU

Scheduling Criteria
Criteria for comparing CPU-scheduling algorithms:
CPU utilization keep the CPU as busy as possible (0 to 100%

efficiency).
Throughput # of processes that complete their execution per time

unit
Turnaround time amount of time to execute a particular process waiting for memory + ready queue + execution + I/O Waiting time amount of time a process waits in the ready queue Response time amount of time it takes from when a request was

submitted until the first response is produced.

CSC 4103: Operating System

5.7

BB Karki, LSU

Optimization Criteria

Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time Optimize the max or min values or average measure or

variance.

CSC 4103: Operating System

5.8

BB Karki, LSU

First-Come, First-Served (FCFS) Scheduling

FCFS is the simplest CPU-scheduling algorithm. The process that requests the CPU first is allocated the CPU first. Implemented with a FIFO queue PCB of a new process is linked onto the tail of the ready queue. Process at the head of the queue gets CPU first. Average waiting time varies a lot and is quite long. FCFS is non-preemptive.

CSC 4103: Operating System

5.9

BB Karki, LSU

FCFS Scheduling (Cont.)


Process Burst Time (ms) P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:
P1 0 24 P2 27 P3 30

Average waiting time: (0 + 24 + 27)/3 = 17 milliseconds Average turnaround time = ?


CSC 4103: Operating System 5.10 BB Karki, LSU

FCFS Scheduling (Cont.)


Suppose that the processes arrive in the order P2 , P3 , P1 The Gantt chart for the schedule is:
P2 0 3 P3 6 P1 30

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

CSC 4103: Operating System

5.11

BB Karki, LSU

Shortest-Job-First (SJF) Scheduling


Associate with each process the length of

its next CPU burst. Use these lengths to schedule the process with the shortest time first.
SJF is optimal gives minimum average

Process

Burst Time 6 8 7 3

P1 P2 P3 P4

waiting time for a given set of processes.

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


P4 0 3 P1 9 P3 16 P2 24

But with FCFS scheme, average waiting time would be 10.25 ms


BB Karki, LSU

CSC 4103: Operating System

5.12

Determining Length of Next CPU Burst


Challenge is to know the length of the next CPU request: Approximate prediction of the length of next CPU burst: from the lengths of previous CPU bursts by using exponential averaging running average of each burst for each process.

1. t n = actual (observed) length of n th CPU burst 2. n +1 = predicted value for the next CPU burst 3. , 0 1 4. Define : n +1

= t n + (1 ) n
5.13

CSC 4103: Operating System

BB Karki, LSU

Examples of Exponential Averaging


=0
n+1 = n Recent history does not count.

=1
n+1 = tn Only the actual last CPU burst counts.

If we expand the formula, we get:


n+1 = tn + (1 - ) tn-1 + + (1 - )j tn-j + + (1 - )n+1 0

Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor.

CSC 4103: Operating System

5.14

BB Karki, LSU

Prediction of the Length of the Next CPU Burst

CSC 4103: Operating System

5.15

BB Karki, LSU

SJF Scheduling (Cont.)

Two schemes: nonpreemptive once CPU given to the process it cannot be preempted until completes its CPU burst. preemptive if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is known as the Shortest-Remaining-Time-First (SRTF). Preemptive improves average waiting time.

CSC 4103: Operating System

5.16

BB Karki, LSU

Example of Non-Preemptive SJF


Process P1 P2 P3 P4 Arrival Time 0 2 4 5 Burst Time 7 4 1 4

SJF (non-preemptive) P1 0 3 7 P3 8 P2 12 P4 16

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

CSC 4103: Operating System

5.17

BB Karki, LSU

Example of Preemptive SJF


Process P1 P2 P3 P4 P1 0 2 P2 4 P3 5 Arrival Time 0 2 4 5 P2 7 P4 11 Burst Time 7 4 1 4 P1 16

Average waiting time = (9 + 1 + 0 +2)/4 = 3 ms Preemptive SJF suffers from starvation Highest response ratio next (HRRN) scheduling: R = (w + e)/e As time elapses, the ratio increases so that a longer process will eventually get past competing shorter processes.
CSC 4103: Operating System 5.18 BB Karki, LSU

Priority Scheduling
A priority is associated with

each process, and the CPU is allocated to the process with the highest priority.
smallest integer highest

Process

Burst Time 10 1 2 1 5

Priority 3 1 4 5 2

P1 P2 P3 P4 P5

priority. SJF is a priority scheduling

where priority is the inverse of the predicted next CPU burst time.

Average waiting time is 8.2 ms

P2 0 1

P5 6

P1 16
5.19

P3 18

P4 19
BB Karki, LSU

CSC 4103: Operating System

Priority Scheduling

Priorities can be defined internally or externally Priority scheduling can be preemptive or non-preemptive Problem Starvation

Low priority processes may never be executed. e.g., a low-priority process submitted in 1967 never ran until the shutdown of IBM 7094 at MIT in 1973.
Solution Aging

As time progresses, increase the priority of the process.

CSC 4103: Operating System

5.20

BB Karki, LSU

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. Ready queue treated as a circular queue. 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.
CSC 4103: Operating System 5.21

BB Karki, LSU

Example of RR with Time Quantum = 20


Process P1 P2 P3 P4
The Gantt chart is:

Burst Time 53 17 68 24

P1 0 20

P2 37

P3 57

P4 77

P1

P3 97 117

P4

P1

P3

P3

121 134 154 162

Typically, higher average turnaround than SJF, but better response. Useful in time sharing systems.

CSC 4103: Operating System

5.22

BB Karki, LSU

Time Quantum and Context Switch Time

Time quantum needs to be large enough with respect to the context switch time. Context switch increases the waiting and turnaround time.
CSC 4103: Operating System 5.23 BB Karki, LSU

Turnaround Time Varies With The Time Quantum

CSC 4103: Operating System

5.24

BB Karki, LSU

Multilevel Queue
Ready queue is partitioned into separate queues: foreground (interactive) background (batch) Each queue has its own scheduling algorithm, foreground RR background FCFS Scheduling must be done between the queues. Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. Time slice each queue gets a certain amount of CPU time which it can schedule amongst its processes; 80% to foreground in RR and 20% to background in FCFS.

CSC 4103: Operating System

5.25

BB Karki, LSU

Multilevel Queue Scheduling

Each queue has absolute priority over lower-priority queues.


CSC 4103: Operating System 5.26 BB Karki, LSU

Multilevel Feedback Queue


A process can move between the various queues: Move a process to a lower priority queue if it uses too much CPU time. Move a process to a higher priority queue if it waits too long aging can be implemented this way. Multilevel-feedback-queue scheduler defined by the

following parameters:
number of queues scheduling algorithm for each queue method used to determine when to upgrade a process to a

higher-priority queue method used to determine when to demote a process to a lower-priority queue method used to determine which queue a process will enter when that process needs service.

CSC 4103: Operating System

5.27

BB Karki, LSU

Example of Multilevel Feedback Queue


Three queues: Q0 time quantum 8 milliseconds Q1 time quantum 16 milliseconds Q2 FCFS

CSC 4103: Operating System

5.28

BB Karki, LSU

Multiple-Processor Scheduling

CPU scheduling more complex when multiple CPUs are available Homogeneous (identical) processors within a multiprocessor system Asymmetric multiprocessing one single processor (called master

server) does all scheduling including I/O processing


Other processors execute only user codes.

Symmetric multiprocessing (SMP) - Each processor is self-

scheduling
Provide a separate queue for each processor Use a common ready queue Two issues: Processor affinity and load balancing

CSC 4103: Operating System

5.29

BB Karki, LSU

Symmetric Multithreading
Symmetric multithreading (SMT) - runs several threads at a time by

providing multiple logical rather than physical processors


Also known as hyperthreading technology on Intel processors

Each logical processor has its own architecture state (registers,

interrupt handling) supported in hardware level.


To create multiple logical processors on the same physical processor

Figure illustrates that four processors are available for work on this

system from OSs perspective.

CSC 4103: Operating System

5.30

BB Karki, LSU

Thread Scheduling
Lightweight process (LWP) maps an user-level thread to an

associated kernel level thread


Contention scope: competition among threads at two level Process-contention scope (PCS): schedules user-level threads to run on an available LWP System-contention scope (SCS): schedules kernel-level threads on a physical CPU Pthread scheduling: POSIX Pthread AIP supports PCS and

SCS scheduling with


PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_SYSTEM.

CSC 4103: Operating System

5.31

BB Karki, LSU

Real-Time Scheduling

Hard real-time systems required to complete a critical

task within a guaranteed amount of time.


Scheduling should admit or not the process by examining the

process requirements. Deadline scheduling. Soft real-time computing uses priority scheduling Critical processes receive priority over less fortunate ones. Priority should not degrade with time. Minimize latency (interrupt and dispatch). Make kernel preemptive.

CSC 4103: Operating System

5.32

BB Karki, LSU

Algorithm Evaluation
Many scheduling algorithms: FCFS, SJF, RR, Priority, Multilevel queue Define the selection criteria: Relative importance of CPU utilization, throughput or response time. Maximize CPU utilization under constraint that the maximum response time is 1 second. Maximize throughput such that turnaround time is (on average) linearly proportional to total execution time. Different methods: Deterministic modeling Queuing models Simulations Implementation.

CSC 4103: Operating System

5.33

BB Karki, LSU

Deterministic Modeling
Analytical evaluation of an algorithm: Takes a particular predetermined workload. Produces a formula or number to define the performance of the algorithm for that workload

Process P1 P2 P3 P4 P5

Burst Time 10 29 3 7 12

Consider FCFS, SJF, and RR (quantum = 10 ms):

Which algorithm would give the minimum average waiting time?


CSC 4103: Operating System 5.34 BB Karki, LSU

Queuing Algorithm
Queuing models CPU burst distribution Arrival time distribution

Littles formula:

average queue length = average arrival rate X average waiting time in the queue.
Compute one variable if you know the other two.

Knowing arrival rates and service rates, one can compute

utilization, average queue length, average wait time.

CSC 4103: Operating System

5.35

BB Karki, LSU

Simulations
A more accurate evaluation Program a model of the computer system A Simulator. OSP operating system project A collection of Java/C modules that together implement an OS. As the simulation executes, statistics that indicate algorithm

performance are gathered and printed.


Data to drive simulation: Random-number generator Trace tapes recorded sequence of actual events.

CSC 4103: Operating System

5.36

BB Karki, LSU

Implementation

Most accurate evaluation Code the algorithm, put in the real OS, and see how it works. Too frequent implementation is inconvenient.

CSC 4103: Operating System

5.37

BB Karki, LSU

Operating System Examples

Solaris Scheduling Windows XP Scheduling Linux Scheduling

CSC 4103: Operating System

5.38

BB Karki, LSU

Solaris Scheduling

Priority-based scheduling Algorithm

Inverse relationship between priorities and time quanta.

CSC 4103: Operating System

5.39

BB Karki, LSU

Solaris Dispatch Table

Interactive and time sharing scheduling use 60 priority levels

CSC 4103: Operating System

5.40

BB Karki, LSU

Windows XP Scheduling

Priority-based, preemptive scheduling algorithm Dispatcher (scheduler) uses a 32-level priority scheme Variable class (1 to 15) and real-time class (16 to 31) Relationship between Windows XP and Win32 API.
CSC 4103: Operating System 5.41 BB Karki, LSU

Linux Scheduling
Preemptive, priority-based scheduling algorithm with two separate

priority ranges:
A real-time range from 0 to 99 and a nice value ranging from 100 to 140

Global priority scheme: Numerically lower values indicate higher priorities Priority is proportional to time quantum (slice)

CSC 4103: Operating System

5.42

BB Karki, LSU

Tasks Indexed According to Priorities


A runqueue data structure contains two priority arrays Active array - tasks with time remaining in their time slices Expired array - all expired tasks Once active array is done, the expired array is changed to the active array Dynamic priorities: Recalculate a tasks priority whenever the task

moves to the expired array


Real-time tasks are assigned static priorities

CSC 4103: Operating System

5.43

BB Karki, LSU

Summary

CPU scheduling selects a process from the ready queue and the dispatcher allocates the CPU to the selected process. FCFS scheduling is the simplest approach but it can hurt short processes SJF scheduling is optimal, providing the shortest average waiting time. It suffers from problems of predicting the length of the next CPU burst and starvation. SJF can be preemptive or non-preemptive. Priority-scheduling algorithm allocates the CPU to the highest-priority process. RR allocates the CPU to all processes in time slice so it is appropriate for time-sharing system. RR is always preemptive. Multilevel queue algorithms allow different algorithms in different queues (interactive and background) and also allow processes to move from one queue to another. Multiprocessor and real-time scheduling are more challenging. Four ways of evaluating scheduling algorithms: Deterministic modeling, queuing models, simulations and implementation. Real operating systems use a combination of different algorithms.
5.44 BB Karki, LSU

CSC 4103: Operating System

Exercises on CPU Scheduling

CSC 4103: Operating System

5.45

BB Karki, LSU

Exercises
5.1: Why is it important for the scheduler to distinguish I/O-

bound programs from CPU-bound programs?


5.3: Consider the exponential average formula used to

predict the length of the next CPU burst. What are the implications of assigning the following values to the parameters used by the algorithm? a. = 0 and 0 = 100 ms b. = 0.99 and 0 = 10 ms.
5.5: Which of the following scheduling algorithms could

result in starvation? a. First-come, first-served b. Shortest job first c. Round robin.


CSC 4103: Operating System 5.46 BB Karki, LSU

Exercises (Cont.)

5.7: Consider a system running ten I/O-bound tasks and one

CPU-bound task. Assume that the I/O-bound tasks issue an I/O operation once for every millisecond of CPU computing and that each I/O operation takes 10 milliseconds to complete. Also assume that the context switching overhead is 0.1 millisecond and that all processes are long-running tasks. What is the CPU utilization for a round-robin scheduler when: a. The time quantum is 1 millisecond b. The time quantum is 10 milliseconds.

CSC 4103: Operating System

5.47

BB Karki, LSU

Exercises (Cont.)

5.9: Consider a preemptive priority scheduling algorithm

based on dynamically changing priorities. Larger priority numbers imply higher priority. When a process is waiting for the CPU (in the ready queue, but not running), its priority changes at a rate when it is running, its priority changes at a rate . All processes are given a priority of 0 when they enter the ready queue. The parameters and can be set to give many different scheduling algorithms. a. What is the algorithm that results from > > 0? b. What is the algorithm that results from < < 0?

CSC 4103: Operating System

5.48

BB Karki, LSU

Exercises (Cont.)

5.12: Consider the scheduling algorithm in the Solaris

operating system for time sharing threads: a. What is the time quantum (in milliseconds) for a thread with priority 10? With priority 55? b. Assume a thread with priority 35 has used its entire time quantum without blocking. What new priority will the scheduler assign this thread? c. Assume a thread with priority 35 blocks for I/O before its time quantum has expired. What new priority will the scheduler assign this thread?

CSC 4103: Operating System

5.49

BB Karki, LSU

Exercises (Cont.)
5.13: The traditional UNIX scheduler enforces an inverse relationship

between priority numbers and priorities: The higher the number, the lower the priority. The scheduler recalculates process priorities once per second using the following function: Priority = (Recent CPU usage / 2) + Base where base = 60 and recent CPU usage refers to a value indicating how often a process has used the CPU since priorities were last recalculated. Assume that recent CPU usage for process P1 is 40, process P2 is 18, and process P3 is 10.
What will be the new priorities for these three processes when priorities

are recalculated? Based on this information, does the traditional UNIX scheduler raise or lower the relative priority of a CPU-bound process?

CSC 4103: Operating System

5.50

BB Karki, LSU

Você também pode gostar