Cpu Scheduling Algorithms

Scheduling Algorithms in Operating System | by Vaishnavi Sonwalkar | Medium

CPU Scheduling Algorithms in Operating System

In our last post, we explored the fundamental concepts of Processes and Threads. Now, it’s time to dive deeper into how the Operating System decides which process gets the CPU next when multiple are ready to run. This critical decision is governed by what we call CPU Scheduling Algorithms.

🔄 What is CPU Scheduling?

The CPU is a limited resource that must be allocated efficiently among competing processes. When more than one process is in the ready queue, the Operating System uses a CPU scheduling algorithm to determine which one to execute first.

Efficient CPU scheduling improves system performance by increasing CPU utilization, minimizing waiting and turnaround times, and providing fairness to all processes.

⏱ Types of CPU Scheduling Algorithms

Here are the most widely used scheduling techniques in Operating Systems:

  1. FCFS (First Come First Serve)
  2. SJF (Shortest Job First)
  3. Priority Scheduling
  4. Round Robin
  5. Multilevel Queue Scheduling

1️⃣ FCFS (First Come First Serve)

In FCFS, the process that arrives first is executed first. It works like a queue in a bank – first in, first out.

P1 |——| P2 |———-| P3 |—-|

Example: Suppose three processes arrive in the order P1 (5s), P2 (10s), P3 (3s). The CPU will handle them in this exact order, without any preemption.

Drawback: If a long process arrives first, shorter ones must wait, leading to the “Convoy Effect”.

2️⃣ SJF (Shortest Job First)

This algorithm selects the process with the smallest burst time (execution time) first. It’s highly efficient for reducing average waiting time.

P3 |—-| P1 |——| P2 |———-|

Example: If P1 = 7s, P2 = 15s, and P3 = 3s, SJF will schedule them as P3 → P1 → P2.

Pros: Gives optimal average waiting time.
Cons: Can lead to starvation of longer jobs if many shorter ones keep coming.

3️⃣ Priority Scheduling

Each process is assigned a priority, and the one with the highest priority runs first. If two processes have the same priority, FCFS is used as a fallback.

P2 (High) |—-| P1 (Med) |——| P3 (Low) |———-|

Real-life analogy: Emergency rooms treat patients based on severity, not arrival time.

Issue: Like SJF, it may lead to starvation of lower-priority processes.

4️⃣ Round Robin

This is one of the fairest scheduling techniques. Every process gets a small fixed time slice (called a quantum). If it doesn’t finish in that time, it’s sent to the back of the queue.

P1 |–| P2 |–| P3 |–| P1 |–| P2 |–| P3 |–|

Example: Used in time-sharing systems where multiple users interact with the system simultaneously.

Pros: Good for fairness and responsiveness
Cons: Choosing the right time quantum is tricky – too small = too many context switches; too large = behaves like FCFS.

5️⃣ Multilevel Queue Scheduling

Processes are grouped into different queues based on their type (foreground, background, system). Each queue may use a different scheduling technique.

Example: Foreground queue (uses Round Robin), Background queue (uses FCFS).

This method separates critical processes from normal ones and improves overall performance.

📊 Comparison Table

Algorithm Preemptive Fairness Best For
FCFS No Low Simple batch systems
SJF No / Yes Medium Reducing waiting time
Priority Yes Low Time-critical tasks
Round Robin Yes High Time-sharing systems
Multilevel Queue Yes Medium Hybrid scheduling

📌 Summary

  • CPU Scheduling decides the execution order of processes in the ready queue.
  • Algorithms like FCFS, SJF, and Round Robin offer different benefits depending on the system needs.
  • The choice of algorithm depends on performance goals like speed, fairness, and priority handling.

Leave a Reply

Your email address will not be published. Required fields are marked *