Ever wondered how the computers runs multiple applications smoothly? The secret lies in CPU scheduling – a core function of operating systems that determines which process gets CPU time and when.
What is CPU Scheduling?
CPU scheduling is the process of selecting which ready process executes next on the CPU. The schedular (part of the OS kernel) makes these decisions:
- Maximize CPU utilization
- Minimize waiting time
- Minimize response time
- Maximize throughput
- Ensure fairness among processes
Types of CPU Scheduling
1️⃣ Preemptive Scheduling
- The OS can interrupt and switch out a running process
- Ensures high priority tasks can preempt lower ones.
- Examples: Round Robin, Shortest Remaining Time First.
2️⃣ Non-preemptive Scheduling
- Once a process starts executing, it runs to completion or voluntarily yields.
- Simpler but less responsive.
- Examples: First-Come, First-Served, Shortest Job First
🧮 Popular CPU Scheduling Algorithms
📥 First-Come, First-Served (FCFS)
- Simplest scheduling method.
- Processes are executed in the order they arrive.
- Can cause convoy effect and long wait times.
⚖️ Shortest Job First (SJF)
- Executes the process with the shortest burst time next.
- Optimal in terms of average waiting time.
- Non-preemptive by default (can be made preemptive as SRTF).
🕒 Shortest Remaining Time First (SRTF)
- Preemptive version of SJF.
- A new process with a shorter burst can preempt the current one.
- Better response time but more complex to implement.
🔁 Round Robin (RR)
- Designed for time-sharing systems.
- Each process gets a fixed time slot (quantum).
- Fair and responsive, but performance depends heavily on quantum size.
📊 Priority Scheduling
- Each process is assigned a priority, and the scheduler picks the highest.
- Can be preemptive or non-preemptive.
- Risk of starvation, mitigated by aging techniques.