Slides 4 Scheduling

Embed Size (px)

Citation preview

  • 8/3/2019 Slides 4 Scheduling

    1/48

    OperatingSystems

    Lus Moura e SilvaEmail: [email protected] de Eng. InformticaUniversidade de Coimbra

    4. CPU Scheduling

  • 8/3/2019 Slides 4 Scheduling

    2/48

    2

    Disclaimer

    This slides and notes are heavily based on the companion material of[Silberschatz05].The original material can be found at: http://codex.cs.yale.edu/avi/os-book/os7/slide-dir/index.html

    In some cases, material from [Stallings04] may also be used. Theoriginal material can be found at:

    http://williamstallings.com/OS/OS5e.html

    The respective copyrights belong to their owners.

  • 8/3/2019 Slides 4 Scheduling

    3/48

    3

    System Queues

  • 8/3/2019 Slides 4 Scheduling

    4/48

    4

    Preemptive vs. Non-Preemptive

    Non-Preemptive SchedulingOnce the CPU is allocated to a process, the process keepsthe CPU until it terminates or it blocks in a I/O operationsand switches to the waiting state (e.g. in Windows 98).

    Preemptive SchedulingThe operating system may decide and is able to take theCPU away from a running process

    (e.g. in WindowsXP, Linux)

  • 8/3/2019 Slides 4 Scheduling

    5/48

    5

    Scheduling Criteria

    CPU utilization Keep the CPU as busy as possible

    Throughput Number of processes that complete their execution per time unit

    Turnaround timeAmount of time to complete a particular process

    Waiting timeAmount of time a process has been waiting in the ready queue

    Response timeAmount of time it takes from when a request was submitted until

    the first response is produced

  • 8/3/2019 Slides 4 Scheduling

    6/48

    6

    Optimization Criteria

    As seen before, it all depends on what type of system weare aiming at

    But, we would like: Maximum CPU utilization Maximum throughput Minimum turnaround time Minimum waiting time Minimum response time

    Thats a multi-objective function, not

    really possible

  • 8/3/2019 Slides 4 Scheduling

    7/48

    7

    Why can we optimize?

    CPU-burst TimesCPU-IO Cycle

  • 8/3/2019 Slides 4 Scheduling

    8/48

    8

    Scheduling Algorithms

    FCFSSJF (or SPN)SRTFPRIORITYROUND-ROBIN

    HRRNMULTILEVEL FEEDBACK

  • 8/3/2019 Slides 4 Scheduling

    9/48

    9

    Algorithm 1: First-Come First-Served (FCFS)

    Process Burst Time

    P1 24

    P2 3

    P3 3

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

    Waiting time for P1 = 0; P2 = 24; P3= 27 Average waiting time: (0 + 24 + 27)/3 = 17 This is a non-preemptive algorithm

    P1 P

    2 P

    3

    24 27 300

  • 8/3/2019 Slides 4 Scheduling

    10/48

    10

    First-Come First-Served (FCFS) 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 I/O bound vs. CPU bound problem

    P1P

    3P

    2

    63 300

  • 8/3/2019 Slides 4 Scheduling

    11/48

    11

    Algorithm 2: Shortest-Job-First (SJF)

    Associate with each process the length of its next CPUburst. Use these lengths to schedule the process with theshortest time. How do you determine that? Can you guess the future?Also known as Shortest-Job-Next (SJN), which is more correct.

    Two schemes: SJF (non-premptive): once CPU given to the process it cannot be

    preempted until completes its CPU burst.

    SRTF (preemptive): if a new process arrives with CPU burstlength less than remaining time of current executing process,preempt. This scheme is know as the Shortest-Remaining-TimeFirst (SRTF).

  • 8/3/2019 Slides 4 Scheduling

    12/48

    12

    (Non-Preemptive) Shortest-Job-First (SJF)

    Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P

    4 5.0 4

    SJF (non-preemptive)

    Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Can be proved optimal (minimizes waiting time) for a set

    of processes

    Used frequently in long-term scheduling

    P1 P

    3 P

    2

    73 160P

    4

    8 12

  • 8/3/2019 Slides 4 Scheduling

    13/48

    13

    (Preemptive) Shortest-Remaining-Time-First (SRTF)

    Process Arrival Time Burst Time P1 0.0 7 P

    2 2.0 4

    P3 4.0 1 P

    4 5.0 4

    SRTF (preemptive)

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

    P1 P

    3P

    2

    42 110P

    4

    5 7P

    2 P

    1

    16

  • 8/3/2019 Slides 4 Scheduling

    14/48

    14

    How to predict the next CPU burst time?

    Based on an exponential moving average based on thepast.

  • 8/3/2019 Slides 4 Scheduling

    15/48

    15

    Priority Scheduling

    A priority number is associated with each process The CPU is allocated to the process with the highest

    priority: Preemptive Non-preemptive

    SJF can be seen as priority scheduling where priority isthe predicted next CPU burst time

    Problem Starvation

    Low priority processes may never execute Solution Aging

    As time progresses increase the priority of the process

  • 8/3/2019 Slides 4 Scheduling

    16/48

    16

    Starvation @MIT

    IBM 7094

    When the system managers shutdown

    the IBM 7094 at MIT in 1973, they founda low-priority process that had been

    submitted in 1967 and had not yet run

  • 8/3/2019 Slides 4 Scheduling

    17/48

    17

    Priority-Based Scheduler

  • 8/3/2019 Slides 4 Scheduling

    18/48

    18

    Algorithm 3: Round-Robin (RR)

    Each process gets a small unit of CPU time (timequantum), usually 10-100 milliseconds. After this timehas elapsed, the process is preempted and added to theend of the ready queue. New processes are added to the tail of the ready queue

    If there are nprocesses in the ready queue and the timequantum is q, then each process gets 1/nof the CPU timein chunks of at most qtime units at once. No process waits more than (n-1)qtime units.

    Performance qlarge FCFS qsmall qmust be large with respect to context switch,

    otherwise overhead is too high. If so, having N processes is likehaving a machine running at 1/N of the speed.

  • 8/3/2019 Slides 4 Scheduling

    19/48

    19

    RR with a quantum of 20

    Process Burst Time P1 53 P

    2

    17 P

    3 68

    P4

    24 The Gantt chart is:

    Typically, higher average turnaround than SJF, but betterresponse time

  • 8/3/2019 Slides 4 Scheduling

    20/48

    20

    Timeline charts are relevant!

  • 8/3/2019 Slides 4 Scheduling

    21/48

    21

    Time Quantum and Context Switches

  • 8/3/2019 Slides 4 Scheduling

    22/48

    22

    Turnaround Time vs. Time Quantum

    Turnaround time depends on time quantum but therelation is not direct.

    In general, turnaround time can be improved if mostprocesses finish on a time quantum.

  • 8/3/2019 Slides 4 Scheduling

    23/48

    23

    HRRN: Highest Response Ratio Next

    Choose next process with the highest ratio (R):

    This algorithm accounts for the age of the process. Shorted jobs are favored (smaller denominator) But aging without service (waiting time) increases R.

    R = time spent waiting + expected service time

    expected service time

  • 8/3/2019 Slides 4 Scheduling

    24/48

    24

    Multilevel Queue Scheduling

  • 8/3/2019 Slides 4 Scheduling

    25/48

    25

    Multilevel Queue Scheduling (2)

    Ready queue is partitioned into separate queues. E.g. Foreground (interactive) Background (batch)

    Each queue has its own scheduling algorithm: Foreground RR Background FCFS

    Scheduling must be done between the queues. Fixed priority scheduling (i.e., serve all processes from

    foreground; then from background). Possibility of starvation.

    Time slice: each queue gets a certain amount of CPU time which itcan schedule amongst its processes; i.e., 80% to foreground inRR; 20% to background in FCFS

  • 8/3/2019 Slides 4 Scheduling

    26/48

    26

    Multilevel Feedback Queue Scheduling

    Processes can move among queues!Aging can be implemented this way

    Multilevel-feedback-queue scheduler defined by thefollowing 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

  • 8/3/2019 Slides 4 Scheduling

    27/48

    27

    Example of a scheduler with three queues

    Note: queue serving is priority-based!

  • 8/3/2019 Slides 4 Scheduling

    28/48

    28

    Example of a scheduler with three queues (2)

    Three queues: Q0 time quantum 8 milliseconds Q1 time quantum 16 milliseconds Q2 FCFS

    Scheduling A new job enters queue Q0which is servedFCFS. 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.

    Gives high-priority to any process with CPU bursts of 8ms or less. Grabs CPU, do processing, move along to next IO cycle

    Process with bursts of >8ms and

  • 8/3/2019 Slides 4 Scheduling

    29/48

    29

    Characteristics of Scheduling Algorithms

    from [Stallings05]

  • 8/3/2019 Slides 4 Scheduling

    30/48

    OperatingSystems

    Lus Moura e SilvaEmail: [email protected] de Eng. InformticaUniversidade de Coimbra

    6. Scheduling: examplesfrom Stallings

  • 8/3/2019 Slides 4 Scheduling

    31/48

    31

    Example

    This example is taken from [Stallings04]

    Homework Calculate Finish-Time, Waiting-Time, Turnaround Time for each

    process

    CalculateAverage Waiting Time andAverage Turnaround Time

  • 8/3/2019 Slides 4 Scheduling

    32/48

    32

    FCFS

    Short process may have to wait a very long time before it can execute Favors CPU-bound processes I/O processes have to wait until CPU-bound process completes

  • 8/3/2019 Slides 4 Scheduling

    33/48

    33

    Round-Robin (q=1)

    Uses preemption based on a clock

    When an interrupt occurs, the running process is placed in the readyqueue

    Next ready job is selected Known as time slicing

  • 8/3/2019 Slides 4 Scheduling

    34/48

    34

    SPN / SJF

    Non-preemptive policy Process with shortest expected processing

    time is selected next

    Short process jumps ahead of longer processes

  • 8/3/2019 Slides 4 Scheduling

    35/48

    35

    SRTF

    Preemptive version Must estimate processing time

  • 8/3/2019 Slides 4 Scheduling

    36/48

    36

    Multilevel Feedback

    Penalize jobs that are longer

  • 8/3/2019 Slides 4 Scheduling

    37/48

    37

    Comparison of Scheduling Algorithms

  • 8/3/2019 Slides 4 Scheduling

    38/48

    38

    Comparison of Scheduling Algorithms (2)

  • 8/3/2019 Slides 4 Scheduling

    39/48

    39

    Exercise_A: SCHEDULING ALGORITHMS

    Considere o conjunto de 5 processos (P1...P5) com o seguinte comportamento:

    3.1- Represente de uma forma grfica o escalonamento dos processos para os seguintes algoritmos:

    (i) Round-Robin (quantum = 3)(ii) SRT(iii) HRRN(iv) Multilevel Feedback (3 filas: Q0,Q1,Q2 com os algoritmos RR(1), RR(2), FCFS)

    3.2- Para cada um dos algoritmos apresente uma tabela com os seguintes valores:

    (a) Finish Time; (b) Turnaround Time; (c) Waiting Time.

  • 8/3/2019 Slides 4 Scheduling

    40/48

    40

    Exercise_B: SCHEDULING ALGORITHMS

    Considere o conjunto de 5 processos (P1...P5) com o seguinte comportamento:

    3.1- Represente de uma forma grfica o escalonamento dos processos para os seguintes algoritmos:

    (i) Round-Robin (quantum = 3)(ii) SRT(iii) HRRN(iv) Multilevel Feedback (3 filas: Q0,Q1,Q2 com os algoritmos RR(1), RR(2), FCFS)

    3.2- Para cada um dos algoritmos apresente uma tabela com os seguintes valores:

    (a) Finish Time; (b) Turnaround Time; (c) Waiting Time.

  • 8/3/2019 Slides 4 Scheduling

    41/48

    41

    Exercise_C: SCHEDULING ALGORITHMS

    Considere o conjunto de 5 processos (P1...P5) com o seguinte comportamento:

    3.1- Represente de uma forma grfica o escalonamento dos processos para os seguintes algoritmos:

    (i) Round-Robin (quantum = 3)(ii) SRT(iii) HRRN(iv) Multilevel Feedback (3 filas: Q0,Q1,Q2 com os algoritmos RR(1), RR(2), FCFS)

    3.2- Para cada um dos algoritmos apresente uma tabela com os seguintes valores:

    (a) Finish Time; (b) Turnaround Time; (c) Waiting Time.

  • 8/3/2019 Slides 4 Scheduling

    42/48

    OperatingSystems

    Lus Moura e SilvaEmail: [email protected] de Eng. InformticaUniversidade de Coimbra

    6. CPU Scheduling: O.S.

  • 8/3/2019 Slides 4 Scheduling

    43/48

    43

    Traditional UNIX Scheduling

    Multilevel feedback using Round-Robin within each queue

    If a process does not block orcomplete within a second, it ispreempted

    Priorities are recomputed onceper second

    Base priority divides all processesinto fixed bands of priority levels

    Process Px CPUx(i) = CPUx (i-1) / 2 Px(i) = Basex + (CPUx(i) / 2) + nicex The priority of each process is recomputed once per second.

  • 8/3/2019 Slides 4 Scheduling

    44/48

    44

    Traditional Unix Scheduling

    Base priority=60

    CPU count:

    incremented very

    second

  • 8/3/2019 Slides 4 Scheduling

    45/48

    45

    Windows XP Scheduling

    Priority-based Preemptive Scheduling The highest priority queue is always served first 32 levels of priorities, two classes of threads:

    Real-time, having a priority from 16 to 31 Variable, having a priority from 1 to 15

    The priorities from all threads vary except if they are in aREAL_TIME_PRIORITY_CLASS

    Within each class, there are relative priorities Base priority = normal (by default) When the quantum expires, the priority is lowered down After a wait operation, the priority is increased

  • 8/3/2019 Slides 4 Scheduling

    46/48

    46

    Linux 2.6 Scheduling

    Preemptive Priority-Based Scheduler Two separate priority ranges:

    Real-time (0-99) and Nice (100-140) Two algorithms: time-sharing and real-time

    Scheduling (Time-Sharing Algorithm Fair preemptive):Active Array and Expired Array Scheduling is done from the active array. The scheduler chooses the

    task from the active array for execution. It runs until its time slice isgone, or it is blocked.

    Whenever a task has used all its time slice, its moved to the expiredarray. Recalculation of its priority is done at that time.

    Execution goes on until the active array is empty. When the activearray is empty, its switched with the expired array.

  • 8/3/2019 Slides 4 Scheduling

    47/48

    47

    Linux 2.6 Scheduling (2)

    Higher priority tasks areassigned a longer timequantum

    Two arrays are used forscheduling

  • 8/3/2019 Slides 4 Scheduling

    48/48

    48

    Reference

    Chapter 5: CPU Scheduling 5.1, 5.2, 5.3, 5.6, 5.8