Welcome to your next lesson in the Operating Systems module. In our previous discussion, we focused on how the OS manages processes that compete for shared resources, culminating in our study of deadlock detection and avoidance with the Banker's Algorithm. We learned how to ensure the system remains in a safe state regarding resource allocation.
Today, we shift our focus from resource contention to another critical OS responsibility: managing the single, most contended resource of all—the CPU. When multiple processes are ready to run, which one gets the CPU, and for how long? This decision is made by the CPU scheduler.
This lesson will equip you to tackle one of the most common and scorable types of questions in the GATE exam. We will cover four fundamental CPU scheduling algorithms: First-Come, First-Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin (RR). Your learning goal is to be able to take a set of processes, apply any of these algorithms, and calculate key performance metrics like average turnaround time and average waiting time.
The Language of Scheduling: Key Metrics
Before we dive into the algorithms, we must define the terms we'll use to measure their performance. For any given set of processes, you'll typically be provided with:
- Process ID: A unique identifier for the process.
- Arrival Time (AT): The time at which the process enters the ready queue, becoming available for execution.
- Burst Time (BT): The total amount of CPU time the process requires to complete its execution.
From these, we will calculate:
- Completion Time (CT): The time at which the process finishes its execution.
- Turnaround Time (TAT): The total time a process spends in the system, from arrival to completion. It's the measure of total time spent, including both waiting and running.
- Waiting Time (WT): The total time a process spends in the ready queue waiting for its turn on the CPU.
- Response Time (RT): The time from a process's arrival until it gets the CPU for the first time. This is particularly important for interactive systems.
Our primary tool for solving these problems will be the Gantt Chart, a timeline diagram that illustrates which process is running on the CPU at any given moment.
A crucial distinction that applies to most scheduling algorithms is their mode:
- Non-Preemptive: Once a process is given the CPU, it runs to completion without interruption.
- Preemptive: A running process can be interrupted (preempted) and moved back to the ready queue if a higher-priority process arrives or its time slice expires.
Let's begin with the simplest algorithm.
1. First-Come, First-Served (FCFS) Scheduling
As the name suggests, FCFS is a non-preemptive algorithm that processes jobs in the order they arrive. It's analogous to a queue at a ticket counter.
- Criterion: Arrival Time.
- Mode: Non-preemptive.
The main advantage of FCFS is its simplicity. However, it can suffer from the convoy effect, where a short process gets stuck waiting behind a very long process, leading to poor average waiting times.
The following video provides a clear, step-by-step walkthrough of an FCFS problem, exactly like one you might see in the GATE exam.
L-2.3: First Come First Serve(FCFS) CPU Scheduling Algorithm with Example
This Gate Smashers video will guide you through constructing a Gantt chart for FCFS and calculating all the essential performance metrics.
Pay close attention to the following stages: Setup and Rules: Understand the FCFS criteria and the non-preemptive mode (intro). Gantt Chart Construction: Follow the construction of the Gantt chart step-by-step from this section. Notice the crucial point where the CPU is idle because no process has arrived yet. This is a common trap. Calculating Metrics: Observe how Completion Time (CT), Turnaround Time (TAT), and Waiting Time (WT) are systematically calculated for each process using the Gantt chart and the formulas we defined. This is covered from this part. Response Time in FCFS: The video makes an important point that for non-preemptive algorithms like FCFS, the response time is always equal to the waiting time. See why from this explanation. Average Calculations: Finally, see how the average TAT and WT are computed (averages).
2. Shortest Job First (SJF) Scheduling
SJF prioritizes processes with the shortest burst times. This algorithm is provably optimal in terms of minimizing the average waiting time. However, its pure form requires knowing the future (the burst time), which is not always possible in real systems. It comes in two flavors.
a) Non-Preemptive SJF
Once a process starts, it runs to completion. When the CPU becomes free, the scheduler selects the process from the ready queue with the smallest burst time.
- Criterion: Burst Time.
- Mode: Non-preemptive.
- Tie-breaker: If two processes have the same burst time, FCFS is used.
Example: Consider the following processes:
| Process | Arrival Time (AT) | Burst Time (BT) |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 4 |
| P3 | 4 | 1 |
| P4 | 5 | 4 |
Gantt Chart:
- At time
t=0, only P1 is in the ready queue. We must run it. - P1 runs from
t=0tot=7. - At
t=7, the CPU is free. Processes P2, P3, and P4 have all arrived. We check their burst times: P2(4), P3(1), P4(4). - P3 has the shortest burst time (1). So, P3 runs from
t=7tot=8. - At
t=8, the CPU is free. P2 and P4 are remaining, both with burst time 4. We use FCFS as a tie-breaker. P2 arrived before P4, so P2 runs next. - P2 runs from
t=8tot=12. - P4 runs from
t=12tot=16.
The final Gantt Chart is: | P1 (0-7) | P3 (7-8) | P2 (8-12) | P4 (12-16) |
You can then calculate TAT and WT for each process and find the average.
b) Preemptive SJF (Shortest Remaining Time First - SRTF)
This is the preemptive version. The scheduler always chooses the process with the shortest remaining burst time. A running process can be preempted if a new process arrives with a burst time shorter than the current process's remaining time.
- Criterion: Remaining Burst Time.
- Mode: Preemptive.
SRTF problems require careful, step-by-step tracking at every time unit where an event (a process arrival or completion) occurs. This makes them a GATE favorite.
L-2.5: Shortest Remaining Time First (SJF With Preemption) Scheduling Algorithm with Example | OS
This Gate Smashers video tackles a complex SRTF numerical problem. Its detailed, incremental approach is key to avoiding mistakes in preemptive scheduling.
Watch this video carefully: Core Idea: Understand that SRTF is just preemptive SJF (the concept). Gantt Chart Construction: This is the most critical part. Follow the process from the beginning. Notice how at every single time unit where a new process arrives, the scheduler re-evaluates which process has the shortest remaining time and may preempt the current one. Track the remaining burst times on a separate piece of paper as you watch. Completion Time Nuances: Pay special attention to how to find the Completion Time in a preemptive schedule. It's always the time at the end of the last burst of a process. This is explained from this section. Metric Calculations: The rest of the video demonstrates calculating TAT, WT, and RT. Note how RT is now different from WT because of preemption. Watch from this part.
3. Priority Scheduling
In Priority Scheduling, a priority is associated with each process. The CPU is allocated to the process with the highest priority. Like SJF, it can be preemptive or non-preemptive. A common convention in GATE is that a lower number implies higher priority.
- Criterion: Priority.
- Mode: Preemptive or Non-preemptive.
- Tie-breaker: If priorities are equal, use FCFS.
A major problem with priority scheduling is starvation, where low-priority processes may never execute. A solution is aging, where the priority of a process increases gradually over time.
Example (Preemptive Priority):
| Process | AT | BT | Priority |
|---|---|---|---|
| P1 | 0 | 4 | 2 |
| P2 | 1 | 1 | 1 |
| P3 | 2 | 5 | 3 |
| P4 | 3 | 2 | 1 |
Gantt Chart:
- At
t=0, only P1 is available. P1 starts running. - At
t=1, P2 arrives. P2's priority (1) is higher than P1's (2). P1 is preempted (remaining BT=3). P2 starts running. - P2 runs from
t=1tot=2and completes. - At
t=2, the ready queue has P1 (Priority 2). P3 also arrives (Priority 3). P1 has higher priority, so P1 resumes. - At
t=3, P1 is running. P4 arrives. P4's priority (1) is higher than P1's (2). P1 is preempted again (remaining BT=2). P4 starts running. - P4 runs from
t=3tot=5and completes. - At
t=5, the ready queue has P1 (Priority 2, remaining BT=2) and P3 (Priority 3, BT=5). P1 has higher priority and resumes. - P1 runs from
t=5tot=7and completes. - Finally, P3 runs from
t=7tot=12.
Gantt Chart: | P1(0-1) | P2(1-2) | P1(2-3) | P4(3-5) | P1(5-7) | P3(7-12) |
4. Round Robin (RR) Scheduling
Round Robin is a preemptive algorithm designed for time-sharing systems. Each process gets a small unit of CPU time, a time quantum (or time slice), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.
- Criterion: Time Slice.
- Mode: Preemptive.
If the time quantum is very large, RR behaves like FCFS. If it's very small, the overhead from frequent context switches can degrade performance.
Example (Time Quantum q=2):
| Process | AT | BT |
|---|---|---|
| P1 | 0 | 5 |
| P2 | 1 | 3 |
| P3 | 2 | 4 |
Gantt Chart:
t=0-2: P1 runs for 2 units. P1 is preempted (remaining BT=3) and goes to the back of the queue. Ready Queue:[P2, P3, P1]t=2-4: P2 runs. It arrived at t=1, P3 at t=2. P2 runs for 2 units, preempted (remaining BT=1). Ready Queue:[P3, P1, P2]t=4-6: P3 runs for 2 units, preempted (remaining BT=2). Ready Queue:[P1, P2, P3]t=6-8: P1 runs for 2 units, preempted (remaining BT=1). Ready Queue:[P2, P3, P1]t=8-9: P2 runs for 1 unit and completes. Ready Queue:[P3, P1]t=9-11: P3 runs for 2 units and completes. Ready Queue:[P1]t=11-12: P1 runs for 1 unit and completes.
Gantt Chart: | P1(0-2) | P2(2-4) | P3(4-6) | P1(6-8) | P2(8-9) | P3(9-11) | P1(11-12) |
Summary of Key Takeaways
You have now learned the mechanics of four fundamental CPU scheduling algorithms, a high-yield topic for GATE.
- Gantt Chart is Key: Always start by drawing the Gantt chart. It is the single source of truth from which all metrics can be derived.
- Preemption vs. Non-preemption: This is the most important distinction. In preemptive algorithms (SRTF, Preemptive Priority, RR), you must re-evaluate the scheduling decision whenever a new process arrives.
- Formulas are Simple: The calculations for TAT and WT are straightforward once you have the Completion Times from the Gantt chart. The complexity lies in creating the Gantt chart correctly.
- Practice is Everything: The only way to master these is by solving numerous numerical problems. Pay attention to arrival times and tie-breaking rules.
In our next lesson, we will move from managing time to managing space. We will explore Memory Management, another core OS function. We'll delve into concepts like paging, segmentation, and the crucial idea of virtual memory, which allows processes to be larger than the physical RAM available.