When several processes are ready, the CPU scheduler must choose which one runs. Its choice affects how fast the system feels and how much work it gets done.

CPU and I/O Bursts

A process alternates between CPU bursts, periods of computation, and I/O bursts, periods of waiting for I/O. An I/O-bound process has many short CPU bursts, and a CPU-bound process has a few long ones. Scheduling decides which process gets the CPU for its next burst.

Definition: CPU scheduling. The selection, by the short-term scheduler, of one process from the ready queue to be given the CPU next.

Scheduling Objectives and Criteria

CPU utilisation: keep the CPU as busy as possible. Throughput: complete as many processes as possible per unit time. Turnaround time: the time from the submission of a process to its completion, TAT = completion time − arrival time. Waiting time: the total time a process spends in the ready queue, WT = TAT − burst time. Response time: the time from submission until the first response is produced. Fairness: each process gets a fair share of the CPU, with no starvation. A good algorithm maximises utilisation and throughput, and minimises turnaround, waiting and response times.

When Scheduling Happens

A scheduling decision is needed when a process (1) switches from running to waiting, (2) switches from running to ready, as on an interrupt, (3) switches from waiting to ready, or (4) terminates.

Preemptive and Non-Preemptive Scheduling

In non-preemptive scheduling, once a process gets the CPU, it keeps it until it terminates or waits for I/O (cases 1 and 4). It is simple and has low overhead, but a long process can hold up short ones. In preemptive scheduling, the OS can take the CPU away from a running process, for example when its time slice ends or a more important process arrives (cases 2 and 3). It gives better response and fairness, but has more context switches and needs care with shared data. FCFS and plain SJF are non-preemptive; Round Robin and SRTF (preemptive SJF) are preemptive.

Table 2.4: Scheduling criteria

Criterion Meaning Aim
CPU utilisation Fraction of time CPU is busy Maximise
Throughput Processes completed per unit time Maximise
Turnaround time Completion − arrival Minimise
Waiting time Turnaround − burst Minimise
Response time Time to first response Minimise

Example 2.4: Turnaround and waiting time

Problem: A process arrives at time 2, needs a CPU burst of 5 ms, and completes at time 14. Find its turnaround and waiting time.

Solution:

  1. TAT = completion − arrival = 14 − 2 = 12 ms.
  2. WT = TAT − burst = 12 − 5 = 7 ms.

Result: The turnaround time is 12 ms and the waiting time is 7 ms.

#include <stdio.h>
int main(void) {
    int arrival = 2, burst = 5, completion = 14;
    int tat = completion - arrival, wt = tat - burst;
    printf("Turnaround = %d ms, Waiting = %d ms\n", tat, wt);
    return 0;
}

Output

Turnaround = 12 ms, Waiting = 7 ms

Common mistakes

  • Mixing up turnaround time and waiting time.
  • Thinking that non-preemptive scheduling can stop a running process. It cannot.
  • Optimising one criterion and ignoring starvation.

Key takeaways

  • TAT = completion − arrival; WT = TAT − burst.
  • Maximise utilisation and throughput; minimise turnaround, waiting and response times.
  • Non-preemptive: the process keeps the CPU until it waits or ends.
  • Preemptive: the OS can take the CPU away.

Practice questions

  1. Define waiting time.
  2. What is throughput?
  3. Distinguish preemptive and non-preemptive scheduling.

Answers to the practice questions

  1. The total time a process spends in the ready queue, equal to turnaround time minus burst time.
  2. The number of processes completed per unit of time.
  3. In preemptive scheduling the OS can take the CPU from a running process; in non-preemptive scheduling the process keeps it until it waits or ends.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit II