Ch05 - Cpu Scheduling.ppt

Embed Size (px)

Citation preview

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    1/147

    Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition,

    CPU Scheduling

    Modified by M.Rebaudengo - 2010

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    2/147

    5.2 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Schedulers

    !Process migrates among several queues

    " Device queue, job queue, ready queue

    ! Scheduler selects a process to run from these queues

    ! Long-term scheduler:

    " load a job in memory

    " Runs infrequently

    ! Short-term scheduler:" Select ready process to run on CPU

    " Should be fast

    ! Middle-term scheduler (or swapper)

    " Reduce multiprogramming or memory consumption

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    3/147

    5.3 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Classification of Scheduling Activity

    ! Long-term: which process to admit?! Medium-term: which process to swap in or out?! Short-term: which ready process to execute next?

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    4/147

    5.4 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Queuing Diagram for Scheduling

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    5/147

    5.5 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Long-Term Scheduling

    ! Determines which programs are admitted to the system forprocessing

    ! Controls the degree of multiprogramming! Attempts to keep a balanced mix of processor-bound and I/O-

    bound processes" CPU usage

    " System performance

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    6/147

    5.6 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Medium-Term Scheduling

    ! Makes swapping decisions based on the current degree ofmultiprogramming" Controls which remains resident in memory and which jobs must be

    swapped out to reduce degree of multiprogramming

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    7/147

    5.7 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Short-Term Scheduling

    ! Selects from among ready processes in memory which one isto execute next" The selected process is allocated the CPU

    ! It is invoked on events that may lead to choose another processfor execution:" Clock interrupts

    " I/O interrupts

    " Operating system calls and traps

    " Signals

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    8/147

    5.8 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    The Scheduler

    !Programs execute concurrently

    ! Schedulerdetermines which thread executes next

    ! Scheduling policydetermines

    " when thread is removed from CPU

    " which thread allocated the CPU next

    ! Scheduling mechanismdetermines

    " how the process manager can determine that it is time to multiplex theCPU

    " how a thread can be allocated to/removed from the CPU

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    9/147

    5.9 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    The Scheduler

    !Scheduling mechanism" Provides the tools and environment for controlling the flow of the thread

    through the various queues

    !Context Switching between Processes

    !Queueing Capabilities

    ! Scheduling policy" Defines which ready thread the scheduler chooses from the ready list

    " Uses the scheduling mechanism

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    10/147

    5.10 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Basic Concepts

    !Processes require alternate use of processor and I/O in a repetitivefashion

    ! Each cycle consist of a CPU burst followed by an I/O burst

    ! CPU-bound processes have longer CPU bursts than I/O-boundprocesses

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    11/147

    5.11 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    CPU and I/O bound processes

    !Bursts of CPU usage alternate with periods of I/O wait

    " a CPU-bound process

    " an I/O bound process

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    12/147

    5.12 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Histogram of CPU-burst Times

    ! The distribution of CPU cycle times shows a greater number of jobsrequesting short CPU cycles, and fewer jobs requesting long CPU cycles.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    13/147

    5.13 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Alternating Sequence of CPU And I/O Bursts

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    14/147

    5.14 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Example

    {

    printf("Enter the first integer: \n");scanf("%d", &a);

    printf("Enter the second integer: \n");

    scanf("%d", &a);

    c = a+b;

    d = (a*b) c ;

    e = a b;

    f = d/e;

    printf("\n a+b = %d", c);

    printf("\n (a*b) c = %d", d);printf("\n a - b = %d", e);

    printf("\n d/e = %d", f);

    }

    I/O Cycle

    CPU Cycle

    I/O Cycle

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    15/147

    5.15 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Remember

    timeout

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    16/147

    5.16 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Scheduling Mechanisms

    ! Software components

    " Enqueuer!Places a pointer to the process descriptor in the ready list

    !places the process into Ready state and decides its priority

    " Context switcher!Saves the contents of all CPU registers and other state

    !Loads the state of the dispatcher

    " Dispatcher

    !Selects a thread from the ready list and allocates the CPU to it byperforming a context switch to it

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    17/147

    5.17 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    The Scheduler

    ReadyProcess

    Enqueuer Ready

    List

    DispatcherContext

    Switcher

    Process

    Descriptor

    CPU

    From

    Other

    States

    RunningProcess

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    18/147

    5.18 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process/Thread Context

    ! Context Switch" Saves all current registers (general purpose and status)

    " Loads all registers for new process

    " Context saved in a processs descriptor.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    19/147

    5.19 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process/Thread Context

    R1

    R2

    Rn

    . . .

    Status

    Registers

    Functional Unit

    Left Operand

    Right Operand

    Result ALU

    PC

    IR

    Ctl Unit

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    20/147

    5.20 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Context Switching

    CPU

    New Thread Descriptor

    Old Thread

    Descriptor

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    21/147

    5.21 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Scheduling Mechanisms

    ! Context switching: time

    " Save all registers

    !32 or more 32- or 64-bit registers + status registers

    !Uses load/store operations

    !Takes (n + m) b x Ktime units

    ngeneral registers, m status registers

    bstore operations required

    Each store requires Ktime units

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    22/147

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    23/147

    5.23 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Scheduling Mechanisms

    ! Context switching: time

    " A 1GHz machine can run a regular instruction (no memory access) inabout 2 ns

    " During the 4 microseconds of context switch, CPU could have executed2,000 instructions

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    24/147

    5.24 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Performance

    ! Time to run a process depends on

    " CPU time needed by process

    " I/O time needed by process

    " Context switch time (significant)

    " Scheduler choices

    !The longer you wait in the ready list, the longer your process takesto execute

    !If you never get chosen, you starve

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    25/147

    5.25 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Choosing a Process to Run

    ! Mechanismnever changes

    ! Strategy = policythe dispatcher uses to select a process from the

    ready list! Different policies for different requirements

    ReadyProcess

    EnqueueReady

    List

    DispatchContext

    Switch

    Process

    Descriptor

    CPU

    RunningProcess

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    26/147

    5.26 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    CPU Scheduling (cont.)

    ! CPU scheduling decisions may take place when a process:

    1. Switches from running to waiting state (e.g., I/O request)

    2. Switches from running to ready state (e.g., the CPU receives aninterrupt)

    3. Switches from waiting to ready (e.g., completion of an I/O operation)

    4. Terminates

    ! Scheduling under 1 and 4 is nonpreemptive! All other scheduling is preemptive

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    27/147

    5.27 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Preemptive vs. nonpreemptive scheduling

    ! Many CPU scheduling algorithms have both preemptive and nonpreemptiveversions:

    " Preemptive:

    ! schedule a new process even when the current process does notintend to give up the CPU

    ! Currently running process may be interrupted and moved to the

    Ready state by the OS! Prevents one process from monopolizing the processor

    " Non-preemptive:

    ! Once a process is in the running state, it will continue until itterminates or blocks for an I/O

    ! only schedule a new process when the current one does not want

    CPU any more.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    28/147

    5.28 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Dispatcher

    ! The dispatcher is the module that gives control of the CPU to theprocess selected by the short-term scheduler

    ! Dispatcher module gives control of the CPU to the processselected by the short-term scheduler; this involves:

    " switching context

    " switching to user mode

    " jumping to the proper location in the user program to restartthat program

    ! Dispatch latency time it takes for the dispatcher to stop oneprocess and start another running

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    29/147

    5.29 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Scheduling Criteria

    ! CPU utilization percentage of the time that CPU is busy

    ! Throughput number of completed processes per time unit

    ! Turnaround time the interval from the time of submission of aprocess to the time of completion

    ! Waiting time the sum of the periods spent waiting in the readyqueue

    ! Response time amount of time it takes from when a request wassubmitted until the first response is produced (for time-sharing

    environment)

    ! Fairness give everyone an equal amount of CPU time

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    30/147

    5.30 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Scheduling goals

    ! All Systems:

    " Fairness: giving each process a fair share of the CPU

    " Policy enforcement: seeing that stated policy is carried out

    " Balance: keeping all parts of the system busy

    ! Batch Systems:

    " Maximize jobs per hour" Minimize time between submission and termination of a task

    " CPU utilization: keep the CPU busy at all times

    ! Interactive systems:

    " Respondto requests quickly

    " Meet usersexpectations (a task that is supposed to take a short time shouldfinish quickly, and not surprise the user in terms of his/her expectations)

    ! Real-time systems:

    " Meeting deadlines: avoid losing data

    " Predictability: avoid quality degradation in multimedia systems

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    31/147

    5.31 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Scheduling Algorithm Optimization Criteria

    ! Maximize:

    " CPU utilization

    " throughput

    " fairness

    ! Minimize:

    " turnaround time

    " waiting time

    " response time.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    32/147

    5.32 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Methods for evaluating CPU scheduling algorithms

    ! Deterministic Modeling" Take a predetermined workload

    " Run the scheduling algorithm manually

    " Find out the value of the performance metric that you care about.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    33/147

    5.33 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Deterministic modeling example:

    ! Suppose we have processes A, B, and C, submitted at time 0

    ! We want to know the response time, waiting time, and turnaround time ofprocess A

    A B C A B C A C A C Time

    response time = 0+ +wait time

    turnaround time

    Gantt chart: visualize how processes execute.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    34/147

    5.34 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Deterministic modeling example

    ! Suppose we have processes A, B, and C, submitted at time 0

    ! We want to know the response time, waiting time, and turnaround time ofprocess B

    A B C A B C A C A C Time

    response time+wait time

    turnaround time

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    35/147

    5.35 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Deterministic modeling example

    ! Suppose we have processes A, B, and C, submitted at time 0

    ! We want to know the response time, waiting time, and turnaround time ofprocess C

    A B C A B C A C A C Time

    response time+ ++wait time

    turnaround time

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    36/147

    5.36 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Scheduling Policies

    ! FCFS (first come, first served)! SJF (shortest job first)! Priority Scheduling! Round robin! Multilevel feedback queues! Lottery scheduling! This is obviously an incomplete list....

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    37/147

    5.37 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    FCFS (First Come First Served)

    ! FCFS: assigns the CPU based on the order of requests

    " Nonpreemptive: A process keeps running on a CPU until it is blockedor terminated

    " when a process arrives, all in ready list will be processed before this job

    " and none of the following processes will be serviced before this process

    " The process that currently has the CPU must finish.

    + Simple

    - Short jobs can get stuck behind long jobs

    - Turnaround time is not ideal

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    38/147

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    39/147

    5.39 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    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.

    P1P3P2

    63 300

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    40/147

    5.40 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Shortest-Job-First (SJF) Scheduling

    ! SJFruns whatever job puts the least demand on the CPU, also known as

    STCF (shortest time to completion first)

    ! Associate with each process the length of its next CPU burst. Use theselengths to schedule the process with the shortest time+ Probably optimal in terms of turn-around time

    + Gives minimum average waiting time for a given set of processes

    + Great for short jobs+ Small degradation for long jobs

    The difficulty is knowing the length of the next CPU request

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    41/147

    5.41 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Example of SJF

    Process Burst Time

    P1 6

    P2 8

    P3 7

    P4 3

    ! SJF scheduling chart

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

    P4 P3P1

    3 160 9

    P2

    24

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    42/147

    5.42 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition 42

    Shortest Job First Scheduling

    ProcessArrival Time Burst Time

    P1 0 7

    P2 2 4

    P3 4 1P4 5 4

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    43/147

    5.43 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition 43

    Shortest Job First Scheduling

    ProcessArrival Time Burst Time

    P1 0 7

    P2 2 4

    P3 4 1P4 5 4

    P1 P3 P2

    7

    P1(7)

    160

    P4

    8 12

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

    2 4 5

    P2(4)

    P3(1)

    P4(4)

    P1s wating time = 0

    P2s wating time = 6

    P3s wating time = 3P4s wating time = 7

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    44/147

    5.44 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Shortest Remaining Time First (SRTF)

    ! SRTF: a preemptiveversion of SJF

    " If a job arrives with a shorter time to completion, SRTF preempts theCPU for the new job

    " Also known as SRTCF (shortest remaining time to completion first)

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    45/147

    5.45 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition 45

    Shortest Remaining Time First

    Process Arrival Time Burst Time

    P1 0 7

    P2 2 4

    P3 4 1P4 5 4

    P1 P3P2

    42 110

    P4

    5 7

    P2 P1

    16

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

    P1(7)

    P2(4)

    P3(1)

    P4(4)

    P1s wating time = 9

    P2s wating time = 1

    P3s wating time = 0P4s wating time = 2

    P1(5)

    P2(2)

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    46/147

    5.46 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Turn-around time for SJF

    ! Let assume that all the processes are available at the same time

    ! Processes = {T1,T2, T3, ..., Tn } in order of duration

    ! Total Turn-around time =

    = T1+ (T1 +T2 ) + (T1 +T2 +T3 ) + ....+ (T1 +T2 +. . .+Tn ) =

    = n * T1 + (n-1) * T2 + (n-2) * T3 + ... + 2* Tn-1 + Tn

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    47/147

    5.47 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Determining Length of Next CPU Burst

    ! Optimal scheduling

    ! Preemptive SJF is superior to non preemptive SJF.

    ! However, there are no accurate estimations to know the length of the nextCPU burst

    ! Can only estimate the length

    ! Can be done by using the length of previous CPU bursts, using exponential

    averaging

    :Define4.

    10,3.

    burstCPUnexttheforvaluepredicted2.

    burstCPUoflengthactual1.

    !!

    =

    =

    +

    ""

    # 1n

    th

    n nt

    ( ) .11 nnn

    t !""! #+=+

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    48/147

    5.48 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Examples of Exponential Averaging

    ! !=0" "n+1= "n" Recent history does not count

    ! !=1

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

    ! Consider t4, we get:

    "4= !t3+(1 - !) "3+

    = !t3+(1 - !)(!t2+(1 - !)"2)

    = !t3+(1 - !)!t2+ (1 - !)2"2

    = !t3+(1 - !)!t2+ (1 - !)2(!t1+ (1 -!) "1)

    = !t3+(1 - !)!t2+ (1 - !)2!t1+ (1 -!)

    3"1

    = !t3+(1 - !)!t2+ (1 - !)2!t1+ (1 -!)

    3(!t0+ (1 -!) "0)

    = !t3+(1 - !)!t2+ (1 - !)2!t1+ (1 -!)

    3!t0+ (1 -!)4"0

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

    than its predecessor.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    49/147

    5.49 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Examples of Exponential Averaging

    0

    5

    10

    15

    Time

    CPU burst 10 6 4 6 4 13 13 13

    guess 10 8 6 6 5 9 11 12

    0 1 2 3 4 5 6 7

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    50/147

    5.50 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Drawbacks of Shortest Job First

    - Starvation: constant arrivals of short jobs can keep long ones from running

    - There is no way to know the completion time of jobs (most of the time)

    " Some solutions

    ! Ask the user, who may not know any better

    ! If a user cheats, the job is killed

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    51/147

    5.51 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Priority Scheduling

    ! Priority scheduling: A priority number (integer) is associated with each

    process

    ! The CPU is allocated to the process with the highest priority (smallestinteger #highest priority)

    " Priority 0:

    " Priority 1:

    " Priority 2:

    A

    B

    C

    A B TimeC

    Priority Scheduling

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    52/147

    5.52 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Priority Scheduling

    + Generalization of SJF: SJF is a priority scheduling where priority is the

    predicted next CPU burst time

    " With SJF, priority = 1/requested_CPU_time

    - Problem #Starvation low priority processes may never execute

    + Solution #Aging as time progresses increase the priority of the process

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    53/147

    5.53 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition 53

    Real-time Systems

    ! On-line transaction systems! Real-time monitoring systems

    ! Signal processing systems

    " multimedia

    ! Embedded control systems:

    " automotives

    " Robots

    " Aircrafts

    " Medical devices

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    54/147

    5.54 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Real-Time Workload

    ! Job (unit of work)

    " a computation, a file read, a message transmission, etc

    ! Attributes

    " Resources required to make progress

    " Timing parameters

    Released

    Absolute

    deadline

    Relative deadline

    Execution time

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    55/147

    5.55 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Real-Time Task

    ! Task : a sequence of similar jobs

    " Periodic task (p,e)

    !Its jobs repeat regularly

    !Period p= inter-release time (0 < p)

    !Execution time e= maximum execution time (0 < e < p)

    !Utilization U = e/p

    5 10 150

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    56/147

    5.56 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Deadlines: Hard vs. Soft

    ! Harddeadline

    " Disastrous or very serious consequences may occur if the deadline ismissed

    " Validation is essential : can allthe deadlines be met, even under worst-case scenario?

    " Deterministic guarantees

    ! Softdeadline

    " Ideally, the deadline should be met for maximum performance. Theperformance degrades in case of deadline misses.

    " Best effort approaches / statistical guarantees

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    57/147

    5.57 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Schedulability

    ! Property indicating whether a real-time system (a set of real-time tasks) can

    meet their deadlines

    (4,1)

    (5,2)

    (7,2)

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    58/147

    5.58 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Real-Time Scheduling

    ! Determines the order of real-time task executions

    ! Static-priority scheduling

    ! Dynamic-priority scheduling

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    59/147

    5.59 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    RM (Rate Monotonic)

    ! Optimal static-priority scheduling

    ! It assigns priority according to period

    ! A task with a shorter period has a higher priority

    ! Executes a job with the shortest period

    (4,1)

    (5,2)

    (7,2)

    5

    5

    10

    10 15

    15

    T1

    T2

    T3

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    60/147

    5.60 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    RM (Rate Monotonic)

    ! Executes a job with the shortest period

    (4,1)

    (5,2)

    (7,2)

    Deadline Miss !

    5

    5

    10

    10 15

    15

    T1

    T2

    T3

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    61/147

    5.61 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    RM Utilization Bound

    ! Real-time system is schedulable under RM if

    $Ui%n (21/n-1)

    Liu & Layland,

    Scheduling algorithms for multi-programming in a hard-real-timeenvironment, Journal of ACM, 1973.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    62/147

    5.62 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    RM Utilization Bound

    ! Real-time system is schedulable under RM if

    $Ui%n (21/n-1)

    ! Example: T1(4,1), T2(5,1), T3(10,1),

    $Ui= 1/4 + 1/5 + 1/10

    = 0.55

    3 (21/3-1) &0.78

    Thus, {T1, T2, T3} is schedulable under RM.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    63/147

    5.63 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    RM Utilization Bounds

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    1.1

    1 4 16 64 256 1024 4096

    The Number of Tasks

    Utilization

    RM Utilization Bound

    ! Real-time system is schedulable under RM if

    $Ui%n (21/n-1)

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    64/147

    5.64 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    EDF (Earliest Deadline First)

    ! Optimal dynamic priority scheduling

    ! A task with a shorter deadline has a higher priority

    ! Executes a job with the earliest deadline

    (4,1)

    (5,2)

    (7,2)

    5

    5

    10

    10 15

    15

    T1

    T2

    T3

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    65/147

    5.65 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    EDF (Earliest Deadline First)

    ! Executes a job with the earliest deadline

    ! Optimal scheduling algorithm

    " if there is a schedule for a set of real-time tasks,

    EDF can schedule it.

    (4,1)

    (5,2)

    (7,2)

    5

    5

    10

    10 15

    15

    T1

    T2

    T3

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    66/147

    5.66 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    EDF Utilization Bound

    ! Real-time system is schedulable under EDF if and only if

    $Ui%1

    Liu & Layland,

    Scheduling algorithms for multi-programming in a hard-real-timeenvironment, Journal of ACM, 1973.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    67/147

    5.67 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    ! Domino effect during overload conditions

    " Example: T1(4,3), T2(5,3), T3(6,3), T4(7,3)

    EDF Overload Conditions

    T1

    50 7

    T2 T3 T4

    3 6

    Deadline Miss !

    T1

    50 7

    T3

    3 6

    Better schedules :

    T1

    50 7

    T4

    3 6

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    68/147

    5.68 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Round Robin

    ! Goal is an equal distribution of processing time

    ! If there are nprocesses, each gets 1/nof the CPU time.

    ! Round Robin (RR) periodically releases the CPU from long-running jobs

    " Based on timer interrupts

    " Preemptive: a process can be forced to leave its running state andreplaced by another running process

    " Time slice (or time quantum): interval between timer interrupts

    !usually 10-100 milliseconds

    " Most widely used scheduling algorithm

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    69/147

    5.69 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Round Robin (RR)

    ! After the time slice has elapsed, the process is preempted andadded to the end of the ready queue.

    " the ready queue is treated as a circular queue

    ! If there are nprocesses in the ready queue and the timequantum is q, then each process gets 1/nof the CPU time in

    chunks of at most qtime units at once. No process waits morethan (n-1)q time units.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    70/147

    5.70 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    More on Round Robin

    ! If time slice is too long

    " Scheduling degrades to FCFS

    ! If time slice is too short

    " Context switching cost dominates

    ! Timeslice frequently set to ~100 milliseconds

    ! Context switches typically cost < 1 millisecond

    " Context switch is usually negligible (< 1% per timeslice) otherwise youcontext switch too frequently and lose all productivity.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    71/147

    5.71 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition 71

    ! Time Quantum (Q) = 20

    Process Burst Time Wait Time

    P1 53 57 +24 = 81P2 17 20P3 68 37 + 40 + 17= 94P4 24 57 + 40 = 97

    Round Robin Scheduling

    P1 P2 P3 P4 P1 P3 P4 P1 P3 P30 20 37 57 77 97 117 121134 154162

    Average wait time = (81+20+94+97)/4 = 73

    57

    20

    37

    57

    24

    40

    40

    17

    P1(53)

    P2(17)P3(68)

    P4(24)

    P1(33) P1(13)

    P3(48) P3(28)P3(8)

    P4(4)

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    72/147

    5.72 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Time Quantum and Context Switch Time

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    73/147

    5.73 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    FCFS vs. RR (varying Q)

    Quantum

    CompletionTime

    Wait

    Time

    AverageP4P3P2P1

    P2[8]

    P4[24]

    P1[53]

    P3[68]

    0 8 32 85 153

    Best FCFS:

    6257852284Q = 1

    104!11215328125Q = 20

    100!8115330137Q = 1

    66"88852072Q = 20

    31"885032Best FCFS

    121#14568153121Worst FCFS

    69!32153885Best FCFS

    83!121014568Worst FCFS

    95!8015316133Q = 8

    57"5685880Q = 8

    99!9215318135Q = 10

    99!8215328135Q = 5

    61"68851082Q = 10

    61"58852082Q = 5

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    74/147

    5.74 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Turnaround Time Varies With The Time Quantum

    Average turnaround time does not

    necessarily decrease with larger timequanta

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    75/147

    5.75 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Round Robin (RR)

    !Performance

    " Average turnaround time decreases if most processes finish theirnext CPU burst in a singletime quantum.

    " Rule of thumb: 80% of the CPU bursts should be shorter thanthe time quantum.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    76/147

    5.76 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    FCFS vs. Round Robin

    ! Suppose we have three jobs of equal length (3 process of 10 time units

    each)

    " time quantum = 1 then turnaround time is 29

    " time quantum = 10, average turnaround time is 20

    A B C A B C TimeA B C

    turnaround time of A

    turnaround time of B

    turnaround time of C

    Round Robin

    A B TimeC

    turnaround time of A

    turnaround time of B

    turnaround time of C

    FCFS

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    77/147

    5.77 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    FCFS vs. Round Robin

    ! Round Robin

    + Shorter response time

    + Fair sharing of CPU

    - Not all jobs are preemptable

    - Not good for jobs of the same length

    - Not good in terms of the turnaround time.

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    78/147

    5.78 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Multi-Level Queues

    ! All processes of the same priority are placed into the same queue

    ! Scheduler uses one strategy to allocate processor time to each pool

    ! Uses a different strategy for all the processes in the same pool

  • 8/12/2019 Ch05 - Cpu Scheduling.ppt

    79/147

    5.79 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Multi-Level Queues

    ! Example:

    " Use priority scheduling among pools

    !Then all processes in pool i are run before any process in pool j if i