Scheduling.

Slides:



Advertisements
Similar presentations
CPU Scheduling.
Advertisements

Slide 7-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7 7 Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 5: CPU Scheduling.
Chapter 5 CPU Scheduling. CPU Scheduling Topics: Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: CPU Scheduling.
Chapter 6: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Chapter 6: CPU Scheduling Basic.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 5-CPU Scheduling
Scheduling. Model of Process Execution Ready List Ready List Scheduler CPU Resource Manager Resource Manager Resources Preemption or voluntary yield AllocateRequest.
Slide 7-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 7.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Basic Concepts Maximum CPU utilization.
Chapter 6: CPU Scheduling
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
2 Program Process Abstract Computing Environment File Manager Memory Manager Device Manager Protection Deadlock Synchronization Process Description Process.
Silberschatz and Galvin  Operating System Concepts Module 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria.
Chapter 5: Process Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Basic Concepts Maximum CPU utilization can be obtained.
1 11/29/2015 Chapter 6: CPU Scheduling l Basic Concepts l Scheduling Criteria l Scheduling Algorithms l Multiple-Processor Scheduling l Real-Time Scheduling.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Chapter 5: CPU Scheduling Basic.
Silberschatz and Galvin  Operating System Concepts Module 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 5 CPU Scheduling Slide 1 Chapter 5 CPU Scheduling.
6.1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
1 Uniprocessor Scheduling Chapter 3. 2 Alternating Sequence of CPU And I/O Bursts.
Chapter 4 CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
CPU scheduling.  Single Process  one process at a time  Maximum CPU utilization obtained with multiprogramming  CPU idle :waiting time is wasted 2.
Basic Concepts Maximum CPU utilization obtained with multiprogramming
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
CPU SCHEDULING.
Chapter 6: CPU Scheduling
Dan C. Marinescu Office: HEC 439 B. Office hours: M, Wd 3 – 4:30 PM.
EEE Embedded Systems Design Process in Operating Systems 서강대학교 전자공학과
Chapter 5a: CPU Scheduling
CPU Scheduling Algorithms
Operating Systems Processes Scheduling.
CPU Scheduling.
Chapter 6: CPU Scheduling
Chapter 6: CPU Scheduling
Process management Information maintained by OS for process management
Chapter 5: CPU Scheduling
CS 143A - Principles of Operating Systems
CPU Scheduling Basic Concepts Scheduling Criteria
CPU Scheduling G.Anuradha
Chapter 6: CPU Scheduling
Module 5: CPU Scheduling
Chapter 5: CPU Scheduling
Outline Announcements Process Management – continued
Operating System Concepts
3: CPU Scheduling Basic Concepts Scheduling Criteria
Chapter5: CPU Scheduling
Chapter 5: CPU Scheduling
Chapter 6: CPU Scheduling
Outline Scheduling algorithms Multi-processor scheduling
Operating System Concepts
Chapter 5: CPU Scheduling
Lecture 2 Part 3 CPU Scheduling
Outline Announcements Process Scheduling– continued
Outline Announcement Process Scheduling– continued
Operating System , Fall 2000 EA101 W 9:00-10:00 F 9:00-11:00
Shortest-Job-First (SJR) Scheduling
Chapter 6: CPU Scheduling
Module 5: CPU Scheduling
Chapter 6: CPU Scheduling
Chapter 6: CPU Scheduling
CPU Scheduling: Basic Concepts
Module 5: CPU Scheduling
Presentation transcript:

Scheduling

Review: Process Manager Program Process Abstract Computing Environment Process Description File Manager Process Mgr Protection Deadlock Synchronization Device Manager Memory Manager Resource Manager Scheduler Devices Memory CPU Other H/W

Scheduling Scheduling mechanism is the part of the process manager that handles the removal of the running process of CPU and the selection of another process on the basis of a particular strategy Scheduler chooses one from the ready threads to use the CPU when it is available Scheduling policy determines when it is time for a thread to be removed from the CPU and which ready thread should be allocated the CPU next

