cosc 3407: operating systems lecture 11: cpu scheduling

47
This lecture… Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling

Upload: magnus-todd

Post on 27-Dec-2015

237 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

This lecture… Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling

Page 2: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

CPU–I/O Burst Cycle The objective of

multiprogramming is to have some process running at all times, to maximum CPU utilization.

Process execution consists of a cycle of CPU execution and I/O wait.

Page 3: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

CPU Burst Distribution The CPU bursts tend to have an exponential or

hyper-exponential curve. There is a large number of short CPU bursts, and

there is a small number of long CPU bursts. An I/O bound program

– many short CPU bursts. A CPU-bound program

– few long CPU bursts.

Page 4: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Histogram of CPU-burst Times

Page 5: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Schedulers Long-term scheduler (or job scheduler) – selects

which processes should be brought into the ready queue.

Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.

The long-term scheduler controls the degree of multiprogramming.

The long-term scheduler must select a good process mix of I/O-bound and CPU-bound processes.

Page 6: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

(Short-Term) CPU Scheduler Selects from among the processes in memory

that are ready to execute, and allocates the CPU to one of them.

CPU scheduling decisions 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. All other scheduling is preemptive.

Page 7: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Scheduling Goals Maximize CPU utilization - keep the CPU as busy as

possible (40% - 90%) Maximize throughput - operations (=jobs) per second

– minimize overhead (context switching)– efficient use of resources (CPU, disk, cache, …)

Minimize turnaround time - amount of time to execute a particular process

Minimize waiting time - amount of time a process has been waiting in the ready queue

Minimize response time - elapsed time to do an operation (or job). Response time is what the user sees: elapsed time to– Echo a keystroke in editor (acceptable delay ~50-

150millisec)– Compile a program– Run a large scientific problem

Page 8: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Scheduling Goals (cont’d) Fairness

– Share CPU among users in some equitable way– Fairness is not minimizing average response time:

» Better average response time by making system less fair

Page 9: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

First-Come, First-Served (FCFS or FIFO) Example: Process Burst 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

Page 10: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

FCFS Scheduling (Cont.)

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

The Gantt chart for the schedule is:

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case. Convoy effect short process behind long process FCFS algorithm is nonpreemptive.

P1P3P2

63 300

Page 11: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

FCFS Scheduling (Cont.) FIFO Pros and Cons:

– Simple (+)– Short jobs get stuck behind long ones (-)

Page 12: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

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.

If two processes have same length next CPU burst, FCFS scheduling is used to break the tie.

Two schemes: – nonpreemptive – once CPU given to the process it

cannot be preempted until it 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 know as the shortest-Remaining-Time-First (SRTF).

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

Page 13: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Example of Non-Preemptive SJF Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4 SJF (non-preemptive)

Average waiting time = (0 + (8-2) + (7-4) + (12-5))/4 = 16/4 = 4

Average waiting time (FCFS) = (0+(7-2)+(11-4)+(12-5))/4 = 19/4 = 4.75

8

P1 P3 P2 P4

0 3 7 12 16

Page 14: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Example of Preemptive SJF (SRTF) Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4 SJF (preemptive)

Average waiting time = ((11-2) + (5-4) + (4-4) +(7-5))/4 = 12/3 = 3

P1 P3P2 P4

0 4 7 11 16

P2 P1

2 5

Page 15: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

SRTF discussion SRTF Pros & Cons

– Optimal (average response time) (+)– Hard to predict future (-)– Unfair (-)

Page 16: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Determining Length of Next CPU Burst Long-term scheduling (batch system)

– process time limit specified by the user– SJF scheduling is used frequently in long-term

scheduling SJF cannot be implemented at the level of short-term CPU

scheduling. Can only predict the length of the next CPU burst. Next CPU burst is predicted as an exponential average of

the measured lengths of previous CPU bursts.1. t n = actual length of nth CPU burst

2. n+1 = predicted value for the next CPU burst3. , 0 14. Define

n+1 = tn + (1- ) n.– controls the relative weight of recent and past history in our

prediction.

Page 17: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Prediction of the Length of the Next CPU Burst

Page 18: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Examples of Exponential Averaging =0

– n+1 = n

– Recent history does not count. =1

– n+1 = tn

– Only the most recent CPU burst counts. = 1/2

– recent and past history are equally weighted

– 0 can be a constant or overall system average

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.

Page 19: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

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

Equal-priority processes are scheduled in FCFS order.– Preemptive– nonpreemptive

SJF is a priority scheduling where priority is the inverse of the (predicted) next CPU burst time.

Page 20: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Priority SchedulingProcess Burst Time Priority

P1 10 3

P2 1 1

P3 2 3

P4 1 4

P5 5 2 The Gantt chart for the schedule is:

Average waiting time = (6+0+16+18+1)/5 = 8.2

P2 P5 P1 P3 P4

0 1 6 16 18 19

Page 21: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Priority Scheduling Internally defined priorities

