os05

50
BB Karki, LSU 5.1 CSC 4103: Operating System CPU Scheduling Source: Operating System Concepts by by Silberschatz, Galvin and Gagne

Upload: ravi-soni

Post on 22-Oct-2014

535 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OS05

BB Karki, LSU5.1CSC 4103: Operating System

CPU Scheduling

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

Page 2: OS05

BB Karki, LSU5.2CSC 4103: Operating System

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

Page 3: OS05

BB Karki, LSU5.3CSC 4103: Operating System

CPU-I/O Burst Cycle

Process execution consists of acycle of CPU execution and I/O wait.

Process alternates between thesetwo states.

Process always begins and endswith a CPU burst.

Page 4: OS05

BB Karki, LSU5.4CSC 4103: Operating System

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.

Page 5: OS05

BB Karki, LSU5.5CSC 4103: Operating System

CPU Scheduler

CPU scheduler (also called short-term scheduler) selects from amongthe 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.

Page 6: OS05

BB Karki, LSU5.6CSC 4103: Operating System

Dispatcher

Dispatcher gives control of the CPU to the scheduledprocess. 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 oneprocess and start another running.

Page 7: OS05

BB Karki, LSU5.7CSC 4103: Operating System

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 timeunit

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 wassubmitted until the first response is produced.

Page 8: OS05

BB Karki, LSU5.8CSC 4103: Operating System

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 orvariance.

Page 9: OS05

BB Karki, LSU5.9CSC 4103: Operating System

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.

Page 10: OS05

BB Karki, LSU5.10CSC 4103: Operating System

FCFS Scheduling (Cont.)

Process Burst Time (ms)P1 24 P2 3P3 3

Suppose that the processes arrive in the order: P1 , P2 , P3The Gantt Chart for the schedule is:

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

Average turnaround time = ?

P1 P2 P3

24 27 300

Page 11: OS05

BB Karki, LSU5.11CSC 4103: Operating System

FCFS Scheduling (Cont.)

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

The Gantt chart for the schedule is:

Average waiting time: (6 + 0 + 3)/3 = 3 ms Much better than previous case.

Convoy effect - short process behind long process

P1P3P2

63 300

Page 12: OS05

BB Karki, LSU5.12CSC 4103: Operating System

Shortest-Job-First (SJF) Scheduling

Associate with each process the length ofits next CPU burst. Use these lengths toschedule the process with the shortesttime first.

SJF is optimal – gives minimum averagewaiting time for a given set of processes.

Process Burst TimeP1 6P2 8P3 7P4 3

Average waiting time is(3 + 16 + 9 + 0)/4 = 7 msBut with FCFS scheme,average waiting time wouldbe 10.25 ms

P4 P1 P3 P2

0 3 9 16 24

Page 13: OS05

BB Karki, LSU5.13CSC 4103: Operating System

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.

:Define 4.

10 , 3.

burst CPUnext for the valuepredicted 2.

burst CPU oflength (observed) actual 1.

1

≤≤

=

=

+

αα

τ n

thn nt

τ n+1 =α tn + 1−α( )τ n

Page 14: OS05

BB Karki, LSU5.14CSC 4103: Operating System

Examples of Exponential Averaging

α =0τn+1 = τnRecent 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 successiveterm has less weight than its predecessor.

Page 15: OS05

BB Karki, LSU5.15CSC 4103: Operating System

Prediction of the Length of the Next CPU Burst

Page 16: OS05

BB Karki, LSU5.16CSC 4103: Operating System

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. Thisscheme is known as the Shortest-Remaining-Time-First (SRTF).

Preemptive improves average waiting time.

Page 17: OS05

BB Karki, LSU5.17CSC 4103: Operating System

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

SJF (non-preemptive)

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

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Page 18: OS05

BB Karki, LSU5.18CSC 4103: Operating System

Example of Preemptive SJF

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

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.

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 19: OS05

BB Karki, LSU5.19CSC 4103: Operating System

Priority Scheduling

A priority is associated witheach process, and the CPU isallocated to the process withthe highest priority. smallest integer ≡ highest

priority.

SJF is a priority schedulingwhere priority is the inverse ofthe predicted next CPU bursttime.

Process Burst Time PriorityP1 10 3P2 1 1P3 2 4P4 1 5P5 5 2

Average waiting time is 8.2 ms

P2 P5 P1 P3

0 1 6 16 18 19

P4

Page 20: OS05

BB Karki, LSU5.20CSC 4103: Operating System

Priority Scheduling

Priorities can be defined internally or externally

Priority scheduling can be preemptive or non-preemptive

Problem ≡ StarvationLow priority processes may never be executed.

e.g., a low-priority process submitted in 1967 never ran untilthe shutdown of IBM 7094 at MIT in 1973.

Solution ≡ AgingAs time progresses, increase the priority of the process.

Page 21: OS05

BB Karki, LSU5.21CSC 4103: Operating System

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 timequantum is q, then each process gets 1/n of the CPU time inchunks 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, otherwiseoverhead is too high.

Page 22: OS05

BB Karki, LSU5.22CSC 4103: Operating System

Example of RR with Time Quantum = 20

Process Burst TimeP1 53 P2 17 P3 68 P4 24

The Gantt chart is:

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

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 23: OS05

BB Karki, LSU5.23CSC 4103: Operating System

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.

Page 24: OS05

BB Karki, LSU5.24CSC 4103: Operating System

Turnaround Time Varies With The Time Quantum

Page 25: OS05

BB Karki, LSU5.25CSC 4103: Operating System

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.

Page 26: OS05

BB Karki, LSU5.26CSC 4103: Operating System

Multilevel Queue Scheduling

Each queue has absolute priority over lower-priority queues.