Thread Scheduler Organization Ready List Scheduler CPU Resource Manager Resources Preemption or voluntary yield Allocate Request Done New Thread job “Ready” “Running” “Blocked”

Thread Scheduling New threads put into ready state and added to ready list Running thread may cease using CPU for any of four reasons Thread has completed its execution Thread requests a resource, but can’t get it Thread voluntarily releases CPU Thread is preempted by scheduler and involuntarily releases CPU Scheduling policy determines which thread gets the CPU and when a thread is preempted

The Scheduler Organization Ready Process Enqueuer Ready List Dispatcher Context Switcher Process Descriptor CPU From Other States Running Process

Enqueuer Adds processes which are ready to run to the ready list May compute the priority for the waiting process (could also be determined by the dispatcher)

Context Switcher Saves contents of processor registers for a process being taken off the CPU If hardware has more than one set of processor registers, OS may just switch between sets Typically, one set is used for supervisor mode, others for applications Depending on OS, process could be switched out voluntarily or involuntarily

Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context From process, to dispatcher, to new process switching to user mode jumping to the proper location in the user program to restart that program

Process/Thread Context Rn . . . Status Registers Functional Unit Left Operand Right Operand Result ALU PC IR Ctl Unit

Context Switching Old Thread Descriptor CPU New Thread Descriptor

Context Switch Timing Context switching is a time-consuming process Assuming n general registers and m status registers, each requiring b store operations, and K time units to perform a store, the time required is (n + m) b × K time units Then a processor requiring 50 ns to store 1 unit of information, and assuming that n = 32 and m = 8, the time required is 2000 ns = 2 μs A complete context switch involves removing process, loading dispatcher, removing dispatcher, loader new process  at least 8 μs required Note that a 1 Ghz processor could have executed about 4,000 instructions in this time Some processors use several sets of registers to reduce this switching time (one set for supervisor mode, the other for user)

Invoking the Scheduler Need a mechanism to call the scheduler Voluntary call Process blocks itself Calls the scheduler Non-preemptive scheduling Involuntary call External force (interrupt) blocks the process Preemptive scheduling

Voluntary CPU Sharing Each process will voluntarily share the CPU By calling the scheduler periodically The simplest approach Requires a yield instruction to allow the running process to release CPU yield(pi.pc, pj.pc) { memory[pi.pc] = PC; PC = memory[pj.pc]; }

Voluntary CPU Sharing – cont. pi can be “automatically” determined from the processor status registers So function can be written as yield(*, pj.pc) { memory[pi.pc] = PC; PC = memory[pj.pc]; }

More on Yield pi and pj can resume one another’s execution yield(*, pj.pc); . . . yield(*, pi.pc); Suppose pj is the scheduler: // p_i yields to scheduler yield(*, pj.pc); // scheduler chooses pk yield(*, pk.pc); // pk yields to scheduler // scheduler chooses ...

Voluntary CPU Sharing – cont. Every process periodically yields to the scheduler Relies on correct process behavior Malicious Accidental Need a mechanism to override running process

Voluntary CPU Sharing – cont.

Involuntary CPU Sharing Periodic involuntary interruption Through an interrupt from an interval timer device Which generates an interrupt whenever the timer expires The scheduler will be called in the interrupt handler A scheduler that uses involuntary CPU sharing is called a preemptive scheduler