– time limits, memory requirements, number of open files, ratio of average I/O burst to average CPU burst

Externally defined priorities– importance of the process, the type and amount of funds

being paid for computer use, the department sponsoring the work, etc.

Problem of Starvation – low priority processes may wait indefinitely for the CPU.– Two possibilities

» process may eventually run when the system is lightly loaded» computer system may crash and loose all low-priority

processes Solution

– Aging: as time progresses increase the priority of the process.

Page 22: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

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.

RR scheduling is implemented as a FIFO queue of processes.

New processes are added to the tail of the ready queue.

A process may itself leave the CPU if its CPU burst is less than 1 quantum, otherwise timer will cause an interrupt and the process will be put at the tail of the queue.

Page 23: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Example: RR with Time Quantum = 20

Process Burst Time P1 53

P2 17

P3 68

P4 24 The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 24: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Example cont’d Waiting time for P1=(77-20)+(121-97)= 81

P2=(20-0)=20 P3=(37-0)+(97-57)+(134-

117)= 94 P4=(57-0)+(117-77)= 97

Average waiting time = (81+20+94+97)/4=73 Typically, higher average turnaround than SJF, but

better response. Round-Robin Pros and Cons:

– Better for short jobs, Fair (+)– Context-switching time adds up for long jobs (-)

Page 25: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Round Robin (RR) 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.

Example– 5 processes, time quantum = 20

» each process will get up to 20 ms every 80 ms. Performance

– q large FCFS– q small processor sharing

» appears as though each of n processes has its own processor running at 1/n the speed of the real processor

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

Page 26: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

How a Smaller Time Quantum Increases Context Switches

Page 27: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Turnaround Time Varies With The Time Quantum

Page 28: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Turnaround Time Varies With The Time Quantum The average turnaround time can be improved if

most processes finish their next CPU burst in a single time quantum.

Example– 3 processes, CPU burst = 10 for each

» quantum = 1, average turnaround time = 29» quantum = 10, average turnaround time = 20» If context-switch time is added, average turnaround time

will increase for smaller time quantum as there will be more number of context switches.

A rule of thumb– 80 percent of the CPU bursts should be shorter than the

time quantum.

Page 29: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Multilevel Queue Scheduling When processes can be classified into different groups, such

as– foreground (interactive) processes– background (batch) processes

These two types of processes have different response-time requirements.

Foreground processes may have priority (externally defined) over background processes.

Multilevel queue-scheduling algorithm partitions the ready queue into separate queues.

The processes are permanently assigned to one queue, based on memory size, process priority, or process type.

Each queue has its own scheduling algorithm, foreground – RRbackground – FCFS

Page 30: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Multilevel Queue Scheduling

Page 31: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Multilevel Queue Scheduling Scheduling must be done between the queues.

– Fixed priority preemptive 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; i.e.,80% to foreground in RR

– 20% to background in FCFS

Page 32: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Multilevel Feedback-Queue Scheduling A process can move between the various queues. CPU-bound processes are moved to lower-priority queue. I/O-bound and interactive processes get the higher-priority

queue. A process that waits too long in a lower-priority queue is

moved to a higher-priority queue. aging can be implemented this way and prevents

starvation. Multilevel-feedback-queue scheduler defined by the

following parameters:– number of queues– scheduling algorithms for each queue– method used to determine when to upgrade a process– method used to determine when to demote a process– method used to determine which queue a process will

enter when that process needs service

Page 33: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Multilevel Feedback Queues

Page 34: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Example of Multilevel Feedback Queue Three queues:

– Q0 – time quantum 8 milliseconds

– Q1 – time quantum 16 milliseconds

– Q2 – FCFS

Scheduling– A new job enters queue Q0 . When it gains CPU, job

receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.

– If Q0 is empty, the process at the head of Q1 receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

– Processes in Q2 are run on an FCFS basis, only when Q0 and Q1 are empty.

Page 35: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Fairness Unfair in scheduling:

– STCF: *must* be unfair to be optimal – Favor I/O jobs? Penalize CPU.

How do we implement fairness?– Could give each queue a fraction of the CPU.

» But this isn’t always fair. What if there’s one long-running job, and 100 short-running ones?

– Could adjust priorities: increase priority of jobs, as they don’t get service. This is what’s done in UNIX.» Problem is that this is ad hoc – what rate should you

increase priorities?» And, as system gets overloaded, no job gets CPU time, so

everyone increases in priority. » The result is that interactive jobs suffer – both short and

long jobs have high priority!

Page 36: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Lottery scheduling: random simplicity Give every job some number of lottery tickets On each time slice, randomly pick a winning

ticket. On average, CPU time is proportional to # of

tickets given to each job.

How do you assign tickets? – To approximate SRTF, short running jobs get more, long

running jobs get fewer. – To avoid starvation, every job gets at least one ticket (so

everyone makes progress).

Page 37: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Grace under load change Adding or deleting a job (and their tickets):

