CPU Scheduling


What is CPU scheduling?


Basic Scheduling Algorithms (Non-preemptive)

Initial set of simple assumptions
  1. Each job (process/thread) runs the same amount of time.
  2. All jobs arrive at the same time.
  3. Once started, each job runs to completion (no interruption in the middle).
  4. All jobs only use the CPU (no I/O).
  5. The run time of each job is known.

These are unrealistic assumptions, and we will relax them gradually to see how it works in a real system.

First performance metric

turn_around_time = job_completion_time - job_arrival_time

Algorithm 1: First Come First Serve/First In First Out(FCFS/FIFO)
Job Arrival Time Service Time
A 0 3
B 0 3
C 0 3
0 1 2 3 4 5 6 7 8 9 10 11
A A A                  
      B B B            
            C C C      
Initial set of simple assumptions: first adjustment
  1. Each job (process/thread) runs the same amount of time.
  2. All jobs arrive at the same time.
  3. Once started, each job runs to completion (no interruption in the middle).
  4. All jobs only use the CPU (no I/O).
  5. The run time of each job is known.
FCFS/FIFO Performance
Job Arrival Time Service Time
A 0 8
B 0 3
C 0 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A A A A A A A A                
                B B B          
                      C C C    
Algorithm 2: Shortest Job First (SJF)
Job Arrival Time Service Time
A 0 8
B 0 3
C 0 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
B B B                          
      C C C                    
            A A A A A A A A    
Initial set of simple assumptions: second adjustment
  1. Each job (process/thread) runs the same amount of time.
  2. All jobs arrive at the same time.
  3. Once started, each job runs to completion (no interruption in the middle).
  4. All jobs only use the CPU (no I/O).
  5. The run time of each job is known.
FCFS/FIFO Performance
Job Arrival Time Service Time
A 0 8
B 2 3
C 2 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A A A A A A A A                
                B B B          
                      C C C    
Initial set of simple assumptions: third adjustment
  1. Each job (process/thread) runs the same amount of time.
  2. All jobs arrive at the same time.
  3. Once started, each job runs to completion (no interruption in the middle).
  4. All jobs only use the CPU (no I/O).
  5. The run time of each job is known.

Preemptive vs Non-preemptive

Second performance metric

response_time = first_scheduled_time - job_arrival_time

Algorithm 3: Shortest Time-to-Completion First (STCF)
Job Arrival Time Service Time
A 0 8
B 2 3
C 2 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A A             A A A A A A    
    B B B                      
          C C C                
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A A             A A A A A A    
    B B B                      
          C C C                
Algorithm 4: Round Robin (RR)
Job Arrival Time Service Time
A 0 8
B 2 3
C 2 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A A     A     A     A A A A    
    B     B     B              
      C     C     C            
Initial set of simple assumptions: fourth adjustment
  1. Each job (process/thread) runs the same amount of time.
  2. All jobs arrive at the same time.
  3. Once started, each job runs to completion (no interruption in the middle).
  4. All jobs only use the CPU (no I/O).
  5. The run time of each job is known.
(Almost) all processes perform I/O
Jobs with I/O
Job Arrival Time CPU Time I/O
A 0 5 One per 1 sec
B 0 5 none
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A I/O A I/O A I/O A I/O A              
                  B B B B B    
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A I/O A I/O A I/O A I/O A              
  B   B   B   B   B            
Initial set of simple assumptions: No more assumption
  1. Each job (process/thread) runs the same amount of time.
  2. All jobs arrive at the same time.
  3. Once started, each job runs to completion (no interruption in the middle).
  4. All jobs only use the CPU (no I/O).
  5. The run time of each job is known.
The question

How do we schedule jobs without knowing their run time duration so that we can minimize turn around time and also minimize response time for interactive jobs?


Multi-level Feedback Queue (MLFQ)

Overview of MLFQ
MLFQ: the queues
MLFQ’s feedback rules 1 and 2
Does it work well?

With only these two rules, A and B will keep alternating via RR, and C and D will never get to run.

What other rule(s) do we need to add?

Attempt 1: How to change priority?
Example
Initial: A single long-running job
Along came a short job
What about I/O?
Problem 1: Starvation
Problem 2: Gaming the system
Problem 3: What if a program changes behavior?
Attempt 2: Priority boost
Problem: Starvation

Rule 5 help guaranteeing that processes will not be staved. It also helps with CPU-bound jobs that become interactive

What S should be set to?
Attempt 3: Better accounting
Summary

MLFQ observes the execution of a job and gradually learns what type of job it is, and prioritize it accordingly.