CPU Scheduling Scheduling Terminology Scheduling Terminology

Document Sample
CPU Scheduling Scheduling Terminology Scheduling Terminology Powered By Docstoc
					                                                                                            CIS 415




                       CPU Scheduling                     Scheduling Terminology
               • Basic Concepts
               • Scheduling Criteria
               • Scheduling Algorithms
                   – FCFS, SJF, RR
               •   Exponential Averaging
               •   Multi-level Queue Scheduling
               •   Performance Evaluation
               •   Unix Scheduler


                                                  1                                     2




               Scheduling Terminology                 Scheduling/Process State Transition




                                                  3                                     4




                    Scheduling/ Queues                    Objectives for Schedulers




                                                  5                                     6




Introduction to Scheduling
                                                                                                                CIS 415




          Objectives for Schedulers (cont’d)                     CPU burst and I/O Burst
                                                           • Maximum CPU utilization obtained with
                                                             multiprogramming
                                                           • CPU–I/O Burst Cycle – Process execution
                                                             consists of a cycle of CPU execution and I/O
                                                             wait.
                                                           • CPU burst distribution




                                                       7                                                    8




             Alternating Sequence of CPU And I/O Bursts      Histogram of CPU-burst Times




                                                       9                                                10




                  Types of Schedulers                         CPU (Short Term) Scheduler
                                                           • Selects from among the processes in memory
                                                             that are ready to execute, and allocates the
                                                             CPU to one of them.
                                                           • CPU scheduling decides which process to
                                                             switch from ready state to running state.




                                                      11                                                12




Introduction to Scheduling
                                                                                                                                                                                CIS 415




                                                                                              First-Come, First-Served (FCFS) Scheduling
                CPU Scheduling Algorithms
          •   First Come First Served (FCFS)
                                                                                                             Process Burst Time
          •   Shortest Job First (SJF)                                                                           P1          24
          •   Round Robin (RR)                                                                                   P2           3
                                                                                                                 P3           3
          •   Multi-level Queues
                                                                                              • Suppose processes arrive in the order P1 , P2 , P3
          •   Multi-level with Feedback                                                         The Gantt Chart for the schedule is:
          •   (There are many others)                                                                                        P1                       P2         P3
                                                                                              •
                                                                                                      0                                         24         27         30

                                                                                              • Waiting time for P1 = 0; P2 = 24; P3 = 27
                                                                                    13
                                                                                              • Average waiting time: (0 + 24 + 27)/3 = 17                                 14




                              FCFS Scheduling (Cont.)                                                 Shortest-Job-First (SJF) Scheduling

          Suppose that the processes arrive in the order                                 • Associate with each process the length of its next CPU burst.
                                                                                           Use these lengths to schedule the process with the shortest time.
                              P2 , P3 , P1 .
                                                                                         • Two schemes:
          • The Gantt chart for the schedule is:                                            – nonpreemptive – once CPU given to the process it cannot be
                                                                                               preempted until completes its CPU burst.
                          P2        P3                          P1
                                                                                            – preemptive – if a new process arrives with CPU burst length
                                                                                               less than remaining time of current executing process,
                     0          3            6                                 30              preempt. This scheme is know as the
                                                                                               Shortest-Remaining-Time-First (SRTF).
          •   Waiting time for P1 = 6; P2 = 0; P3 = 3                                    • SJF is optimal – gives minimum average waiting time for a
          •   Average waiting time: (6 + 0 + 3)/3 = 3                                      given set of processes. It is an idealized algorithm because
                                                                                           difficult to accurately know next CPU burst time.
          •   Much better than previous case.
          •   Avoided convoy effect short process behind long
              process
                                                                                    15                                                                                     16




              Example of Non-Preemptive SJF                                                       Example of Preemptive SJF
             Arrival Time Burst Time                                                        Arrival Time Burst Time
               P1 0.0        7                                                                P1 0.0        7
                P2 2.0       4                                                                 P2 2.0       4
                P3 4.0       1                                                                 P3 4.0       1
                P4 5.0       4                                                                 P4 5.0       4
          • SJF (non-preemptive)                                                         • SJF (preemptive)
                         P1                  P3       P2             P4                               P1       P2       P3        P2       P4               P1

                 0        3              7        8        12             16                      0                                                  11               16
                                                                                                           2        4         5        7
          • Average waiting time = (0 + 6 + 3 + 7)/4 = 4                                 • Average waiting time = (9+1+0+2)/4 = 3

                                                                                    17                                                                                     18