– affects all jobs proportionately, independent of how many tickets each job has.

For example, if short jobs get 10 tickets, and long jobs get 1 each, then:

# short jobs / % of CPU each short % of CPU each long# long jobs job gets job gets

1/1 91% 9% 0/2 NA 50% 2/0 50% NA 10/1 10% 1% 1/10 50% 5%

Page 38: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Multiple-Processor Scheduling Homogeneous processors within a

multiprocessor: use any available processor to run any processes in the queue.

Load sharing : several identical processors available.– Separate queue for each processor

» one processor could be idle, with an empty queue, while another processor is overloaded.

– Keep a common ready queue» all processes go into one queue and are scheduled onto

any available processor

Page 39: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Multiple-Processor Scheduling Common ready queue:

– Symmetric Multiprocessing (SMP) – each processor makes its own scheduling decisions.» Each processor examines the common ready queue and

selects a process to execute.» Two processors should not choose the same process and

processes should not get lost from the queue.

– Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing.

Page 40: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Real-Time Scheduling Hard real-time systems – required to complete a

critical task within a guaranteed amount of time.– The scheduler may either admit the process, guaranteeing

that it will complete in time, or reject the request as impossible.

– “apply-breaks” process in your car Soft real-time computing – requires that critical

processes receive priority over less fortunate ones.– Keep Dispatch latency low

» system calls should be preemptible: few safe preemption points in kernel

» make entire kernel preemptible: protect kernel data through synchronization

» priority inversion: inherit high priority to finish with kernel data

Page 41: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Thread Scheduling User-level threads are mapped to an associated kernel-

level thread through an indirect mapping to a lightweight process (LWP).

Process Local Scheduling – The threads library schedules user-level threads onto an available LWP.

System Global Scheduling – How the kernel decides which kernel thread to run next.

Solaris 2 Scheduling– uses priority-based process scheduling.– Four classes of scheduling (in order of priority)

» real-time » system» time sharing» interactive

Page 42: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Solaris 2 Scheduling Within each class different priorities and

scheduling algorithms. Time-sharing - default scheduling class

– scheduling policy - dynamically alters priorities and assigns time slices of different lengths using a multilevel feedback queue.

– Inverse relationship between priorities and time slices: The higher the priority, lower the time slice, and vice versa.

Interactive processes have a higher priority; CPU-bound processes a lower priority.– Good response time for interactive processes and good

throughput for CPU-bound processes.

Page 43: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Solaris 2 Scheduling System class: runs kernel processes, such as the scheduler

and paging daemon.– Priority of a system process does not change.

Real-time class: threads have highest priority among all classes.– guaranteed response from the system within a bounded

period of time. The scheduler converts the class-specific priorities into

global priorities, and selects to run the thread with the highest global priority.

The selected thread runs on the CPU until it– blocks– uses its time slice– is preempted by a higher priority thread; multiple

threads with same priority are run in round robin

Page 44: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Java Thread Scheduling JVM Uses a Preemptive, Priority-Based Scheduling Algorithm. JVM schedules the Runnable thread with the highest priority

for execution. FIFO Queue is used if there are multiple Runnable threads

with the same priority. JVM schedules a thread to run when one of the following

events occur:– The currently running thread exits the Runnable state.

» Blocks for an I/O, » exits the run() method» suspend() or stop() methods are invoked

– A thread with a higher priority enters the Runnable state» JVM preempts the currently running thread and schedules the

thread with the higher priority for execution* Note – the JVM Does Not Specify Whether Threads are Time-

Sliced or Not - it is up to the particular implementation of the JVM.

Page 45: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Time-Slicing If threads are time sliced:

– Runnable thread is executed for its time quantum or – until it exits the Runnable state, or– is preempted by a higher-priority thread.

A system without time slicing:– To allocate equal amount of time to all threads, the

yield() Method may be used to relinquish control of the CPU.

Cooperative multitasking: A thread voluntarily yielding control to another thread of equal priority.

Page 46: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Thread Priorities JVM selects to run a Runnable thread with the highest

priority. JVM does not dynamically alter priorities. The Java Thread class identifies the following thread

priorities:

Priority CommentThread.MIN_PRIORITY Minimum Thread PriorityThread.MAX_PRIORITY Maximum Thread PriorityThread.NORM_PRIORITY Default Thread Priority

MIN_PRIORITY = 1 MAX_PRIORITY = 10 NORM_PRIORITY = 5 Every Java thread has a priority between 1 and 10.

Page 47: COSC 3407: Operating Systems Lecture 11: CPU Scheduling

Thread Priorities When a thread is created, it is given the same priority as

the thread that created it. Priorities May Be Set Using setPriority() method. The class HighThread increases the priority by 1 the default

priority before running the remainder of its run() method.

Public class HighThread extends Thread{ public void run() { this.setPriority(Thread.NORM_PRIORITY + 1); // remainder of the run() method }}