Programmable Interval Timer InterruptCount--; if (InterruptCount <= 0) { InterruptRequest = TRUE; InterruptCount = K } SetInterval( <programmableValue>) { K = <programmableValue>; InterruptCount = K; Interrupt occurs every K clock ticks

Involuntary CPU Sharing – cont Interval timer device handler Keeps an in-memory clock up-to-date Invokes the scheduler IntervalTimerHandler() { Time++; // update the clock TimeToSchedule--; if(TimeToSchedule <= 0) { <invoke scheduler>; TimeToSchedule = TimeSlice; }

Contemporary Scheduling Involuntary CPU sharing – timer interrupts Time quantum determined by interval timer – usually fixed size for every process using the system Sometimes called the time slice length

Choosing a Process To Run Mechanism never changes Strategy = policy the dispatcher uses to select a process from the ready list Different policies for different requirements

Policy Considerations Policy can control/influence: CPU utilization Average time a process waits for service Average amount of time to complete a job Could strive for any of: Equitability Favor very short or long jobs Meet priority requirements Meet deadlines

Optimal Scheduling Suppose the scheduler knows each process pi’s service time, t(pi) -- or it can estimate each t(pi) : Policy can optimize on any criteria, e.g., CPU utilization Waiting time Deadline To find an optimal schedule: Have a finite, fixed # of pi Know t(pi) for each pi Enumerate all schedules, then choose the best

However ... The t(pi) are almost certainly just estimates General algorithm to choose optimal schedule is O(n2) Other processes may arrive while these processes are being serviced Usually, optimal schedule is only a theoretical benchmark – scheduling policies try to approximate an optimal schedule

Strategy Selection The scheduling criteria will depend in part on the goals of the OS and on priorities of processes, fairness, overall resource utilization, throughput, turnaround time, response time, and deadlines

Process Model and Metrics P will be a set of processes, p0, p1, ..., pn-1 S(pi) is the state of pi {running, ready, blocked} τ(pi), the service time The amount of time pi needs to be in the running state before it is completed W (pi), the waiting time The time pi spends in the ready state before its first transition to the running state TTRnd(pi), turnaround time The amount of time between the moment pi first enters the ready state and the moment the process exits the running state for the last time

Simplified Model Simplified, but still provides analysis results Ready List Scheduler CPU Resource Manager Resources Allocate Request Done New Process job “Ready” “Running” “Blocked” Preemption or voluntary yield Simplified, but still provides analysis results Easy to analyze performance No issue of voluntary/involuntary sharing

Estimating CPU Utilization New Process Ready List Scheduler CPU Done Let l = the average rate at which processes are placed in the Ready List, arrival rate Let μ = the average service rate  1/ μ = the average t(pi) l pi per second System Each pi uses 1/ μ units of the CPU

Estimating CPU Utilization New Process Ready List Scheduler CPU Done Let l = the average rate at which processes are placed in the Ready List, arrival rate Let μ = the average service rate  1/ μ = the average t(pi) Let r = the fraction of the time that the CPU is expected to be busy r = # pi that arrive per unit time * avg time each spends on CPU r = l * 1/ μ = l/ μ Note: must have l < m (i.e., r < 1) What if r approaches 1?

Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time Which one to use depends on the system’s design goal

Nonpreemptive Schedulers Blocked or preempted processes New Process Ready List Scheduler CPU Done Try to use the simplified scheduling model Only consider running and ready states Ignores time in blocked state: New process created when it enters ready state Process is destroyed when it enters blocked state Really just looking at “small phases” of a process

Everyday scheduling methods First-come, first served (FCFS) Shorter jobs first (SJF) or Shortest job next (SJN) Higher priority jobs first Job with the closest deadline first

FCFS at the supermarket

SJF at the supermarket

Gantt Chart Used to illustrate deterministic schedules Dependencies of a process on other processes Plots processor(s) against time Shows which processes on executing on which processors at which times Also shows idle time, so illustrates the utilization of each processor In following, will only assume one processor

First-Come-First-Served Assigns priority to processes in the order in which they request the processor i τ(pi) 350 1 125 2 475 3 250 4 75

First-Come-First-Served – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 p0 TTRnd(p0) = t(p0) = 350 W(p0) = 0 350

First-Come-First-Served – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 350 475 p0 p1 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 W(p0) = 0 W(p1) = TTRnd(p0) = 350

First-Come-First-Served – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 475 950 p0 p1 p2 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475

First-Come-First-Served – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 950 1200 p0 p1 p2 p3 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 TTRnd(p3) = (t(p3) +TTRnd(p2)) = 250+950 = 1200 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475 W(p3) = TTRnd(p2) = 950

First-Come-First-Served – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 1200 1275 p0 p1 p2 p3 p4 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 TTRnd(p3) = (t(p3) +TTRnd(p2)) = 250+950 = 1200 TTRnd(p4) = (t(p4) +TTRnd(p3)) = 75+1200 = 1275 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475 W(p3) = TTRnd(p2) = 950 W(p4) = TTRnd(p3) = 1200

FCFS Average Wait Time Easy to implement Ignores service time, etc i t(pi) 0 350 1 125 2 475 3 250 4 75 p0 p1 p2 p3 p4 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 TTRnd(p3) = (t(p3) +TTRnd(p2)) = 250+950 = 1200 TTRnd(p4) = (t(p4) +TTRnd(p3)) = 75+1200 = 1275 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475 W(p3) = TTRnd(p2) = 950 W(p4) = TTRnd(p3) = 1200 Wavg = (0+350+475+950+1200)/5 = 2974/5 = 595 1275 1200 950 475 350 Easy to implement Ignores service time, etc Not a great performer

Predicting Wait Time in FCFS In FCFS, when a process arrives, all in ready list will be processed before this job Let μ be the service rate Let L be the ready list length Wavg(p) = L*1/μ + 0.5* 1/ μ = L/ μ +1/(2 μ) (in queue) (active process) Compare predicted wait with actual in earlier examples

First-Come-First-Served – cont. 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 30

First-Come-First-Served – 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. P1 P3 P2 6 3 30

Shortest-Job-Next Scheduling Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. SJN is optimal gives minimum average waiting time for a given set of processes.

Shortest-Job-Next Scheduling – cont. Two schemes: non-preemptive – 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 know as the Shortest-Remaining-Time-Next (SRTN).

Shortest Job Next (nonpreemptive) i t(pi) 0 350 1 125 2 475 3 250 4 75 75 p4 TTRnd(p4) = t(p4) = 75 W(p4) = 0

Shortest Job Next – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 75 200 p4 p1 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p4) = t(p4) = 75 W(p1) = 75 W(p4) = 0

Shortest Job Next – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 75 200 450 p4 p1 p3 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75 W(p1) = 75 W(p3) = 200 W(p4) = 0

Shortest Job Next – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 75 200 450 800 p4 p1 p3 p0 TTRnd(p0) = t(p0)+t(p3)+t(p1)+t(p4) = 350+250+125+75 = 800 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75 W(p0) = 450 W(p1) = 75 W(p3) = 200 W(p4) = 0

Shortest Job Next – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 75 200 450 800 1275 p4 p1 p3 p0 p2 TTRnd(p0) = t(p0)+t(p3)+t(p1)+t(p4) = 350+250+125+75 = 800 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p2) = t(p2)+t(p0)+t(p3)+t(p1)+t(p4) = 475+350+250+125+75 = 1275 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75 W(p0) = 450 W(p1) = 75 W(p2) = 800 W(p3) = 200 W(p4) = 0

