of 21/21
Operating Systems Lecture Notes Operating Systems Lecture Notes CPU Scheduling CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002

Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002

  • View

  • Download

Embed Size (px)

Text of Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin,...

  • Operating Systems Lecture NotesMatthew DaileySome material Silberschatz, Galvin, and Gagne, 2002CPU Scheduling

  • CPU SchedulingMain Question: Which process to select from ready queue?Preemption vs. non-preemptionEvaluating scheduling algorithmsAlgorithms (FCFS, SJF, RR, Priority, Multilevel Queue)

    Readings: Silberschatz et al., chapter 6

  • CPU Scheduling: What Policy?Multiprogramming goal: keep busy. Run some process at all timesPolicy determines how you allocate resources to processesNo single policy is best for all situations!Best policy depends on goal of the systemSingle-user desktop PCCompute server for scientific applicationsInteractive time-sharing system

  • CPU Burst --- I/O Burst CycleEmpirically, most programs alternate between CPU bursts and I/O bursts.This makes multiprogramming desirable.

  • CPU Burst TimesVaries with application and hardware.

    But histogram almost always looks exponential.

    Most bursts short, a few are long.

  • Short-term schedulerSelects next job from ready queue.Ready queue not necessarily FIFO!Must run when:A process switches from running to waiting (I/O, wait(), etc.)A process terminatesIf preemptive, can also run when:A process switches from running to ready (Interrupt)A process switches from waiting to ready (I/O completion)

  • Preemptive scheduler: Pros and ConsPreemption seems a good choice to avoid this. BUT:Interrupt during shared user data updateProblem: Can cause inconsistency or corruptionRemedy: Synchronization (Chapter 7)Interrupt during system callProblem: Could cause kernel data inconsistency or corruptionRemedy: Disable interrupts during kernel data updatesMust keep disable time super short or might miss interruptsOSs like Windows 3.1 and early Apple MacOS were non-preemptive. Badly-behaved apps could kill these systems.Preemption increases complexity for OS designer AND programmers

  • Note on Dispatch LatencyThe dispatcher is a piece of kernel code that:Switches contextFlips protection bit to user modeJumps to correct location in user programDispatch latency is the time it takes from the interrupt to the final jump

  • Scheduling algorithm: Evaluation criteriaGenerally want to maximizeCPU utilization% of time CPU is in useThroughput # of processes completed per unit timeGenerally want to minimizeTurnaround timetime from submission to completionadmit time + ready time + CPU time + I/O timeWait time amount of time spend in ready queueResponse time time from submission to first output(important in interactive systems)Usually optimize average but there are other choices.

  • Algorithm 1: First Come, First Served (FCFS)Characteristics:A non-preemptive scheme.Like Bangkok Bank when only one service desk is open.One long-running process can clog the queue for a long time.Example:Process Arrival Burst time P1024 P213 P323Avg wait time: ( 0 + 24 + 27 ) / 3 = 17 msBUT opposite order wait time = ???( 0 + 3 + 6 ) / 3 = 3 ms. Much improved!

  • Algorithm 2: Non-Preemptive Shortest Job First (SJF)Characteristics:Always assign job with shortest next CPU burst to CPUProvably optimizes average wait time among non-preemptive algorithms.Example:Process Arrival Burst P1 0 7 P2 2 4 P3 4 1 P4 5 4Avg wait time: ( 0 + 6 + 3 + 7 ) / 4 = 4 msFCFS wait time: ( 0 + 5 + 7 + 7 ) / 4 = 4.75 ms

  • Algorithm 3: Preemptive Shortest Job First (SJF)Characteristics:Like Shortest Job First, but running job can be preempted.Provably optimizes average wait time among preemptive algorithms.Example:Process Arrival Burst P1 0 7 P2 2 4 P3 4 1 P4 5 4Average waiting time = (9 + 1 + 0 +2)/4 = 3 ms

    Reason that SJF is provably optimal: moving a shorter process earlier always decreases short process wait time more than it increases long process wait time.

  • Implementing SJFSJF is great, but how do we implement it?We dont know a priori how long a jobs burst time isWe have to try to predict the burst time

  • Burst time prediction with exponential average

  • Priority SchedulingCharacteristics:Always schedule the ready process with highest priorityPriority scheduling can be preemptive or non-preemptiveSJF is a special case of priority scheduling:process priority = the inverse of remaining CPU time

  • Priority Scheduling IssuesPriorities backwards or forwards?Unix: -20 is highest priority, +20 is lowestShow output of the top Unix program on the ITLAB serverWhere do the priorities come from?Usually internally derived by the OSSometimes externally derived by users/managersProblem: Low-priority processes suffer from starvation. They may have to wait indefinitely.Solution: Process aging (gradually increase priority of old processes)

  • Round-RobinCharacteristics:For time-sharing systemsSimilar to FCFS but preemptiveReady queue is a circular queueDefine a short time quantum, e.g. 20 ms

    Algorithm: Before starting a process, set timer to generate interrupt after quantum expires If (CPU burst time < quantum) thenprocess gives up CPU voluntarily elsetimer generates interrupt after quantum expiresinterrupt causes context switch to kernel moderunning process is moved to tail of the ready queueswitch to next process in queue

  • Round Robin Example [Quantum = 20]Process Arrival Burst TimeP1053P2117P3268P4324

    Notice the long wait times but short response times

    Issue: Whats the right time quantum?Too short: too many context switches (too much overhead)Too long: approaches FCFS performanceRule of thumb: large enough to handle 80% of the CPU bursts02037577797117121134154162Gantt Chart

  • Variations on scheduling: Multilevel queuesUse more than one ready queuee.g. foreground queue for interactive programs and background queue system maintenance, batch programsUse different scheduling algorithm for each queuee.g. RR for the foreground queue, FCFS for background queueNew problem: how to split time between the queues?Absolute priority: can cause starvationTime division: e.g. 80% foreground, 20% background

  • Variations on scheduling: Multilevel feedback queuesDont fix a process in a queue: let it move.One example: several queues with different prioritiesLet I/O bound processes float upward (higher priority)Move CPU hogs downward (lower priority)Move processes waiting a too long gradually upwardFlexible system but complex implementation.

  • What have we learned?What CPU scheduling isPreemptive vs. non-preemptive schedulingHow to evaluate different algorithmsSome of the important scheduling algorithms:FCFSSJF (both preemptive and non-preemptive versions)Provably optimal, but impossible to implementCan be approximated with burst time predictionPriority schedulingRound-robin