CPU Scheduling Algorithms
Scheduling Criteria
 CPU utilization – keep the CPU as busy as possible
 Turnaround time – amount of time to execute a particular
  process
 Waiting time – amount of time a process has been waiting
  in the ready queue
 Response time – amount of time it takes from when a
  request was submitted until the first response is produced,
  not output (for time-sharing environment)
Scheduling Algorithm Optimization Criteria
 Max CPU utilization
 Max throughput
 Min turnaround time
 Min waiting time
 Min response time
        First- Come, First-Served (FCFS) Scheduling
                          Process       Burst Time
                             P1              24
                             P2              3
                           P3                3
 Suppose that the processes arrive in the order: P1 , P2 , P3
  The Gantt Chart for the schedule is:
                              P1                              P2        P3
   0                                                     24        27        30
 Waiting time for P1 = 0; P2 = 24; P3 = 27
 Average waiting time: (0 + 24 + 27)/3 = 17
                         FCFS Scheduling
Suppose that the processes arrive in the order:
                               P2 , P3 , P1
 The Gantt chart for the schedule is:
       P2       P3                            P1
   0        3        6                                   30
 Waiting time for P1 = 6; P2 = 0; P3 = 3
 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Not effective when short process behind long process
FCFS Scheduling
  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
 SJF is optimal – gives minimum average waiting time for a
   given set of processes
       The difficulty is knowing the length of the next CPU request
       Could ask the user
                         Example of SJF
                   Process Arrival Time        Burst Time
                    P1         0.0                 6
                    P2         2.0                 8
                    P3         4.0                 7
                    P4         5.0                 3
 SJF scheduling chart
          P4             P1               P3                P2
      0        3               9                    16           24
 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
Example of Shortest-remaining-time-first
                Process A        Arrival Time    Burst Time
                    P1                  0             8
                    P2                  1             4
                    P3                  2             9
                    P4                  3             5
 Preemptive SJF Gantt Chart
      P1       P2           P4              P1            P3
  0        1        5              10            17                  26
 Waiting Time = [Total waiting time - No. of units process executed
                                                          - Arrival Time]
 Average waiting time = [(10-1-0)+(1-0-1)+(17-0-2)+(5-0-3)]/4 =
                         [9+0+15+2]/4 = 26/4 = 6.5
           SRTF
Find Average Waiting Time
SRTF
             SRTF
Find Average Turn Around Time
SRTF
                    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)
 SJF is priority scheduling where priority is the inverse of predicted
   next CPU burst time
 Problem  Starvation – low priority processes may never execute
 Solution  Aging – as time progresses increase the priority of the
   process
          Example of Priority Scheduling
           Process        Burst Time   Priority
              P1             10           3
              P2             1            1
              P3             2            4
              P4             1            5
             P5              5            2
 Priority scheduling Gantt Chart
 Average waiting time = 8.2 msec
            PRIO
Find Average Waiting Time
PRIO
               Round Robin (RR)
 Each process gets a small unit of CPU time (time
  quantum q).
 After this time has elapsed, the process is preempted
  and added to the end of the ready queue.
Round Robin (RR)
Example of RR with Time Quantum = 4
                 Process        Burst Time
                    P1              24
                    P2               3
                    P3               3
 The Gantt chart is:
 Typically, higher average turnaround than SJF, but better response
 q should be large compared to context switch time
 q usually 10ms to 100ms, context switch < 10 usec
RR
        RR
Time Quantum – 2ms
RR
RR
    Earliest Deadline First Scheduling (EDF)
 Priorities are assigned according to deadlines:
 the earlier the deadline, the higher the priority;
   the later the deadline, the lower the priority
   Dynamic Priority Scheduling
FCFS Scheduling
FCFS Scheduling
FCFS Scheduling
THANKS