CPU Scheduling: Algorithms and Concepts
CPU scheduling is a fundamental concept in operating systems that determines which process gets access to the CPU when multiple processes are ready to execute. The primary goals of CPU scheduling are to maximize CPU utilization, maximize throughput (number of processes completed per time unit), minimize turnaround time (time from submission to completion), minimize waiting time (time spent waiting in the ready queue), and minimize response time (time from submission to first response).
In multiprogramming environments, the operating system must make decisions about which process to run at any given moment. These decisions are made by the CPU scheduler, which is part of the operating system's process management component. The scheduler selects a process from the ready queue and allocates the CPU to it.
CPU scheduling becomes especially important because the CPU is one of the most important and expensive resources in a computer system. Efficient scheduling ensures that this resource is utilized effectively while providing good performance for all processes.
The scheduling algorithm must consider various factors such as process priority, estimated execution time, and arrival time to make optimal decisions. Different algorithms have different strengths and weaknesses, making them suitable for different scenarios.
In preemptive scheduling, the operating system can interrupt a currently running process and switch to another process. This allows the system to respond promptly to important tasks but requires more complex logic to manage context switching. Preemptive scheduling is used in most modern operating systems because it improves system responsiveness and allows for fair resource allocation.
Examples of preemptive scheduling include Round Robin and Multilevel Feedback Queue scheduling.
In non-preemptive scheduling, once a process starts executing, it continues until it finishes or voluntarily gives up the CPU (for example, when waiting for I/O). This approach is simpler to implement but may lead to poor system responsiveness, especially when time-critical tasks need to be executed.
Examples of non-preemptive scheduling include First-Come, First-Served and Shortest Job First (when implemented non-preemptively).
Different CPU scheduling algorithms have different performance characteristics. To compare them, we use several criteria:
Often, these criteria conflict with each other. For example, minimizing average waiting time might increase the waiting time of some processes. The choice of scheduling algorithm depends on which criteria are most important for the specific system and workload.
The First-Come, First-Served (FCFS) scheduling algorithm is the simplest CPU scheduling algorithm. As the name suggests, the process that requests the CPU first gets the CPU first. The implementation of the FCFS policy is easily managed with a FIFO (First-In-First-Out) queue.
When a process enters the ready queue, its PCB (Process Control Block) is linked to the tail of the queue. When the CPU becomes free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. The FCFS scheduling is non-preemptive, meaning that once the CPU has been allocated to a process, that process keeps the CPU until it releases it, either by terminating or by requesting I/O.
Consider the following set of processes that arrive at time 0 with the indicated burst times:
| Process | Burst Time |
|---|---|
| P1 | 24 |
| P2 | 3 |
| P3 | 3 |
If we use FCFS scheduling, the Gantt chart is:
0 24 27 30
Let's calculate the waiting time for each process:
The average waiting time is (0 + 24 + 27) / 3 = 17 ms.
Advantages:
Disadvantages:
The Shortest Job First (SJF) scheduling algorithm associates with each process the length of the process's next CPU burst. When the CPU is available, it is assigned to the process with the smallest next CPU burst. If two processes have the same length, FCFS is used to break the tie.
The SJF algorithm can be either preemptive or non-preemptive. When a new process arrives with a shorter CPU burst than the current process's remaining burst, the CPU is preempted. This preemptive version is called Shortest Remaining Time First (SRTF).
Consider the following set of processes:
| Process | Burst Time |
|---|---|
| P1 | 6 |
| P2 | 8 |
| P3 | 7 |
| P4 | 3 |
Using SJF, we would arrange the processes in order of burst time: P4, P1, P3, P2.
0 3 9 16 24
The waiting time is:
The average waiting time is (3 + 16 + 9 + 0) / 4 = 7 ms.
Advantages:
Disadvantages:
Since we usually can't know the length of the next CPU burst, SJF is often implemented by predicting the next burst time based on previous bursts. A common prediction technique is exponential averaging:
n+1 = tn + (1-)n
Where:
A higher gives more weight to recent history, while a lower gives more weight to past history.
Priority scheduling is a scheduling algorithm where each process is assigned a priority, and the CPU is allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order.
Priority scheduling can be either preemptive or non-preemptive. When a higher priority process arrives, a preemptive priority scheduling algorithm will preempt the currently running lower priority process. In non-preemptive priority scheduling, the newly arrived process with higher priority is placed at the head of the ready queue.
Consider the following set of processes:
| Process | Burst Time | Priority |
|---|---|---|
| P1 | 10 | 3 |
| P2 | 1 | 1 |
| P3 | 2 | 2 |
| P4 | 1 | 4 |
| P5 | 5 | 2 |
Higher numbers represent higher priority in this example.
Assuming all processes arrive at time 0 and we're using non-preemptive priority scheduling, the Gantt chart would be:
0 1 11 13 18 19
The waiting time is:
The average waiting time is (1 + 18 + 11 + 0 + 13) / 5 = 8.6 ms.
A major problem with priority scheduling is the potential for indefinite blocking or starvation. Low-priority processes may never execute if the system has a continuous supply of higher-priority processes. This can be addressed by aging, which gradually increases the priority of processes that wait a long time in the ready queue.
Round Robin (RR) scheduling is designed for time-sharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes. A small unit of time, called a time quantum or time slice, is defined. The ready queue is treated as a circular queue. The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time interval of up to one time quantum.
The process starts at the front of the ready queue, executes for at most one time quantum. If the process still needs more CPU time at the end of its time quantum, it is preempted and placed at the end of the ready queue. If the process finishes before its time quantum expires, it releases the CPU voluntarily, and the scheduler proceeds to the next process in the ready queue.
Consider the following set of processes:
| Process | Burst Time |
|---|---|
| P1 | 24 |
| P2 | 3 |
| P3 | 3 |
With a time quantum of 4 ms:
0 4 7 10 14 18 22 26 30
The waiting time is:
The average waiting time is (6 + 4 + 7) / 3 = 5.67 ms.
The performance of RR scheduling heavily depends on the size of the time quantum:
Advantages:
Disadvantages:
Multilevel queue scheduling allows processes to be divided into separate queues. Each queue might have its own scheduling algorithm. For example, we might use RR scheduling for interactive processes and FCFS for batch processes. The queues themselves have priorities, and the scheduler schedules the highest-priority queue first.
Processes are permanently assigned to one queue based on some property like memory size, process priority, or process type. Each queue has absolute priority over lower-priority queues. No process in a lower-priority queue can run until all processes in higher-priority queues are finished.
A typical multilevel queue scheduler might have:
Advantages:
Disadvantages:
Multilevel feedback queue scheduling is an extension of multilevel queue scheduling that allows processes to move between queues. The idea is to separate processes according to the characteristics of their CPU bursts.
If a process uses too much CPU time, it will be moved to a lower-priority queue. This scheme leaves I/O-bound and interactive processes in the higher-priority queues. If a process waits too long in a lower-priority queue, it may be moved to a higher-priority queue (aging). This form of aging prevents starvation.
A multilevel feedback queue scheduler is defined by the following parameters:
Consider a system with three queues:
A new job enters queue 1. When it gets CPU, it receives 8ms. If it does not finish in 8ms, it is moved to queue 2. At queue 2, it receives 16ms. If it still does not complete, it is moved to queue 3, where it is served in FCFS order.
Advantages:
Disadvantages:
| Algorithm | Preemptive | Throughput | Response Time | Waiting Time | Overhead | Best For |
|---|---|---|---|---|---|---|
| FCFS | No | Low | Poor | High | Low | Simple batch systems |
| SJF | Can be | High | Good | Low | Moderate | Processes with known burst times |
| Priority | Can be | Variable | Variable | Variable | Moderate | Systems with process priorities |
| Round Robin | Yes | Moderate | Excellent | Moderate | High | Time-sharing systems |
| Multilevel Queue | Variable | Variable | Good | Variable | High | Systems with distinct process types |
| Multilevel Feedback | Yes | High | Excellent | Low | Very High | General-purpose systems |
The choice of CPU scheduling algorithm depends on the specific requirements of the system:
