previous
 next 
CS 3733 Operating Systems Notes: Process Scheduling
(CPU Scheduling)
(change semester)

Recall the states of a process
What causes a process to move out of the CPU?
Processes are moved from one state to another using queues
Schedulers
When might the CPU scheduler need to make a decision?
When a process:
  1. switches from running to waiting --- say for I/O
  2. terminates
  3. switches from running to ready --- because of an interrupt
  4. switches from waiting to ready --- I/O has completed

Non-preemptive scheduling: Only 1 and 2 cause scheduler action.\
Once a process has the CPU, it keeps it until it needs I/O or is finished.
Not suitable for time-sharing.

Preemptive scheduling: 3 or 4 may cause scheduler action.
Note: 4 is a special case of 3.

preemptive examples 1

Context switch --- Switching the CPU to another process requires saving the state of the old process and loading the saved state of the new process.
This can take from 1 microsecond to 100's of microseconds depending on the hardware support.

Dispatcher --- The module that gives control of the CPU to the process selected by the CPU scheduler. It does:

Performance Criteria
Methods of measuring performance of CPU schedulers:
  1. Fairness
    Each process should get a fair share of the CPU.
  2. Efficiency = CPU Utilization
    Percentage of time CPU is busy. May be 40% to 90%.
  3. Throughput
    The number of processes completed per unit time.
    10 per second to 1 per hour.
  4. Turnaround Time
    Time to execute a process from submission to termination.
    This is the sum of CPU time, I/O time, and waiting time.
  5. Waiting Time
    time in ready queue
    The scheduling algorithm does not affect CPU time or I/O time.
  6. Response Time
    Time between submission of a request and the first response.
    This may be more appropriate for an interactive system.
  7. Response Time Variance
    For an interactive system, it may be important for the response time to not vary too much.

Performance Criteria

A simple multiprogramming example:
Assume 2 processes, each requires 60 seconds of CPU time and waits 1 second for I/O every second.
Without multiprogramming, this takes 2 minutes for each process.
With multiprogramming the first finishes in 2 minutes and the second one finishes 1 second later.
Without multiprogramming, CPU utilization is about 50%.

Scheduling Algorithms

First-Come/First-Served (FCFS)

FCFS is managed by a strict FIFO queue.

Example 1:

PrcocessBurst Time
124
23
33

CPU Gantt chart
Shows which process is using the CPU at any time.

P1 P2 P3
0 24 27 30
Average waiting time = (0 + 24 + 27)/3 = 17.

Example 2: suppose the processes arrive in reverse order. We get:

P3 P2 P1
0 3 6 30
AWT = (0 + 3 + 6)/3.

Original example:

Process Gantt Chart
For each process, shows that state of the process at any time.

P1
P2
P3
Run
Ready Run
Ready Run
0 24 27 30
The same chart may be represented as:
P1: RRRRRRRRRRRRRRRRRRRRRRRR
P2: rrrrrrrrrrrrrrrrrrrrrrrrRRR
P3: rrrrrrrrrrrrrrrrrrrrrrrrrrrRRR
Example 3:
ProcessCPU BurstI/O BurstCPU Burst
1873
2632
P1: RRRRRRRRwwwwwwwRRR
P2: rrrrrrrrRRRRRRwwwrRR
Note: Example 3 FCFS AWT = (0 + 9)/2 = 4.5
FCFS Example 3a

Shortest Job First (SJF)

Take the job with has the shortest CPU burst and run it next.

Example 4:

PrcocessBurst Time
16
28
37
43

P4 P1 P3 P2
0 3 9 16 24
AWT = (0 + 3 + 9 + 16)/4 = 7.

How do you know what the shortest job is?
Use past history to do the prediction.
Let τn+1 be the predicted time of the next cycle and tn the actual time of the current cycle.
Use τn+1 = αtn + (1 - α)τn
Usualy α = 1/2.
The initial τ0 can be defined as a constant which is the system average.
This mehtod is called the exponential average.
To see this, expand out the recurrence relation:

τn+1 = αtn + (1 - α)τn
τn+1 = αtn + (1 - α)(αtn-1 + (1 - α)τn-1)
τn+1 = αtn + (1 - α)αtn-1 + (1 - α)2τn-1
τn+1 = αtn + (1 - α)αtn-1 + (1 - α)2αtn-2 + (1 - α)3τn-2
τn+1 = αtn + (1 - α)αtn-1 + ... + (1 - α)jαtn-j + ... (1 - α)nαt0
Each successive term has a smaller weight.

Example 3 for SJF:

P1: rrrrrrRRRRRRRRwwwwwwwRRR
P2: RRRRRRwwwrrrrrRR
Example 3 SJF AWT = (6 + 5)/2 = 5.5

Preemptive Shortest Job First (PSJF)

The SJF algorithm may be preemptive or non-preepmtive.
In the preemptive version, if a new process enters the ready queue with a shorter next CPU burst than what is (expected to be) left of the currently executing process, the current job with be replaced by the new one.
This is also called shortest-remaining-time-first scheduling.

P1: rrrrrrRRRrrRRRRRwwwwwwwRRR
P2: RRRRRRwwwRR
Example 3 PSJF AWT = (8 + 0)/2 = 4

In non-preemptive scheduling, once the CPU has been allocated to a process, it keeps the CPU until it terminates or requests I/O.
Pre-emptive scheduling allows a higher priority process to preempt an executing process if its priority is higher.
In time sharing systems you don't want to have CPU intensive processes, grab the CPU and keep it. This is usually handled by the following preemptive algorithm.

PSJF Examples 1

Round Robin Scheduling (RR)

In round-robin scheduling, a small unit of time called a quantum is defined (originally 10 to 100 milliseconds).

Example 1 using a quantum of 4:

P1: RRRRrrrrrrRRRRRRRRRRRRRRRRRRRR
P2: rrrrRRR
P3: rrrrrrrRRR
AWT = (6 + 4 + 7)/3 = 17/3 = 5.67.

If the quantum is small, n processes, each appear to have their own CPU which is running at 1/n of the speed of the original machine.

Each time a process is switched out (context switch) there is overhead.
If the quantum is too small, no useful work will be done.
Usually there is some hardware assistance for context switching.

Rule of thumb 80% of the cpu bursts should be shorter than the quantum.
If the process executes its entire CPU burst during most quantums, you will get much better turnaround time for the process.

The quantum affects both the efficiency of CPU utilization and the response time.
If the quantum is too long, response time suffers.
If it is too short, too much time is spent on context switches.

Example 3 with Round Robin and a quantum of 3:

P1: RRRrrrRRRrrrRRwwwwwwwRRR
P2: rrrRRRrrrRRRwwwRR
Example 3 RR3 AWT = (6 + 6)/2 = 6.0
RR Examples 1

Summary

We looked at four major scheduling algorithms:
Next Notes

Back to CS 3733 Notes Table of Contents