OS Lecture#4 - Process Scheduling

Embed Size (px)

Citation preview

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    1/50

    Operating SystemsProf. Navneet Goyal Department of Computer Science & Information SystemsBITS, Pilani

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    2/50

    Topics for TodayProcess SchedulingCriteria & Objectives

    Types of SchedulingLong termMedium termShort term

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    3/50

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    4/50

    CPU SchedulerSelects from among the processes in memory that are ready to execute, and allocates theCPU to one of themCPU scheduling decisions may take placewhen a process:1. Switches from running to waiting state2. Switches from running to ready state3. Switches from waiting to ready

    4. TerminatesScheduling under 1 and 4 is non-preemptiveA ll other scheduling is preemptive

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    5/50

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    6/50

    P reemptive & Non-preemptiveScheduling

    PreemptiveCurrently running process may beinterrupted and moved to the ready stateby the OSWindows 95 introduced preemptiveschedulingUsed in all subsequent versions of windowsMac OS X uses it

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    7/50

    D ispatcherD ispatcher module gives control of theCPU to the process selected by the short-term scheduler; this involves:

    switching contextswitching to user modejumping to the proper location in the userprogram to restart that program (IP)

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

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    8/50

    Criteria & ObjectivesCPU utilization keep the CPU as busy as

    possible (40 lightly loaded, 90 heavily loaded)Throughput # of processes that completetheir execution per time unitTurnaround time amount of time to executea particular process (total time spent on thesystem)Waiting time amount of time a process hasbeen waiting in the ready queueResponse time amount of time it takes fromwhen a request was submitted until the firstresponse is produced, not output (for time-sharing environment)

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    9/50

    OptimizationC

    riteriaMax CPU utilizationMax throughputMin turnaround timeMin waiting timeMin response time

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    10/50

    Types of SchedulingLong-term scheduling

    the decision to add to pool of processesto be executed

    Mid-term schedulingthe decision to add to the number of processes that are partially or fully inmemory

    Short-term schedulingdecision as to which available processwill be executed

    I/O schedulingdecision as to which processs pendingrequest shall be handled by an availableI/O device

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    11/50

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    12/50

    Long- Term SchedulingD etermines which programs are admitted

    to the system for processingControls the degree of multiprogrammingMore processes, smaller percentage of time each process is executed

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    13/50

    Medium- Term SchedulingPart of the swapping function

    Based on the need to manage the degreeof multiprogramming

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    14/50

    Short- Term SchedulingK nown as the dispatcher

    Executes most frequently Invoked when an event occursClock interruptsI/O interrupts

    Operating system calls

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    15/50

    P rocess Scheduling Example

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    16/50

    F irst- Come- F irst-Served ( F C F S)0 5 10 15 20

    1

    2

    3

    4

    5

    Each process joins the Ready queueWhen the current process ceases toexecute, the oldest process in the Ready queue is selected

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    17/50

    F irst- C ome- F irst-Served ( F C F S)Short jobs suffer penalty

    Favors CPU-bound processes over I/Obound processesI/O processes have to wait until CPU-bound process completes

    May result in inefficient use of both theprocessor & I/O devicesReasons?

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    18/50

    F irst- C ome- F irst-Served ( F C F S)Not an attractive alternative for single

    processor systems. Must be combinedwith priority scheme

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    19/50

    Round RobinReduces the penalty that short hobssuffer with FCFS

    Uses clock based preemptionA ready job is selected from the queue ona FCFS basisA lso called as Time Slicing

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    20/50

    Round- Robin (q=1)0 5 10 15 20

    1

    2

    3

    4

    5

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    21/50

    Round- Robin (q=4)D O IT YOURSELF!!

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    22/50

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    23/50

    Round- RobinD rawback: relative treatment of CPU-bound

    & I/O bound processesReason?Virtual Round Robin (VRR) avoids thisunfairness

    SO what is VRR?

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    24/50

    Virtual Round- RobinWhen a running process is timed out, it joinsthe ready queue (FCFS queue)When a process is blocked, it joins an I/OqueueThis is normal!New queue: A uxiliary FCFS queueProcesses are moved to this queue after beingreleased from an I/O block When dispatching decision is made, processesin the auxiliary queue are given preferenceover those in main ready queueFigure

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    25/50

    Virtual Round- RobinProcess selected from the auxiliary queueruns for q-q1 timeq1 is the time it ran the last time whenselected from the ready queueWHY q q1??

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    26/50

    Shortest P rocess Next (S P N or SJ F )0 5 10 15 20

    1

    2

    3

    4

    5

    Non-preemptive policy Process with shortest expectedprocessing time is selected nextShort process jumps ahead of longerprocesses

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    27/50

    Shortest P rocess NextPredictability of longer processes is

    reducedIf estimated time for process not correct,the operating system may abort itPossibility of starvation for longer

    processesReduces bias in favor of longer processes,it is still not desirable because of lack of preemptions

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    28/50

    Shortest Remaining T ime

    Preemptive version of shortest processnext policy Must estimate processing time

    0 5 10 15 20

    1

    2

    3

    4

    5

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    29/50

    Analysis of Scheduling AlgorithmsPredictability of longer processes isreducedIf estimated time for process not correct,the operating system may abort itPossibility of starvation for longer

    processes

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    30/50

    Normalized Turnaround T imeTA T ( T r ) does not tell us anything aboutthe breakup between the service time ( T

    s)

    and the waiting timeNormalized T A T (NT A T) = T r / T sIndicates the relative delay experienced

    by a processMinimum possible value for NT A T isIncreasing value corresponds todecreasing level of service

    1.0

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    31/50

    Normalized Turnaround T imeTA T = 1+1 = 2

    NTA

    T = 2/1 = 2TA T = 1+9 = 10NT A T = 10/9 = 1.1 (good service)By looking at T A T, we get no idea about

    the quality of service!

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    32/50

    Some F inal RemarksFCFS and RR (including VRR) do notrequire the information about service

    time!!SJF & SRT both need to know or at leastestimate the processing time of eachprocess

    Exponential SmoothingFormula and discussion

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    33/50

    H ighest Response Ratio Next ( HRR N)NT A T is a better metric than T A TFor each individual process we would like toreduce this ratioMinimize its average value for all processesConsider

    R = (w+s)/s, whereR is response ratio

    w = time spent waiting for processors = expected service timeMin (R) = 1.0 (when w=0)

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    34/50

    H ighest Response Ratio Next ( HRR N)When current process completes or isblocked, choose the ready process with max.value of R HRRN is attractive as it incorporates age of aprocess (in terms of w)Shorter jobs are favored (small s)A ging without service (increase in w) will

    increase R Like SJF & SRT, HRRN also needs toestimate expected service timeD O the running example with HRRN

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    35/50

    HRR N Example0 5 10 15 20

    1

    2

    3

    4

    5

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    36/50

    F eedback SchedulingIf service time is not known, we can notuse any of the following :

    SJF or SPNSRTHRRN

    Focus on time spent on processorA nother way of penalizing that have beenrunning longer and giving preference toshorter jobs

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    37/50

    F eedback SchedulingQ uantum based preemptionD

    ynamic priority mechanismWith each preemption, a process isdemoted to the next lower-priority queueA short process will complete quickly

    without slipping very far down thehierarchy of ready queuesA longer process will gradually drift down

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    38/50

    F eedback SchedulingNewer shorter processes are favored overolder, longer processesIn each ready queue, except the lowestpriority queue, FCFS is usedLowest priority queue is treated in RR

    fashionScheme is called as multi-level feedback scheme

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    39/50

    F eedback Scheduling: Example q=10 5 10 15 20

    1

    2

    3

    4

    5

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    40/50

    Feedback Scheduling: Example q=2

    i

    0 5 10 15 20

    1

    2

    3

    4

    5

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    41/50

    C omparisonTA T

    NTA

    T

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    42/50

    F air Share SchedulingA ll scheduling algorithms discussed so fartreat collection of ready processes as ahomogeneous collectionGenerally a user application or job consistsof many processesThis structure is not recognized by traditional schedulers

    User is per se not interested in how anindividual process performsInterested in how set of processes whichconstitute a single application, performs

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    43/50

    F air Share SchedulingIntroduces two levels of abstraction viz.,users & groupsUsers of the same department - one groupA ttempt to give each group/user similarserviceIf large no. of users from one dept. log onto the system, we make sure that theresponse times of members of only thatdept. suffers rather than that of otherdepts.

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    44/50

    F air Share SchedulingFSS is all about making schedulingdecisions based on process sets ratherthan on basis of individual processesEach group/user is assigned a share of theprocessorConsiders the execution history of a relatedgroup of processes, along with theindividual execution history of eachprocess in making scheduling decisionsScheduling is based on priority

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    45/50

    F air Share SchedulingEach group is allocated a weight W k 0

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    46/50

    Revising P riorities

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    47/50

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    48/50

    P roblem4 processes3 groups

    Processor is interrupted 60 times per secondD uring each interrupt the processor usagefield of the currently running process isincremented as is the corresponding groupprocessor usage fieldOnce per second the priorities are recalculatedusing FSS algorithm draw the Gantt chart forthe first 5 seconds

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    49/50

    Problem

    Group name Group 1 Group2 Group3Process Id P1 P2 P3 P4

    Groupweightage

    0.4 0.3 0.3

    Process basepriority

    60 80 50 50

  • 8/7/2019 OS Lecture#4 - Process Scheduling

    50/50

    Coming U p Process Coordination

    SynchronizationD

    eadlocks