Preemptive and Non Preemptive Scheduling

Embed Size (px)

Citation preview

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    1/34

    Chapter 5 CPU Scheduling

    Bernard Chen

    Spring 2007

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    2/34

    Outline

    Basic Concepts Scheduling Criteria

    Scheduling Algorithms

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    3/34

    Basic Concepts The objective of multiprogramming is to

    have some process running at all times

    Scheduling of this kind is a fundamentaloperating-system function

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    4/34

    CPU-I/O Burst Cycle Process execution consists of a cycle of

    CPU execution and I/O request

    Process execution begins with a CPUburst and followed by an I/O burst

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    5/34

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    6/34

    CPU Scheduler Whenever the CPU becomes idle, theOS must select one of the processes inthe ready queue to be executed

    The selection process is carried out bythe short-term scheduler

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    7/34

    Diagram of Process State

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    8/34

    Preemptive scheduling vs.

    non-preemptive schedulingCPU scheduling decisions may take place

    when a process:

    1. Switches from running to waiting state

    2. Terminates3. Switches from waiting to ready

    4. Switches from running to ready state

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    9/34

    Preemptive scheduling vs.

    non-preemptive scheduling When scheduling takes place only under

    circumstances 1 and 2, we say that the

    scheduling scheme is non-preemptive;otherwise, its called preemptive

    Under non-preemptive scheduling, once the

    CPU has been allocated to a process, theprocess keep the CPU until it release the CPUeither by terminating or by switching towaiting state. (Windows 95 and earlier)

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    10/34

    Preemptive scheduling vs.

    non-preemptive scheduling Preemptive scheduling incurs a cost

    associated with access to share data.

    Consider the case of two processes that sharea data. It is preemptive so that while one

    process is updating the data, the secondprocess then tries to read the data, which arein an inconsistent state. (Ch6)

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    11/34

    Dispatcher Dispatcher module gives control of the CPU to

    the process selected by the short-termscheduler

    It should work as fast as possible, since it isinvoked during every process switch

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

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    12/34

    Scheduling CriteriaCPU utilization keep the CPU as busy as possible (from 0% to

    100%)

    Throughput # of processes that complete their execution pertime unit

    Turnaround time amount of time to execute a particularProcess

    Waiting time amount of time a process has been waiting in theready queue

    Response time amount of time it takes from when a request wassubmitted until the first response is produced

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    13/34

    Optimization Criteria Max CPU utilization

    Max throughput Min turnaround time

    Min waiting time

    Min Response time

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    14/34

    Scheduling Algorithems First Come First Serve Scheduling

    Shortest Job First Scheduling Priority Scheduling

    Round-Robin Scheduling

    Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    15/34

    First Come First Serve

    Scheduling (FCFS)Process Burst time

    P1 24

    P2 3

    P2 3

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    16/34

    First Come First Serve

    Scheduling The average of waiting time in this

    policy is usually quite long

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

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

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    17/34

    First Come First Serve

    Scheduling Suppose we change the order of

    arriving job P2, P3, P1

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    18/34

    First Come First Serve

    Scheduling Consider if we have a CPU-bound process and

    many I/O-bound processes

    There is a convoy effect as all the otherprocesses waiting for one of the big processto get off the CPU

    FCFS scheduling algorithm is non-preemptive

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    19/34

    Short job first scheduling (SJF) This algorithm associates with each

    process the length of the processes

    next CPU burst

    If there is a tie, FCFS is used

    In other words, this algorithm can bealso regard as shortest-next-cpu-burstalgorithm

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    20/34

    Short job first scheduling SJF is optimal gives minimum average

    waiting time for a given set ofprocesses

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    21/34

    ExampleProcesses Burst time

    P1 6

    P2 8

    P3 7

    P4 3

    FCFS average waiting time: (0+6+14+21)/4=10.25

    SJF average waiting time: (3+16+9+0)/4=7

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    22/34

    Short job first schedulingTwo schemes:Non-preemptive once CPU given to the

    process it cannot be preempted untilcompletes its CPU burst

    Preemptive if a new process arrives with CPUburst length less than remaining time of

    current executing process, preempt. Thisscheme is know as the Shortest-Remaining-Time-First (SRTF)

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    23/34

    Short job first scheduling-

    Non-preemptive

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    24/34

    Short job first scheduling-

    Preemptive

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    25/34

    Priority SchedulingA priority number (integer) is associated with each

    processThe CPU is allocated to the process with the highest

    priority(smallest integer highest priority) Preemptive Non-preemptive

    SJF is a special priority scheduling where priority is thepredicted next CPU burst time, so that it can decidethe priority

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    26/34

    Priority SchedulingProcesses Burst time Priority Arrival time

    P1 10 3

    P2 1 1

    P3 2 4

    P4 1 5

    P5 5 2The average waiting

    time=(6+0+16+18+1)/5=8.2

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    27/34

    Priority SchedulingProcesses Burst time Priority Arrival time

    P1 10 3 0.0

    P2 1 1 1.0P3 2 4 2.0

    P4 1 5 3.0

    P5 5 2 4.0

    Gantt chart for both preemptive and non-preemptive, also waiting time

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    28/34

    Priority SchedulingProblem : Starvation low priority

    processes may never execute

    Solution : Aging as time progressesincrease the priority of the process

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    29/34

    Round-Robin Scheduling The Round-Robin is designed especially

    for time sharing systems.

    It is similar FCFS but add preemptionconcept

    A small unit of time, called time

    quantum, is defined

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    30/34

    Round-Robin Scheduling Each process gets a small unit of CPU

    time (time quantum), usually 10-100

    milliseconds. After this time haselapsed, the process is preempted andadded to the end of the ready queue.

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    31/34

    Round-Robin Scheduling

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    32/34

    Round-Robin Scheduling If there are n processes in the ready

    queue and the time quantum is q, then

    each process gets 1/n of the CPU timein chunks of at most q time units atonce. No process waits more than

    (n-1)q time units.

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    33/34

    Round-Robin SchedulingPerformance

    q large => FIFO

    qsmall => qmust be large withrespect to context switch, otherwiseoverhead is too high

    Typically, higher average turnaroundthan SJF, but better response

  • 8/6/2019 Preemptive and Non Preemptive Scheduling

    34/34

    Round-Robin Scheduling