Page 27: OS05

BB Karki, LSU5.27CSC 4103: Operating System

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 thefollowing 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.

Page 28: OS05

BB Karki, LSU5.28CSC 4103: Operating System

Example of Multilevel Feedback Queue

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

Page 29: OS05

BB Karki, LSU5.29CSC 4103: Operating System

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 masterserver) 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

Page 30: OS05

BB Karki, LSU5.30CSC 4103: Operating System

Symmetric Multithreading

Symmetric multithreading (SMT) - runs several threads at a time byproviding 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 thissystem from OS’s perspective.

Page 31: OS05

BB Karki, LSU5.31CSC 4103: Operating System

Thread Scheduling

Lightweight process (LWP) maps an user-level thread to anassociated 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 andSCS scheduling with PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_SYSTEM.

Page 32: OS05

BB Karki, LSU5.32CSC 4103: Operating System

Real-Time Scheduling

Hard real-time systems – required to complete a criticaltask 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.

Page 33: OS05

BB Karki, LSU5.33CSC 4103: Operating System

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.

Page 34: OS05

BB Karki, LSU5.34CSC 4103: Operating System

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 Burst Time P1 10

P2 29 P3 3 P4 7 P5 12

Consider FCFS, SJF, and RR (quantum = 10 ms):Which algorithm would give the minimum average waiting time?

Page 35: OS05

BB Karki, LSU5.35CSC 4103: Operating System

Queuing Algorithm

Queuing models CPU burst distribution Arrival time distribution

Little’s 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 computeutilization, average queue length, average wait time.

Page 36: OS05

BB Karki, LSU5.36CSC 4103: Operating System

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 algorithmperformance are gathered and printed.

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

Page 37: OS05

BB Karki, LSU5.37CSC 4103: Operating System

Implementation

Most accurate evaluation Code the algorithm, put in the real OS, and see how it

works.

Too frequent implementation is inconvenient.

Page 38: OS05

BB Karki, LSU5.38CSC 4103: Operating System

Operating System Examples

Solaris Scheduling

Windows XP Scheduling

Linux Scheduling

Page 39: OS05

BB Karki, LSU5.39CSC 4103: Operating System

Solaris Scheduling

Priority-based scheduling Algorithm

Inverse relationshipbetween priorities and time quanta.

Page 40: OS05

BB Karki, LSU5.40CSC 4103: Operating System

Solaris Dispatch Table

Interactive and timesharing scheduling use 60 priority levels

Page 41: OS05

BB Karki, LSU5.41CSC 4103: Operating System

Windows XP Scheduling

Priority-based, preemptive scheduling algorithmDispatcher (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.

Page 42: OS05

BB Karki, LSU5.42CSC 4103: Operating System

Linux Scheduling

Preemptive, priority-based scheduling algorithm with two separatepriority 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)

Page 43: OS05

BB Karki, LSU5.43CSC 4103: Operating System

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 task’s priority whenever the taskmoves to the expired array Real-time tasks are assigned static priorities

Page 44: OS05

BB Karki, LSU5.44CSC 4103: Operating System

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 fromproblems of predicting the length of the next CPU burst and starvation. SJF can bepreemptive 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-sharingsystem. RR is always preemptive.

Multilevel queue algorithms allow different algorithms in different queues (interactiveand 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, queuingmodels, simulations and implementation.

Real operating systems use a combination of different algorithms.

Page 45: OS05

BB Karki, LSU5.45CSC 4103: Operating System

Exercises on CPU Scheduling

Page 46: OS05

BB Karki, LSU5.46CSC 4103: Operating System

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 topredict the length of the next CPU burst. What are theimplications of assigning the following values to theparameters used by the algorithm?

a. α = 0 and τ0 = 100 msb. α = 0.99 and τ0 = 10 ms.

5.5: Which of the following scheduling algorithms couldresult in starvation? a. First-come, first-served b. Shortest job first c. Round robin.

Page 47: OS05

BB Karki, LSU5.47CSC 4103: Operating System

Exercises (Cont.)

5.7: Consider a system running ten I/O-bound tasks and oneCPU-bound task. Assume that the I/O-bound tasks issue anI/O operation once for every millisecond of CPU computingand that each I/O operation takes 10 milliseconds tocomplete. Also assume that the context switching overheadis 0.1 millisecond and that all processes are long-runningtasks. What is the CPU utilization for a round-robinscheduler when: a. The time quantum is 1 millisecond b. The time quantum is 10 milliseconds.

Page 48: OS05

BB Karki, LSU5.48CSC 4103: Operating System

Exercises (Cont.)

5.9: Consider a preemptive priority scheduling algorithmbased on dynamically changing priorities. Larger prioritynumbers imply higher priority. When a process is waitingfor the CPU (in the ready queue, but not running), itspriority changes at a rate α when it is running, its prioritychanges at a rate β. All processes are given a priority of 0when 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?

Page 49: OS05

BB Karki, LSU5.49CSC 4103: Operating System

Exercises (Cont.)

5.12: Consider the scheduling algorithm in the Solarisoperating 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 willthe scheduler assign this thread?

c. Assume a thread with priority 35 blocks for I/Obefore its time quantum has expired. What new prioritywill the scheduler assign this thread?

Page 50: OS05

BB Karki, LSU5.50CSC 4103: Operating System

Exercises (Cont.)

5.13: The traditional UNIX scheduler enforces an inverse relationshipbetween priority numbers and priorities: The higher the number, thelower the priority. The scheduler recalculates process priorities onceper second using the following function:

Priority = (Recent CPU usage / 2) + Basewhere base = 60 and recent CPU usage refers to a value indicatinghow often a process has used the CPU since priorities were lastrecalculated.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?