Ch (1)6cpu Scheduling

download Ch (1)6cpu Scheduling

of 35

Transcript of Ch (1)6cpu Scheduling

  • 8/3/2019 Ch (1)6cpu Scheduling

    1/35

    1

    CPU Scheduling

    Chapter 5

  • 8/3/2019 Ch (1)6cpu Scheduling

    2/35

    2

    CPU Scheduling

    Basic Concepts

    Scheduling Criteria

    Scheduling Algorithms Multiple-Processor Scheduling

    Thread Scheduling

    UNIX example

  • 8/3/2019 Ch (1)6cpu Scheduling

    3/35

    3

    Basic Concepts

    Maximum CPU utilization obtained

    with multiprogramming

    CPUI/O Burst Cycle Processexecution consists of a cycle of CPU

    execution and I/O wait

    CPU times are generally much

    shorter than I/O times.

  • 8/3/2019 Ch (1)6cpu Scheduling

    4/35

    4

    CPU-I/O Burst Cycle

    Process AProcess B

  • 8/3/2019 Ch (1)6cpu Scheduling

    5/35

    5

    Histogram of CPU-burst Times

  • 8/3/2019 Ch (1)6cpu Scheduling

    6/35

    6

    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

    Reduce multiprogramming or memory consumption

  • 8/3/2019 Ch (1)6cpu Scheduling

    7/35

    7

    CPU Scheduler

    CPU scheduling decisions may take place when aprocess:

    1. Switches from running to waiting state (by sleep).

    2. Switches from running to ready state (by yield).3. Switches from waiting to ready (by an interrupt).

    4. Terminates (by exit).

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

  • 8/3/2019 Ch (1)6cpu Scheduling

    8/35

    8

    Dispatcher

    Dispatcher module gives control of theCPU to the process selected by the short-term scheduler; this involves: switching context

    switching to user mode

    jumping to the proper location in the userprogram to restart that program

    Dispatch latency time it takes for thedispatcher to stop one process and startanother running

  • 8/3/2019 Ch (1)6cpu Scheduling

    9/35

    9

    Scheduling Criteria

    CPU utilization keep the CPU as busy as possible

    Throughput # of processes that complete their execution

    per time unit

    Turnaround time (TAT) The interval of time ofsubmission of a process to the time of completion i.e

    amount of time to execute a particular process

    Waiting time amount of time a process has been waiting

    in the ready queue

    Response time amount of time it takes from when a

    request was submitted until the first response is produced,

    not output (for time-sharing environment)

  • 8/3/2019 Ch (1)6cpu Scheduling

    10/35

    10

    The perfect CPU scheduler

    Minimize latency: response or job completion time

    Maximize throughput: Maximize jobs / time.

    Maximize utilization: keep I/O devices busy.

    Recurring theme with OS scheduling

    Fairness: everyone makes progress, no one starves

  • 8/3/2019 Ch (1)6cpu Scheduling

    11/35

    11

    Algorithms

  • 8/3/2019 Ch (1)6cpu Scheduling

    12/35

    12

    Scheduling Algorithms FCFS

    First-come First-served (FCFS) (FIFO) Jobs are scheduled in order of arrival

    Non-preemptive

    Problem: Average waiting time depends on arrival order

    Troublesome for time-sharing systems

    Convoy effectshort process behind long process

    Advantage: really simple!

  • 8/3/2019 Ch (1)6cpu Scheduling

    13/35

    13

    First Come First Served

    Scheduling Example: Process Burst Time

    P1 24

    P2 3

    P3 3 Suppose that the processes arrive in the order: P1 ,

    P2 , P3

    Suppose that the processes arrive in the order: P2 ,P3 , P1 .

    P1P3P263 300

    P1 P2 P324 27 300

    Waiting time forP1 = 0; P2 = 24; P3 = 27

    Average waiting time: (0 + 24 + 27)/3 = 17

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

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

  • 8/3/2019 Ch (1)6cpu Scheduling

    14/35

    14

    Shortest-Job-First (SJR)

    Scheduling Associate with each process the length of itsnext CPU burst. Use these lengths to schedulethe process with the shortest time

    Two schemes: nonpreemptive once CPU given to the process it

    cannot be preempted until completes its CPU burst

    preemptive if a new process arrives with CPU burstlength less than remaining time of current executingprocess, preempt. This scheme is know as the

    Shortest-Remaining-Time-First (SRTF)

    SJFisoptimal gives minimum average waitingtime for a given set of processes

  • 8/3/2019 Ch (1)6cpu Scheduling

    15/35

    15

    Shortest Job First Scheduling

    Example: Process Arrival Time Burst Time

    P1 0 7

    P2 2 4

    P3 4 1

    P4 5 4 Non preemptive SJF

    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 = 3

    P4s wating time = 7

  • 8/3/2019 Ch (1)6cpu Scheduling

    16/35

    16

    Shortest Job First Scheduling

    Contd Example: Process Arrival Time Burst Time

    P1 0 7

    P2 2 4

    P3 4 1

    P4 5 4

    Preemptive SJF

    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 = 0

    P4s wating time = 2

    P1(5)

    P2(2)

  • 8/3/2019 Ch (1)6cpu Scheduling

    17/35

    17

    Shortest Job First Scheduling

    Contd

    Optimal scheduling

    However, there are no accurate

    estimations to know the length of the next

    CPU burst

  • 8/3/2019 Ch (1)6cpu Scheduling

    18/35

    18

    Optimal for minimizing queueing time, but impossible to

    implement. Tries to predict the process to schedule based on

    previous history.

    Predicting the time the process will use on its next schedule:

    t( n+1 ) = w * t( n ) + ( 1 - w ) * T( n )

    Here: t(n+1) is time of next burst.

    t(n) is time of current burst.T(n) is average of all previous bursts .

    W is a weighting factor emphasizing current or previous

    bursts.

    Shortest Job First Scheduling

    Contd

  • 8/3/2019 Ch (1)6cpu Scheduling

    19/35

    19

    A priority number (integer) is associated with each process

    The CPU is allocated to the process with the highest priority

    (smallest integer| highest priority in Unix but lowest in Java).

    Preemptive

    Non-preemptive

    SJF is a priority scheduling where priority is the predicted next CPU

    burst time.

    Problem | Starvation low priority processes may never execute.

    Solution |Aging as time progresses increase the priority of the

    process.

    Priority Scheduling

  • 8/3/2019 Ch (1)6cpu Scheduling

    20/35

    20

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

    If there are n processes in the ready queue andthe time quantum is q, then each process gets

    1/n of the CPU time in chunks of at most q timeunits at once. No process waits more than (n-1)q time units.

  • 8/3/2019 Ch (1)6cpu Scheduling

    21/35

    21

    time quantum = 20

    Process Burst Time Wait Time

    P1 53 57 +24 = 81

    P2 17 20

    P3

    68 37 + 40 + 17= 94

    P4 24 57 + 40 = 97

    Round Robin Scheduling

    P1 P2 P3 P4 P1 P3 P4 P1 P3 P30 20 37 57 77 97 117 121 134 154 162

    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/3/2019 Ch (1)6cpu Scheduling

    22/35

    22

    Typically, higher average turnaround than SJF, but betterresponse.

    Performance

    q large FCFS

    q small

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

    Round Robin Scheduling

  • 8/3/2019 Ch (1)6cpu Scheduling

    23/35

    23

    Turnaround Time Varies With

    The Time Quantum

    TAT can be improved if most processfinish their next CPU burst in a single

    time quantum.

  • 8/3/2019 Ch (1)6cpu Scheduling

    24/35

    24

    Multilevel Queue Ready queue is partitioned into separate queues:

    EX:

    foreground (interactive)background (batch)

    Each queue has its own scheduling algorithm

    EX foreground RR

    background FCFS

    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;

    EX

    80% to foreground in RR

    20% to background in FCFS

  • 8/3/2019 Ch (1)6cpu Scheduling

    25/35

    2

    5

    Multilevel Queue Scheduling

  • 8/3/2019 Ch (1)6cpu Scheduling

    26/35

    26

    Multi-level Feedback Queues

    Implement multiple ready queues Different queues may be scheduled using different algorithms

    Just like multilevel queue scheduling, but assignments are not static

    Jobs move from queue to queue based on feedback

    Feedback = The behavior of the job, EX does it require the full quantum for computation, or

    does it perform frequent I/O ?

    Need to select parameters for:

    Number of queues Scheduling algorithm within each queue

    When to upgrade and downgrade a job

  • 8/3/2019 Ch (1)6cpu Scheduling

    27/35

    27

    Example of Multilevel Feedback

    Queue Three queues: Q0 RR with time quantum 8 milliseconds

    Q1 RR 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 (RR).If it does not finish in 8 milliseconds, job is moved toqueue Q1.

    At Q1job is again served FCFS and receives 16additional milliseconds (RR). If it still does notcomplete, it is preempted and moved to queue Q2.

    AT Q2job is served FCFS

  • 8/3/2019 Ch (1)6cpu Scheduling

    28/35

    28

    Multilevel Feedback Queues

  • 8/3/2019 Ch (1)6cpu Scheduling

    29/35

    29

    Multiple-Processor Scheduling CPU scheduling more complex when multiple

    CPUs are available

    y Different rules forhomogeneous orheterogeneousprocessors.

    Loadsharing in the distribution of work, such that

    all processors have an equal amount to do. Asymmetric multiprocessing only one processor

    accesses the system data structures, alleviatingthe need for data sharing

    Symmetric multiprocessing (SMP) each

    processor is self-schedulingy Each processor can schedule from a common ready

    queue OR each one can use a separate ready queue.

  • 8/3/2019 Ch (1)6cpu Scheduling

    30/35

    30

    Thread Scheduling

    On operating system that support threads thekernel-threads (notprocesses) that are beingscheduled by the operating system.

    Local Scheduling (process-contention-scopePCS ) How the threads library decides whichthread to put onto an available LWP

    PTHREAD_SCOPE_PROCESS

    Global Scheduling (system-contention-scopeSCS ) How the kernel decides which kernelthread to run next

    PTHREAD_SCOPE_PROCESS

  • 8/3/2019 Ch (1)6cpu Scheduling

    31/35

    31

    Linux Scheduling

    Two algorithms: time-sharing and real-time

    Time-sharing Prioritized credit-based process with most credits is

    scheduled next

    Credit subtracted when timer interrupt occurs

    When credit = 0, another process chosen

    When all processes have credit = 0, recrediting occurs

    Based on factors including priority and history

    Real-time

    Defined by Posix.1b Real time Tasks assigned static priorities. All other taskshave dynamic (changeable) priorities.

  • 8/3/2019 Ch (1)6cpu Scheduling

    32/35

    32

    The Relationship Between Priorities and

    Time-slice length

  • 8/3/2019 Ch (1)6cpu Scheduling

    33/35

  • 8/3/2019 Ch (1)6cpu Scheduling

    34/35

    34

    Conclusion

    Weve looked at a number of different

    scheduling algorithms.

    Which one works the best is application

    dependent.

    General purpose OS will use priority based,

    round robin, preemptive

    Real Time OS will use priority, nopreemption.

  • 8/3/2019 Ch (1)6cpu Scheduling

    35/35

    35

    References

    Some slides from

    Text book slides

    Kelvin Sung - University of Washington,

    Bothell

    Jerry Breecher - WPI

    Einar Vollset - Cornell University