7. Scheduling.ppt

Embed Size (px)

Citation preview

  • 8/14/2019 7. Scheduling.ppt

    1/42

    OSes: 7. CPU Scheduling 1

    Operating Systems

    Objectivesexamine and compare some of the

    common CPU scheduling algorithms

    Certificate Program in Software DevelopmentCSE-TC and CSIM, AITSeptember -- November, 2003

    7. CPU Scheduling(Ch. 5, S&G)

    ch 6 in the 6th ed.

  • 8/14/2019 7. Scheduling.ppt

    2/42

    OSes: 7. CPU Scheduling 2

    Contents

    1. CPU Scheduling

    what it is, burst cycles, criteria

    2. Scheduling Algorithms

    FCFS, SJF, Priority, RR, multilevel

    3. Algorithm Evaluationdeterministic, queueing, simulation

  • 8/14/2019 7. Scheduling.ppt

    3/42

    OSes: 7. CPU Scheduling 3

    1. CPU Scheduling

    One of the main aims of an OS is to

    maximise the utilization of the CPU by

    switching it between processeswhen should the switching occur?

    which processes should be executed next?

  • 8/14/2019 7. Scheduling.ppt

    4/42

    OSes: 7. CPU Scheduling 4

    1.1. What is a Process? Fig. 4.1, p.90

    terminated

    ready

    waiting

    running

    new

    exit

    I/O eventor wait

    dispatch

    interrupt

    event

    completed

  • 8/14/2019 7. Scheduling.ppt

    5/42

    OSes: 7. CPU Scheduling 5

    1.2. Scheduling Opportunities

    When the running process yields the CPU:

    the process enters a wait state

    the process terminates

    When an interrupt occurs:

    the current process is still ready

    process switches from wait state to ready

    Preemptive vs. non-preemptive scheduling

  • 8/14/2019 7. Scheduling.ppt

    6/42

    OSes: 7. CPU Scheduling 6

    1.3. CPU and I/O Burst Cycle

    :

    load

    store

    add

    storeread from file

    wait for I/O

    store

    increment index

    write to file

    wait for I/O

    load

    store

    :

    CPU burst

    I/O burst

    CPU burst

    I/O burst

    CPU burst

    Fig. 5.1, p.124

  • 8/14/2019 7. Scheduling.ppt

    7/42OSes: 7. CPU Scheduling 7

    CPU Burst Duration (ms)

    Fig. 5.2, p.125;VUW CS 305

    02

    48

    1624

    3240

    frequency

    020

    40

    60

    80

    100

    120

    140

    160

  • 8/14/2019 7. Scheduling.ppt

    8/42OSes: 7. CPU Scheduling 8

    1.4. Scheduling Criteria

    CPU utilization

    keep the CPU as busy as possible

    Throughput

    no. of processes completed per time unit

    Turnaround time

    how long it takes to complete a process

    continued

  • 8/14/2019 7. Scheduling.ppt

    9/42OSes: 7. CPU Scheduling 9

    Waiting time

    the total time a process is in the ready queue

    the measure used in chapter 5

    Response time

    time a process takes to start responding

  • 8/14/2019 7. Scheduling.ppt

    10/42OSes: 7. CPU Scheduling 10

    2. Scheduling Algorithms

    2.1. First-Come, First-Served (FCFS)

    2.2. Shortest Job First (SJF)

    2.3. Priority Scheduling

    2.4. Round Robin (RR)

    2.5. Multilevel Queue Scheduling

    2.6. Multilevel Feedback Queue Scheduling

  • 8/14/2019 7. Scheduling.ppt

    11/42OSes: 7. CPU Scheduling 11

    2.1. FCFS Scheduling

    When the CPU is available, assign it to the

    process at the start of the ready queue.

    Simple to implement

    use a FIFO queue

    Non-preemptive

  • 8/14/2019 7. Scheduling.ppt

    12/42

  • 8/14/2019 7. Scheduling.ppt

    13/42

    OSes: 7. CPU Scheduling 13

    Example (v.2)

    Process Burst Time

    P2 3

    P3 3P1 24

    Gantt Chart:

    Average waiting time:

    (6 + 0 + 3)/3 = 3 ms

    P1P2 P3

    0 3 6 30

  • 8/14/2019 7. Scheduling.ppt

    14/42

    OSes: 7. CPU Scheduling 14

    FCFS Features

    May not give the best average waiting time.

    Average times can vary a lot depending onthe order of the processes.

    Convoy effectsmall processes can get stuck behind a big

    process

  • 8/14/2019 7. Scheduling.ppt

    15/42

    OSes: 7. CPU Scheduling 15

    2.2. Shortest Job First Scheduling (SJF)

    When the CPU is available, assign it to the

    process with the smallest next CPU burst

    duration

    better name is shortest next CPU burst

    Can be preemptive or non-preemptive

  • 8/14/2019 7. Scheduling.ppt

    16/42

    OSes: 7. CPU Scheduling 16

    Non-preemptive Example

    Process Burst Time

    P1 6

    P2 8

    P3 7P4 3

    Gantt Chart:

    Average waiting time:

    (3 + 16 + 9 + 0)/4 = 7 ms

    FCFS gives 10.25 ms

    p.131

    P2P4 P1

    0 3 9 24

    P3

    16

  • 8/14/2019 7. Scheduling.ppt

    17/42

    OSes: 7. CPU Scheduling 17

    SJF Features

    Provably optimal

    gives the minimum average waiting time

    Problem: it is usually impossible to know

    the next CPU burst duration for a process

    solution: guess (predict)

  • 8/14/2019 7. Scheduling.ppt

    18/42

    OSes: 7. CPU Scheduling 18

    Predicting the next CPU burst time

    Use the formula:

    Tn+1= w tn+ (1- w) Tn

    Meaning:Tn+1= prediction for the next (n+1

    th) CPU burst

    duration

    tn= known duration of current (nth) CPU burst

    Tn= previous prediction (based on the sequence

    of old titimes)

    w = a weight (0

  • 8/14/2019 7. Scheduling.ppt

    19/42

    OSes: 7. CPU Scheduling 19

    Preemptive SJF

    When a new process arrives, ifit has a

    shorter next CPU burst duration than what

    is left of the currently executing processthenpreempt the current process.

  • 8/14/2019 7. Scheduling.ppt

    20/42

    OSes: 7. CPU Scheduling 20

    Example

    Process Arrival Time Burst Time

    P1 0 8

    P2 1 4

    P3 2 9P4 3 5

    Gantt Chart:

    p.133

    P1P1 P2

    0 1 5 26

    P4

    10

    P3

    17

    continued

  • 8/14/2019 7. Scheduling.ppt

    21/42

    OSes: 7. CPU Scheduling 21

    Average waiting time:

    ( (10-1) + (1-1) + (17-2) + (5-3) )/4

    = 6.5 ms

    Non-preemptive SJF gives 7.75 ms

    start time arrival time

  • 8/14/2019 7. Scheduling.ppt

    22/42

    OSes: 7. CPU Scheduling 22

    2.3. Priority Scheduling

    Associate a priority with each process and

    the CPU is allocated to the process with the

    highest priority.

    FCFS, SJF are special cases.

    Low numbers = high priority.

  • 8/14/2019 7. Scheduling.ppt

    23/42

    OSes: 7. CPU Scheduling 23

    Example

    Process Burst Time Priority

    P1 10 3

    P2 1 1

    P3 2 3P4 1 4

    P5 5 2

    Gantt Chart:

    Average waiting time: 8.2 ms

    p.134

    P3P2 P5

    0 1 6 19

    P1

    16

    P4

    18

  • 8/14/2019 7. Scheduling.ppt

    24/42

    OSes: 7. CPU Scheduling 24

    Features

    Internal/external priorities.

    Preemptive or non-preemptive.

    How to avoid starvation?

    aging

  • 8/14/2019 7. Scheduling.ppt

    25/42

    OSes: 7. CPU Scheduling 25

    2.4. Round Robin Scheduling (RR)

    A small unit of time (a time quantum, a time

    slice) is defined

    typically 10 - 100 ms

    The ready queue is treated as a circular queue.

    The CPU scheduler goes around the queue

    giving each process one time quantum

    preemptive

  • 8/14/2019 7. Scheduling.ppt

    26/42

    OSes: 7. CPU Scheduling 26

    Example

    Time quantum = 4 ms. At time 0.

    Process Burst Time

    P1 24

    P2 3

    P3 3

    Gantt Chart:

    Average waiting time: 17/3 = 5.67 ms

    p.135

    P1P1 P20 4 7 30

    P1

    22

    P1

    2610 14 18

    P3 P1 P1

  • 8/14/2019 7. Scheduling.ppt

    27/42

    OSes: 7. CPU Scheduling 27

    RR Features

    Average waiting time can be quite long.

    Context switching is an important overhead

    when the time quantum is small.

    continued

  • 8/14/2019 7. Scheduling.ppt

    28/42

    OSes: 7. CPU Scheduling 28

    Average turnaround time of a set ofprocesses does not necessarily improve as

    the time quantum size increase:

    Fig. 5.5, p.137

    9.5

    10

    10.5

    11

    11.5

    12

    12.5

    1 2 3 4 5 6 7

    Process Time

    P1 6

    P2 3

    P3 1P4 7

    Time Quantum

    Average

    Turnar

    oundTime

  • 8/14/2019 7. Scheduling.ppt

    29/42

    OSes: 7. CPU Scheduling 29

    2.5. Multilevel Queue SchedulingFig. 5.6, p.138

    system processes

    interactive processes

    interactive editing processes

    batch processes

    student processes

    highest priority

    lowest prioritycontinued

  • 8/14/2019 7. Scheduling.ppt

    30/42

    OSes: 7. CPU Scheduling 30

    Scheduling between queues:

    fixed priority preemptive scheduling

    varying time slices between the queues

  • 8/14/2019 7. Scheduling.ppt

    31/42

    OSes: 7. CPU Scheduling 31

    2.6. Multilevel Feedback Queue Scheduling

    Allow a process to

    move between queues

    priority sinks whenquantum exceeded

    greater discrimination

    against longer jobs

    better response for

    shorter jobs

    Q0

    Q1> Q0

    Q2> Q1

    FCFS

    Fig. 5.7, p.140;VUW CS 305

  • 8/14/2019 7. Scheduling.ppt

    32/42

    OSes: 7. CPU Scheduling 32

    3. Algorithm Evaluation

    3.1. Deterministic Modelling

    3.2. Queueing Models

    3.3. Simulation

  • 8/14/2019 7. Scheduling.ppt

    33/42

    OSes: 7. CPU Scheduling 33

    Common Issues

    Model/simulation is based on some pattern

    of use of a real machine

    e.g. students use of bazooka: lots ofinteractive, small jobs, and a few large ones

    How to represent the changesin usage:changes in problems, programs, users

  • 8/14/2019 7. Scheduling.ppt

    34/42

    OSes: 7. CPU Scheduling 34

    3.1. Deterministic Modelling

    Take a given workload and calculate the

    performance of each scheduling algorithm:FCFS, SJF, and RR (quantum = 10 ms)

  • 8/14/2019 7. Scheduling.ppt

    35/42

    OSes: 7. CPU Scheduling 35

    Example

    At time 0.

    Process Burst Time

    P1 10

    P2 29

    P3 3

    P4 7

    P5 12

    p.145

    continued

  • 8/14/2019 7. Scheduling.ppt

    36/42

    OSes: 7. CPU Scheduling 36

    Gantt Charts and Times

    1. FCFS:

    Average waiting time:

    (0 + 10 + 39 + 42 + 49)/5= 28 ms

    P1 P2

    0 10 39 61

    P5

    42 49

    P3 P4

    continued

  • 8/14/2019 7. Scheduling.ppt

    37/42

    OSes: 7. CPU Scheduling 37

    2. Non-preemptive SJF:

    Average waiting time:

    (10 + 32 + 0 + 3 + 20)/5

    = 13 ms

    continued

    P3 P4

    0 3 10 61

    P2

    20 32

    P1 P5

  • 8/14/2019 7. Scheduling.ppt

    38/42

    OSes: 7. CPU Scheduling 38

    3. RR:

    Average waiting time:

    (0 + 32 + 20 + 23 + 40)/5

    = 23 ms

    P1 P2

    0 10 20 23 30

    P3 P4

    50 52

    P2 P5

    61

    P2P5

    40

  • 8/14/2019 7. Scheduling.ppt

    39/42

    OSes: 7. CPU Scheduling 39

    Results

    For this mix of processes and CPU burst

    durations:

    SJF 13 ms

    RR 23 ms

    FCFS 28 ms

  • 8/14/2019 7. Scheduling.ppt

    40/42

  • 8/14/2019 7. Scheduling.ppt

    41/42

    OSes: 7. CPU Scheduling 41

    3.2. Queueing Models

    Usually the process mix varies greatly in an

    OS, but it may still be possible to determine

    statistical distributions for the CPU and I/Obursts.

    The mathematics is difficult, and onlyapplies to limited cases.

  • 8/14/2019 7. Scheduling.ppt

    42/42

    3.3. Simulations

    Often driven by random number generators

    to model CPU burst durations, process

    arrival, departure, etc.

    Trace tapes

    records of actual events in a real system, whichcan be used to drive simulations