Introduction to Scheduling
                                                                                                                                       CIS 415




                      Priority Scheduling                                   Determining Length of Next CPU Burst

          • A priority number (integer) is associated with each       • Can only estimate the length.
            process
          • The CPU is allocated to the process with the
                                                                      • Can be done by using the length of previous
            highest priority (smallest integer ≡ highest priority).     CPU bursts, using exponential averaging.
          • SJF is a priority scheduling where priority is the               1. tn = actual lenght of nthCPU burst
            predicted next CPU burst time.                                   2. # n +1 = predicted value for the next CPU burst
          • Problem !!! Starvation – low priority processes                  3. " , 0 ! " ! 1
                                                                             4. Define :
            may never execute.
                                                                                                τ n+1 = α tn + (1 −α ) n .
                                                                                                                     τ
          • Solution = Aging – as time progresses increase the
            priority of the process.


                                                                 19                                                               20




                  Predicting Length of Next CPU Burst                   Exponential Averaging and the Value of α
                                                                      • α =0
                                                                         – τn+1 = τn
                                                                         – Recent history does not count.
                                                                      • α =1
                                                                         – τn+1 = t n
                                                                         – Only the actual last CPU burst counts.
                                                                      • If we expand the formula, we get:
                                                                            τn+1 = α tn+(1 - α) α tn -1 + …
                                                                                    +(1 - α )j α tn -1 + …
                                                                                    +(1 - α )n=1 tn τ0
                                                                      • Since both α and (1 - α) are less than or equal to 1,
                                                                        each successive term has less weight than its
                                                                        predecessor.
                                                                 21                                                               22




                   Exponential Averaging                                              Round Robin (RR)
                                                                      • Each process gets a small unit of CPU time (time
                                                                        quantum), usually 10-200 milliseconds. After this
                                                                        time has elapsed, the process is preempted and
                                                                        added to the end of the ready queue.
                                                                      • If there are n processes in the ready queue and the
                                                                        time quantum is q, then each process gets 1/n of the
                                                                        CPU time in chunks of at most q time units at once.
                                                                        No process waits more than (n-1)q time units.
                                                                      • Performance
                                                                         – q large ⇒ FIFO
                                                                         – q small ⇒ q must be large with respect to context
                                                                           switch, otherwise overhead is too high.

                                                                 23                                                               24




Introduction to Scheduling
                                                                                                                                                          CIS 415




             Example of RR with Time Quantum = 20                                              Time Quantum and Context Switch Time

                                  Process              Burst Time
                                    P1                    53
                                     P2                    17
                                     P3                    68
                                     P4                    24
            • The Gantt chart is:
                   P1        P2    P3        P4    P1     P3   P4   P1   P3   P3

               0        20    37        57        77    97 117 121 134 154 162



            • Typically, higher average turnaround than
              SJF, but better response.
                                                                                   25                                                                26




          Turnaround Time Varies With Time Quantum                                                      Multilevel Queue
                                                                                        • Ready queue is partitioned into separate queues:
                                                                                          foreground (interactive)
                                                                                          background (batch)
                                                                                        • Each queue has its own scheduling algorithm,
                                                                                          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; i.e.,
                                                                                             80% to foreground in RR
                                                                                   27      – 20% to background in FCFS                               28




            Multilevel Queue Scheduling                                                        Multilevel Feedback Queue
                                                                                        • A process can move between the various queues;
                                                                                          aging can be implemented this way.
                                                                                        • Multilevel-feedback-queue scheduler defined by
                                                                                          the following parameters:
                                                                                           –   number of queues
                                                                                           –   scheduling algorithms for each queue
                                                                                           –   method used to determine when to upgrade a process
                                                                                           –   method used to determine when to demote a process
                                                                                           –   method used to determine which queue a process will
                                                                                               enter when that process needs service


                                                                                   29                                                                30




Introduction to Scheduling
                                                                                                               CIS 415




                Example of Multilevel Feedback Queue                         Multilevel Feedback Queues
          • Three queues:
            – Q0 – time quantum 8 milliseconds
            – Q1 – time quantum 16 milliseconds
            – Q2 – FCFS
          • Scheduling
            – A new job enters queue Q 0 which is served FCFS. When
              it gains CPU, job receives 8 milliseconds. If it does not
              finish in 8 milliseconds, job is moved to queue Q1.
            – At Q 1 job is again served FCFS and receives 16
              additional milliseconds. If it still does not complete, it
              is preempted and moved to queue Q2.

                                                                     31                                   32




               Performance for Schedulers                                     Queuing Theory Analysis




                                                                     33                                   34




          Queuing Theory Analysis (cont’d)                                 Queuing Theory Analysis (cont’d)




                                                                     35                                   36




