09 CPU Scheduling

download 09 CPU Scheduling

of 21

Transcript of 09 CPU Scheduling

  • 7/27/2019 09 CPU Scheduling

    1/21

    1

    Amity School of Engineering

    B.Tech., CSE(3thSem.)

    Operating Systems (Module-II)Topic: CPU Scheduling

    ANIL SAROLIYA

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

  • 7/27/2019 09 CPU Scheduling

    2/21

    2

    Maximum CPU utilization obtained with multiprogramming

    CPUI/O Burst CycleProcess execution consists of a cycleof CPU execution and

    I/O wait

    Program executionbegins with CPU burst and that is followed by an I/O burst CPU burst distribution

    Basic Concepts

    Alternating Sequence of

    CPU And I/O Bursts

  • 7/27/2019 09 CPU Scheduling

    3/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Histogram of CPU-burst Times

    Burst

    Frequency

    Duration (millisecond)

  • 7/27/2019 09 CPU Scheduling

    4/21

    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

    2. Switches from running to ready state

    3. Switches from waiting to ready

    4. Terminates

    Scheduling under 1 and 4 is non-preemptive

    All other scheduling ispreemptive

    Figure: Process State

    once CPU given to the process it cannot be

    preempted until it completes its CPU burst.

    if a new process arrives with CPU

    burst length less than remainingtime of current executing process,

    preempt. This scheme is known as

    the Shortest-Remaining-Time-First

    (SRTF)

    CPU Scheduler

  • 7/27/2019 09 CPU Scheduling

    5/21

    Dispatchermodule gives control of the CPU to the process selected by the short-

    term scheduler; this involves:

    context switching

    switching to user mode

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

    Dispatch latencytime it takes for the dispatcher to stop one process and start

    another running

    CPU utilizationkeep the CPU as busy as possible

    Throughputnumber of processes that complete their execution per time unit

    Turnaround timeamount of time to execute a particular process. The interval

    from the time of submission of a process to the time of completion

    periods spent waiting to get into memory + waiting in the ready queue

    + executing on the CPU + doing I/O

    Waiting timeamount of time a process has been waiting in the ready queue. It

    is the sum of the periods spent waiting in the ready queue

    Response timeamount of time it takes from when a request was submitted

    until the first response is produced, notoutput (for time-sharing environment)

    Dispatcher

    Scheduling Criteria

  • 7/27/2019 09 CPU Scheduling

    6/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Optimization Criteria

    CPU utilization

    Throughput

    Turnaround time

    Waiting time

    Response time

    Max

    Min

  • 7/27/2019 09 CPU Scheduling

    7/21

    Process Burst Time

    P1 24

    P2 3

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

    The Gantt Chartfor the schedule is:

    Waiting time for P1 = 0; P2 = 24; P3 = 27

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

    Turnaround Time = Burst time + Waiting time

    Process Turnaround Time

    P1 24 + 0 = 24

    P2 3 + 24 = 27

    P3 3 + 27 = 30 Average Turnaround Time: (24 + 27+30)/3 = 27

    P1 P2 P3

    24 27 300

    First-Come, First-Served (FCFS) Scheduling

    Drawback:

    High waiting time

  • 7/27/2019 09 CPU Scheduling

    8/21

    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 FCFS group effectshort process behind long process

    P1P3P2

    63 300

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    FCFS Scheduling (continued)

  • 7/27/2019 09 CPU Scheduling

    9/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Shortest-Job-First (SJF) Scheduling

    Associate with each process the length of its next CPU burst. Use

    these lengths to schedule the process with the shortest time

    Two schemes:

    nonpreemptiveonce CPU given to the process it cannot bepreempted until completes its CPU burst

    preemptive if a new process arrives with CPU burst length

    less than remaining time of current executing process,

    preempt. This scheme is know as theShortest-Remaining-Time-First (SRTF)

    SJF is optimalgives minimum average waiting time for a given

    set of processes

  • 7/27/2019 09 CPU Scheduling

    10/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Example of Non-Preemptive SJF

    Process Arrival Time Burst Time

    P1 0 7

    P2 2 4

    P3 4 1

    P4 5 4 SJF (non-preemptive)

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

    P1 P3 P2

    73 160

    P4

    8 12P2P1 P3 P41 2 4 5 6

  • 7/27/2019 09 CPU Scheduling

    11/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Example of Preemptive SJF

    Process Arrival Time Burst Time

    P1 0.0 7

    P2 2.0 4

    P3 4.0 1

    P4 5.0 4

    SJF (preemptive)

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

    P1 P3P2

    42 110

    P4

    5 7

    P2 P1

    16

  • 7/27/2019 09 CPU Scheduling

    12/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Round Robin (RR) Scheduling

    One of the oldest, simplest, fairest and most widely used algorithms in round robin.

    In this approach, a time slice is defined, which is a particular small unit of time.

    In each time slice, CPU runs the current process until the end of the time slice.

    if (that process is done)

    it is discarded and the next one in the queue is dealt with.

    else

    it is halted and put at the back of the queue, then the next process in line is

    addressed during the next time slice.

  • 7/27/2019 09 CPU Scheduling

    13/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Example: Round Robin (RR) Scheduling

    Letstake four processes that arrive at the same time in this order andthe time quantum is 20

    Process Burst Time

    P1 53

    P2

    17

    P3 68

    P4 24

    The Gantt chart is:

    Typically, higher average turnaround than SJF, but having betterresponse time.

    P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

    0 20 37 57 77 97 117 121 134 154 162

    Waiting time for:

    P1 = 57+24=81; P2 = 20

    P3 = 37+40+17=94; P4 = 57+40=97

    Average Waiting Time = (81+20+94+97)/4 = 73

    Turnaround time=burst time + Waiting time

    P1 = 53+81=134; P2 = 17+20 =37

    P3 = 68+94=162; P4 = 24 + 97=121

    Avg. Turnaround Time = 113.5 = 114 (Approx)

  • 7/27/2019 09 CPU Scheduling

    14/21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Priority Scheduling

    A priority number (integer) is associated with each process

    The CPU is allocated to the process with the highest priority

    (smallest integer highest priority)

    Preemptive

    Non-preemptive

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

    time (really hard to predict the next CPU burst)

    Problem Starvationlow priority processes may never execute

    Solution Agingas time progresses increase the priority of the process

  • 7/27/2019 09 CPU Scheduling

    15/21

    CPU scheduling more complex when multiple CPUs are available

    Load sharingcan be achieve by this scheduling

    Here, we will only concentrate on the systems that contains

    Homogeneous processorswithin a multiprocessor Asymmetric multiprocessing only one processor accesses the

    system data structures, reducing the need for data sharing

    Symmetric multiprocessing every processor is self-scheduled.

    Each processor has its own private ready queue.

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Multiple Processor Scheduling

  • 7/27/2019 09 CPU Scheduling

    16/21

    Calculate the Turnaround (Completion) time, wait time and average of each for

    FCFS, Round Robin and SJF scheduling algorithms and fill the blanks below.

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

    Question

  • 7/27/2019 09 CPU Scheduling

    17/21

    Solution:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

    2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

    3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5

    FCFS schedulingRepresents time sliceof 1 second

    Waiting time for:

    P1 =0, P2 =50, P3 =90, P4 =120, P5 =140

    Average Waiting Time = 80

    Turnaround time (Completion time)

    P1 =50, P2 =90, P3 =120, P4 =140, P5= 150

    Average Turnaround Time = 110

  • 7/27/2019 09 CPU Scheduling

    18/21

    Solution (Continued.)

    Round Robin schedulingRepresents time sliceof 1 second

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 251 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

    1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

    1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1

    2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 1 2 3 1 2 3 1

    2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 1 2 1

    2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1

    Turnaround time (Completion time)

    P1 =150, P2 =140, P3 =120, P4 =90, P5 =50

    Average Turnaround Time = 110

    Waiting timefor

    P1 =100, P2 =100, P3 =90, P4 =70, P5= 40

    Average Waiting Time = 80

  • 7/27/2019 09 CPU Scheduling

    19/21

    Solution (Continued.)

    SJF SchedulingRepresents time sliceof 1 second

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

    3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

    2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    Turnaround time (Completion time)

    P1 =150, P2 =100, P3 =60, P4 =30, P5 =10

    Average Turnaround Time = 70

    Waiting timefor

    P1 =100, P2 =60, P3 =30, P4 =10, P5= 0

    Average Waiting Time = 40

  • 7/27/2019 09 CPU Scheduling

    20/21

    Solution (Continued.)The answer is:

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)

  • 7/27/2019 09 CPU Scheduling

    21/21

    Thanks

    21

    Amity School of Engineering

    B.Tech., CSE-IT(3rdSem.)