Shortest Job Next – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 Minimizes wait time May starve large jobs Must know service times 75 200 450 800 1275 p4 p1 p3 p0 p2 W(p0) = 450 W(p1) = 75 W(p2) = 800 W(p3) = 200 W(p4) = 0 TTRnd(p0) = t(p0)+t(p3)+t(p1)+t(p4) = 350+250+125+75 = 800 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p2) = t(p2)+t(p0)+t(p3)+t(p1)+t(p4) = 475+350+250+125+75 = 1275 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75 Wavg = (450+75+800+200+0)/5 = 1525/5 = 305

Determining Length of Next CPU Burst Can only estimate the length. Can be done by using the length of previous CPU bursts, using exponential averaging.

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.

Priority Scheduling In priority scheduling, processes/threads are allocated to the CPU based on the basis of an externally assigned priority A commonly used convention is that lower numbers have higher priority Static priorities vs. dynamic priorities Static priorities are computed once at the beginning and are not changed Dynamic priorities allow the threads to become more or less important depending on how much service it has recently received

Priority Scheduling – cont. There are non-preemptive and preemptive priority scheduling algorithms Preemptive nonpreemptive SJN is a priority scheduling where priority is the predicted next CPU burst time. FCFS is a priority scheduling where priority is the arrival time

Non-preemptive Priority Scheduling i t(pi) Pri 0 350 5 1 125 2 2 475 3 3 250 1 4 75 4 Reflects importance of external use May cause starvation Can address starvation with aging 250 375 850 925 1275 p3 p1 p2 p4 p0 TTRnd(p0) = t(p0)+t(p4)+t(p2)+t(p1) )+t(p3) = 350+75+475+125+250 = 1275 TTRnd(p1) = t(p1)+t(p3) = 125+250 = 375 TTRnd(p2) = t(p2)+t(p1)+t(p3) = 475+125+250 = 850 TTRnd(p3) = t(p3) = 250 TTRnd(p4) = t(p4)+ t(p2)+ t(p1)+t(p3) = 75+475+125+250 = 925 TTRnd = (1275+375+850+250+925)/5 = 735 W(p0) = 925 W(p1) = 250 W(p2) = 375 W(p3) = 0 W(p4) = 850 Wavg = (925+250+375+0+850)/5 = 2400/5 = 480