Introduction to Scheduling
                                                                                                  CIS 415




          Queuing Theory Analysis (cont’d)                  Queuing Theory Analysis (cont’d)




                                                       37                                    38




                                                               Evaluation of CPU Schedulers by
                    Simulation Analysis                                   Simulation




                                                       39                                    40




                   Performance metrics                        Simulation Analysis (cont’d)
          • Turnaround time = time from when job is
            submitted to the system until when it is
            completed
          • Normalized timearound time = Turnaround
            time / job runtime (also called slowdown)
          • Sometime ‘response time’ is used
            interchangeably. Strictly speaking, response
            time is used in interactive systems to mean
            the time from when the user submits a
            request to systems’ first reaction

                                                       41                                    42




Introduction to Scheduling
                                                                                                                                            CIS 415




                     Empirical Experiments                                 Traditional UNIX Scheduling
                                                                     •   Multilevel feedback queues
                                                                     •   128 priorities possible (-64 to -63)
                                                                     •   1 Round Robin queue per priority
                                                                     •   Every scheduling event the scheduler picks
                                                                         the highest priority (lowest number) non-
                                                                         empty queue and runs jobs in round-robin




                                                               43                                                                   44




                          UNIX Scheduler                                   Process Scheduling in Unix
                                                                    • Negative numbers reserved for processes waiting in kernel
                                                                      mode (just woken up by interrupt handlers) (why do they
                                                                      have a higher priority?)
                                                                    • Time quantum = 1/10 sec (empirically found to be the
                                                                      longest quantum that could be used without loss of the
                                                                      desired response for interactive jobs such as editors)
                                                                         – short time quantum means better interactive response
                                                                         – long time quantum means higher overall system throughput since
                                                                           less context switch overhead and less processor cache flush.
                                                                    • Priority dynamically adjusted to reflect
                                                                         – resource requirement (e.g., blocked awaiting an event)
                                                                         – resource consumption (e.g., CPU time)




              Traditional UNIX Scheduling                                     UNIX Priority Calculation
          • All processes assigned a baseline priority based on      • Every 4 clock ticks a processes priority is updated:
            the type and current execution status (for
            convenience here renumber from 0 to 127):                •                          & utilization #
             –   swapper 0                                                       P = BASELINE + $             ! + 2 NiceFactor
                                                                                                %       4     "
             –   waiting for disk 20
             –   waiting for lock 35                                 • The utilization is incremented by 1 on every clock
             –   user-mode execution   50                              tick when the process is running; it is also decayed
                                                                       every second (next slide)
          • At scheduling events, all process’s priorities are
            adjusted based on the amount of CPU used, the            • The niceFactor allows user some control of job
            current load, and how long the process has been            priority. It can be set by user from –20 to 20.
            waiting.                                                 • Jobs using a lot of CPU increase the priority value.
          • Most processes are not running, so lots of                 Interactive jobs not using much CPU will return to
            computing shortcuts are used when computing new            the baseline.
                                                               47                                                                   48
            priorities.




Introduction to Scheduling
                                                                                                                                       CIS 415




                UNIX Priority Calculation                                         UNIX Priority Decay
          • Without decay, long running CPU bound jobs will         •1 job on CPU: load will thus be 1. Assume niceFactor is 0.
            get “stuck” at the highest numbered priority.           •Compute utilization at time N:
          • Decay function used to weight utilization to recent
            CPU usage.                                              •+1 second:         2
                                                                                    U1 = U 0
          • A process’s utilization at time t is decayed every                          3
            second:
                                                                    •+2 seconds
          • utilt =( 2*load / 2*load+1) * utilt-1 + niceFactor                     2,     2 ) 2          &2#
                                                                                                             2

                                                                    •         U 2 = *U 1 + U 0 ' = U 1 + $ ! U 0
                                                                                       3+       3     (   3       %3"
          • The system load is the system average number of         •+N seconds
            runnable jobs during last 1 minute                                        2         '2$
                                                                                                     2

                                                                                  Un = Un ! 1 + % " U n ! 2 ...
                                                                                      3         & 3#
                                                               49                                                                 50




               Summary of Unix Scheduler

           • Commonly used implementation with multiple
             priority queues
           • Priority computed using 3 factors
              – PUSER used as a base (changed dynamically)
              – CPU utilization (time decayed)
              – Value specified at process creation (nice)
           • Processes with short CPU bursts are favored
           • Processes just woken up from blocked states are
             favored even more
           • Weighted averaging of CPU utilization
           • Details vary in different versions of Unix




Introduction to Scheduling

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:10/6/2011
language:English
pages:9