ch05_CPU_Scheduling.pdf

Embed Size (px)

Citation preview

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    1/29

    Chapter 5: CPU SchedulingChapter 5: CPU Scheduling

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    2/29

    Chapter 5: CPU SchedulingChapter 5: CPU Scheduling

    Basic Concepts

    Scheduling Criteria

    Scheduling Algorithms

    Multiple-Processor Scheduling

    -

    Thread Scheduling

    Operating Systems Examples

    Java Thread Scheduling

    Algorithm Evaluation

    5.2 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    3/29

    Basic ConceptsBasic Concepts

    Remember the benefit of multiprogramming?

    z We have several processes in memory

    z

    When one blocks for I/O we can switch the CPU to anotherready process

    Attem ts to maximize CPU utilization

    Question: If we have many ready-to-run processes, which one to

    5.3 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    4/29

    Alternating Sequence of CPU And I/O BurstsAlternating Sequence of CPU And I/O Bursts

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

    Initial CPU burst

    Zero or more

    CPU-I/O burst pairs

    +

    5.4 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    5/29

    Histogram of CPUHistogram of CPU--burst Timesburst Times

    For scheduling we will consider the length of the next CPU burst

    5.5 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

    Typically a few long bursts, many short ones

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    6/29

    CPU SchedulerCPU 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 (e.g. system call, I/O)

    2. Switches from runnin to read state e. . interru t

    3. Switches from waiting to ready (e.g. I/O completes)

    4. Terminatese per orm sc e u ng un er an

    Under (2) and (3) we can:

    z Allow the running process to continue non-preemptive

    z Perform scheduling preemptive

    5.6 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    7/29

    CPU SchedulerCPU Scheduler

    Non-preemptive means process continues until termination or I/O

    Preemptive means that the process can be forced off the CPU

    - ,

    z Preemption could lead to complications: what if the process wasupdating a data shared with another process? E.g.,

    u v , w

    process P2

    If P1 is preempted; s ar s execu ng

    P2 accesses the shared variable, which is in inconsistent

    state

    Non-preemptive OS examples: Windows 3.x, MacOS before OS X

    Preemptive OS examples: Windows 95 and later, Solaris, Linux

    5.7 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    8/29

    DispatcherDispatcher

    Once a process is selected by the short-term scheduler, it is

    (re)started by the dispatcher

    z

    saves state of currently running process (to its PCB)z loads state of selected process (from its PCB)

    contextswitch

    z switches to user mode

    z jumps to the proper location in the selected process to restart itresume

    The dispatch latency is time it takes for the dispatcher to stop one

    process and start another running

    5.8 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    9/29

    Dispatch LatencyDispatch Latency

    5.9 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    10/29

    Scheduling CriteriaScheduling Criteria

    What measures to use to select a scheduling algorithm?

    CPU util ization % of time CPU is running user processes

    To optimize

    z On heavily-loaded computers want utilization 90%-100%

    Throughput # of processes that complete their execution pertime unit

    good

    z depends on process length

    z short processes: maybe 10 per second

    Turnaround time lifespan of process

    z Time from process submission to completion

    z Includes execution time, I/O time, waiting on ready queue

    Waiting time time spent on in the ready queue good

    Response time time it takes from when a request wassubmitted until the first response is produced, not output (fortime-sharing environment)

    good

    5.10 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

    z mpor an or n erac ve processes

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    11/29

    FirstFirst--Come, FirstCome, First--Served (FCFS) SchedulingServed (FCFS) Scheduling

    P1 24

    P2 3

    3

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

    1 2 3

    24 27 300

    Process Burst Time Turnaround

    Time

    Waiting Time

    P1 24 24 0

    P2 3 27 24

    P3 3 30 27

    5.11 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

    verage wa t ng t me: a t 1 + a t 2 + a t 3 =(0 + 24 + 27)/3 = 17

    Average turnaround time: (24+ 27 + 30)/3 = 27

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    12/29

    FCFS Scheduling (Cont.)FCFS Scheduling (Cont.)Suppose that the processes arrive in the order

    P2 , P3 , P1

    The Gantt chart for the schedule is:

    P1P3P2

    Waiting time forP1 = 6; P2 = 0; P3 = 3

    63 300

    urnaroun me or 1 = ; 2 = ; 3 =

    Average waiting time: (6 + 0 + 3)/3 = 3

    Average turnaround time: (30 + 3 + 6)/3 = 13

    Much better than previous case

    Convoy effect delays short process behind long process

    z For example 1 CPU-bound, many I/O-bound processes

    5.12 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

    z Results in lower CPU and device utilization than letting shorter

    processes execute first.

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    13/29

    ShortestShortest--JobJob--First (SJF) SchedulingFirst (SJF) Scheduling

    Associate with each process the length of its next CPU burst

    Two versions:

    z Non-preemptive once CPU given to the process it cannot be

    preemp e un comp e es s urs

    z preemptive if a new process arrives with CPU burst length

    less than remaining time of current executing process, preempt Known as Shortest-Remaining-Time-First (SRTF)

    SJF is optimal

    z Gives minimum average waiting time for a given set of

    processes (why?)

    5.13 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    14/29

    Example of NonExample of Non--Preemptive SJFPreemptive SJF

    Process Arrival Time Burst Time

    P 0.0 7

    P2 2.0 4P3 4.0 1

    4 .

    SJF (non-preemptive)

    1 3 2

    73 160

    4

    8 12

    Arrival Time should be considered -> Wait(Pi) = StartTime(Pi) - ArrivalTime(Pi)

    Average waiting time = Wait(P1) + Wait (P2) + Wait (P3) + Wait (P4) / 4

    5.14 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

    = - - -

    = (0 + 6 + 3 + 7) / 4 = 4

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    15/29

    Example of Preemptive SJFExample of Preemptive SJF

    Process Arrival Time Burst Time

    P 0.0 7

    P2 2.0 4P3 4.0 1

    4 .

    SJF (preemptive)

    P1 P3P2

    42 110

    P4

    5 7

    P2 P1

    16

    Average waiting time = Wait(P1) + Wait(P2) + Wait(P3) + Wait(P4)

    = (11 - 2) + (5 4) + (4 4) + (7 - 5)

    5.15 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

    = =

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    16/29

    Determining Length of Next CPU BurstDetermining Length of Next CPU Burst

    Can only estimate the length

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

    exponential averaging

    burstCPUoflengthactual1. = thn nt

    10,3.

    burstCPUnexttheforvaluepredicted2.

    =+

    1n

    .1t +=:e ne. nnn=

    5.16 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    17/29

    Prediction of the Length of the Next CPU BurstPrediction of the Length of the Next CPU Burst

    5.17 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    18/29

    Examples of Exponential AveragingExamples of Exponential Averaging

    =0

    z n+1 = n

    z Recent history does not count

    =1

    z + = t

    z Only the actual last CPU burst counts

    If we expand the formula, we get:

    = t + 1 - t - 1 +

    +(1 - )j tn -j +

    +(1 - )n +1 0

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

    5.18 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    19/29

    Priority SchedulingPriority Scheduling

    A priority number is associated with each process

    Process with the highest priority is selected

    Priority: usually integer

    In UNIX and textbook, smallest integer highest priority

    Can be:

    z Preemptive

    z Non-preemptive

    SJF is a s ecial case of riorit schedulin . Wh ?

    z Priority is the predicted next CPU burst time

    z Larger the CPU burst the lower the priority

    z It is possible that a low priority process never executes

    Solution: aging

    5.19 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

    ,

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    20/29

    Round Robin (RR)Round Robin (RR)

    Designed for time-sharing systems

    Each process gets a small unit of CPU time q (time quantum),

    - .

    If quantum expires: preempt process, add to end of ready queue If process executes less than q: reschedule

    Ready queue is FIFO

    If there are n processes in the ready queue, each process gets

    1/n of the CPU time in chunks of at most q time units at once.o process wa s more an n- q me un s.

    Ifq= then RR becomes what scheduling algorithm?

    z q FCFS

    z q small q must be large with respect to context switch,otherwise overhead is too high

    5.20 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    21/29

    Example of RR with Time Quantum = 20Example of RR with Time Quantum = 20

    Process Burst Time

    1

    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

    Typically, higher average turnaround than SJF, but betterresponse

    5.21 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    22/29

    Time Quantum and Context Switch TimeTime Quantum and Context Switch Time

    Smaller time quantum increases context switches.

    5.22 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    23/29

    Turnaround Time Varies With The Time QuantumTurnaround Time Varies With The Time Quantum

    Turnaround time varies with the time quantum.

    5.23 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    24/29

    Multi level QueueMulti level Queue

    Ready queue is partitioned into separate queues:

    foreground (interactive)

    ac groun a c

    Each queue has its own scheduling algorithm

    z foreground RR

    z background FCFS

    Scheduling must be done between the queues

    . .,

    from background). Possibility of starvation.

    z Time slice each queue gets a certain amount of CPU time

    which it can schedule amon st its rocesses; i.e., 80% to

    foreground in RR

    z 20% to background in FCFS

    5.24 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    25/29

    Multilevel Queue SchedulingMultilevel Queue Scheduling

    5.25 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    26/29

    Multi level Feedback QueueMulti level Feedback Queue

    A process can move between the various queues; aging can be

    Multilevel-feedback-queue scheduler defined by the followingparameters:

    z scheduling algorithms for each queue

    z method used to determine when to upgrade a processz method used to determine when to demote a process

    z method used to determine which queue a process will enter

    when that process needs service

    5.26 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    27/29

    Example of Multi level Feedback QueueExample of Multi level Feedback Queue

    Three queues:

    z Q RR with time quantum 8 milliseconds

    z Q1 RR time quantum 16 milliseconds

    z Q2 FCFS

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

    z At Q1job is again served FCFS and receives 16 additional

    milliseconds. If it still does not complete, it is preempted and

    moved to ueue Q .

    5.27 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    28/29

    Multilevel Feedback QueuesMultilevel Feedback Queues

    5.28 Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th Edition, Feb 2, 2005

  • 7/28/2019 ch05_CPU_Scheduling.pdf

    29/29

    End of Chapter 5End of Chapter 5