Deadline Scheduling Allocates service by deadline May not be feasible i t(pi) Deadline 0 350 575 1 125 550 2 475 1050 3 250 (none) 4 75 200 p0 p1 p2 p3 p4 1275 1050 550 200 Allocates service by deadline May not be feasible 575

Real-Time Scheduling Hard real-time systems – required to complete a critical task within a guaranteed amount of time. Soft real-time computing – requires that critical processes receive priority over less fortunate ones.

Preemptive Schedulers Ready List Scheduler CPU Preemption or voluntary yield Done New Process Highest priority process is guaranteed to be running at all times Or at least at the beginning of a time slice Dominant form of contemporary scheduling But complex to build & analyze

Preemptive Shortest Job Next Also called the shortest remaining job next When a new process arrives, its next CPU burst is compared to the remaining time of the running process If the new arriver’s time is shorter, it will preempt the CPU from the current running process

Example of Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 Average time spent in ready queue = (9 + 1 + 0 +2)/4 = 3 P1 P3 P2 4 2 11 P4 5 7 16

Comparison of Non-Preemptive and Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJN (non-preemptive) Average time spent in ready queue (0 + 6 + 3 + 7)/4 = 4 P1 P3 P2 7 3 16 P4 8 12

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

Round-robin scheduling Good way to upset customers!

Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 W(p0) = 0 50 0 350 1 125 2 475 3 250 4 75 50 p0 W(p0) = 0

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 p0 p1 W(p0) = 0 W(p1) = 50

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 p0 p1 p2 W(p0) = 0 W(p1) = 50 W(p2) = 100

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 p0 p1 p2 p3 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 p0 p1 p2 p3 p4 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 300 p0 p1 p2 p3 p4 p0 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 300 400 475 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 300 400 475 550 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 TTRnd(p1) = 550 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 p0 p2 p3 p0 p2 p3 TTRnd(p1) = 550 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 1050 p0 p2 p3 p0 p2 p3 p0 p2 p0 TTRnd(p0) = 1100 TTRnd(p1) = 550 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 1050 1150 1250 1275 p0 p2 p3 p0 p2 p3 p0 p2 p0 p2 p2 p2 p2 TTRnd(p0) = 1100 TTRnd(p1) = 550 TTRnd(p2) = 1275 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 p0 TTRnd(p0) = 1100 TTRnd(p1) = 550 TTRnd(p2) = 1275 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 Wavg = (0+50+100+150+200)/5 = 500/5 = 100 475 400 300 200 100 Equitable Most widely-used Fits naturally with interval timer p4 p1 p3 p2 550 650 750 850 950 1050 1150 1250 1275 TTRnd_avg = (1100+550+1275+950+475)/5 = 4350/5 = 870

Round Robin – cont. Performance q large  FIFO q small  q must be large with respect to context switch, otherwise overhead is too high.

Turnaround Time Varies With The Time Quantum

How a Smaller Time Quantum Increases Context Switches

Round Robin (TQ=50) – cont. Overhead must be considered i t(pi) 0 350 1 125 2 475 3 250 4 75 p0 TTRnd(p0) = 1320 TTRnd(p1) = 660 TTRnd(p2) = 1535 TTRnd(p3) = 1140 TTRnd(p4) = 565 W(p0) = 0 W(p1) = 60 W(p2) = 120 W(p3) = 180 W(p4) = 240 Wavg = (0+60+120+180+240)/5 = 600/5 = 120 540 480 360 240 120 p4 p1 p3 p2 575 790 910 1030 1150 1270 1390 1510 1535 TTRnd_avg = (1320+660+1535+1140+565)/5 = 5220/5 = 1044 635 670

Multi-Level Queues Each list may use a different policy FCFS SJN RR Preemption or voluntary yield Ready List0 New Process Scheduler Ready List1 CPU Done Ready List2 Each list may use a different policy FCFS SJN RR Ready Listn

Multilevel Queues Ready queue is partitioned into separate queues foreground (interactive) background (batch) Each queue has its own scheduling algorithm foreground – RR background – FCFS

Multilevel Queues – cont. 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; i.e., 80% to foreground in RR 20% to background in FCFS

Multilevel Queue Scheduling

Multilevel Feedback Queue A process can move between the various queues; aging can be implemented this way. 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

Multilevel Feedback Queues – cont.

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 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

Two-queue scheduling

Three-queue scheduling

Multiple-Processor Scheduling CPU scheduling more complex when multiple CPUs are available. Homogeneous processors within a multiprocessor. Load sharing Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing.

Algorithm Evaluation Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload. Queuing models Implementation

Evaluation of CPU Schedulers by Simulation

Contemporary Scheduling Involuntary CPU sharing -- timer interrupts Time quantum determined by interval timer -- usually fixed for every process using the system Sometimes called the time slice length Priority-based process (job) selection Select the highest priority process Priority reflects policy With preemption Usually a variant of Multi-Level Queues

Scheduling in real OSs All use a multiple-queue system UNIX SVR4: 160 levels, three classes time-sharing, system, real-time Solaris: 170 levels, four classes time-sharing, system, real-time, interrupt OS/2 2.0: 128 level, four classes background, normal, server, real-time Windows NT 3.51: 32 levels, two classes regular and real-time Mach uses “hand-off scheduling”

BSD 4.4 Scheduling Involuntary CPU Sharing Preemptive algorithms 32 Multi-Level Queues Queues 0-7 are reserved for system functions Queues 8-31 are for user space functions nice influences (but does not dictate) queue level

Windows NT/2K/XP Scheduling Involuntary CPU Sharing across threads Preemptive algorithms 32 Multi-Level Queues Highest 16 levels are “real-time” Next lower 15 are for system/user threads Range determined by process base priority Lowest level is for the idle thread

Scheduler in Linux In file kernel/sched.c The policy is a variant of RR scheduling

Summary The scheduler is responsible for multiplexing the CPU among a set of ready processes / threads It is invoked periodically by a timer interrupt, by a system call, other device interrupts, any time that the running process terminates It selects from the ready list according to its scheduling policy Which includes non-preemptive and preemptive algorithms