Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Reader activity
0 Useful 2 Views 6 min Read — No ratings
Fundamentals
6 min read

How Does Your CPU Juggle Hundreds of Processes? Inside OS Scheduling Algorithms

This article explains how operating systems schedule processes across limited CPU cores, covering scheduling levels, timing triggers, algorithm goals, and detailed comparisons of FCFS, SJF, Round Robin, Priority, and Multilevel Feedback Queue algorithms with concrete examples and trade-offs.

Why Scheduling Is Needed

Your computer runs hundreds of processes simultaneously, but only has a few CPU cores. The CPU is the most precious resource; if one process hogs it, others freeze — browsers stall, music stops, the mouse becomes unresponsive. The process scheduler's job is to fairly and efficiently allocate CPU time to all ready processes . Think of it like a restaurant queue system: a line of waiting customers, and the system decides who gets seated next. A good system balances efficiency and fairness — it cannot only seat fast eaters, nor make a diner ordering a full banquet wait the same as someone ordering a single bowl of noodles.

Levels of Scheduling

Operating systems actually have multiple scheduling layers:

High-Level Scheduling (Job Scheduling)

Decides which programs are loaded from disk into memory, becoming processes. It controls the degree of multiprogramming (how many processes reside in memory at once). Analogy: deciding how many tables the restaurant accepts today — too many overwhelms the kitchen, too few wastes resources.

Medium-Level Scheduling (Swapping)

Moves temporarily idle processes from memory to disk ( swap out , suspending them) and back into memory ( swap in ) when needed. This relieves memory pressure. Analogy: putting dishes not yet needed into a warming cabinet to free table space for new guests.

Low-Level Scheduling (CPU Scheduling)

This is what is usually meant by "process scheduling" — picking a process from the ready queue and giving it the CPU. It happens most frequently, hundreds or thousands of times per second. Analogy: the chef deciding which dish to cook next.

Scheduling Timing

The scheduler cannot switch processes at arbitrary moments; it triggers only at specific events:

Running → Ready : time slice expires

Running → Blocked : waiting for I/O

Blocked → Ready : I/O completes

New process created or process terminates

The first two cases force a context switch of the currently running process ( preemptive scheduling ); the latter two merely add new members to the ready queue.

Scheduling Algorithm Goals

Different algorithms emphasize different objectives, but overall goals include:

Goal              | Meaning                              | Typical Scenario
------------------|--------------------------------------|----------------
CPU Utilization   | Keep CPU as busy as possible         | Servers
Throughput        | Maximize processes completed per unit| Batch systems
Turnaround Time   | Minimize submit-to-completion time   | Batch
Waiting Time      | Minimize total time in ready queue   | General
Response Time     | Minimize request-to-first-response   | Interactive systems
Fairness          | Each process gets fair CPU share     | General

These goals often conflict . Optimizing for response time may sacrifice throughput; pursuing fairness may reduce efficiency. Different scenarios demand different trade-offs.

First-Come, First-Served (FCFS)

The simplest algorithm — whoever arrives first runs first. Analogy: cafeteria line, first in line gets served first.

Characteristics:

Non-preemptive: once allocated CPU, runs to completion or until it blocks

Simple and seemingly fair

But suffers from the convoy effect : a long job at the front forces a queue of short jobs to wait excessively

Example with three processes:

Process | Arrival | Burst | Completion | Waiting
--------|---------|-------|------------|--------
P1      | 0       | 10    | 10         | 0
P2      | 1       | 2     | 12         | 10
P3      | 2       | 1     | 13         | 10
Average waiting time = (0 + 10 + 10) / 3 ≈ 6.67

P2 and P3 run only 1–2 time units but wait 10 units — very inefficient.

Shortest Job First (SJF)

Prioritizes the process with the shortest estimated runtime. Analogy: supermarket express lane for 10 items or fewer — light shoppers go first.

Advantage: Minimizes average waiting time; theoretically optimal.

Disadvantages:

Requires knowing each process's runtime in advance (practically difficult)

Long processes may starve — if short jobs keep arriving, long jobs never get scheduled

Round Robin (RR)

Each process receives a fixed time slice (e.g., 10 ms) and cycles through the queue. If a process doesn't finish within its slice, it goes to the back of the queue. Analogy: taking turns playing one card each in a card game.

Advantage: Fair; every process gets CPU time promptly, yielding good response time.

Disadvantages:

Time slice too small → excessive context-switch overhead

Time slice too large → degrades to FCFS

Priority Scheduling

Each process has a priority; the scheduler always picks the highest-priority process. Analogy: hospital ER — most critical patients treated first, regardless of arrival order.

Problem: Low-priority processes may starve .

Solution: Aging — gradually increase a process's priority the longer it waits. The longer it waits, the higher its priority, guaranteeing it eventually runs.

Multilevel Feedback Queue (MFQ)

This is the algorithm most commonly used in real operating systems, combining the strengths of the above.

Core Idea: Maintain multiple ready queues, each with a different priority and time slice size.

Queue 0 (highest priority): time slice = 1 ms
Queue 1: time slice = 2 ms
Queue 2: time slice = 4 ms
...
Queue n (lowest priority): largest time slice

Rules:

New processes enter the highest-priority queue

If a process uses up its entire time slice in the current queue, it is demoted to the next lower queue

If a process voluntarily blocks (e.g., for I/O) before its slice expires, it returns to the same or a higher priority queue

Lower-priority queues are only scheduled when all higher-priority queues are empty

Effects:

Short jobs finish in the first queue, achieving fast response

Long jobs gradually sink to lower priorities but never starve (they eventually get served)

I/O-bound jobs do not lose priority

Analogy: restaurant with VIP, regular, and waiting areas. New guests start in VIP; quick eaters leave immediately; slow eaters move to regular; even slower to waiting. But even in waiting, they eventually get served.

Software Qualification Exam Key Points

Concept and purpose of process scheduling

Common algorithms: FCFS, SJF, RR, Priority, Multilevel Feedback Queue

Calculation of turnaround time, waiting time, response time

Difference between preemptive and non-preemptive scheduling

Concepts of starvation and aging

Summary

Process scheduling is one of the OS kernel's most core functions. From the simplest "first-come, first-served" to the sophisticated multilevel feedback queue that balances multiple factors, every algorithm has its suitable scenarios and trade-offs. Real-world operating systems (Windows, Linux) all use MFQ or its variants. Remember the core principle: there is no perfect algorithm, only the most appropriate trade-off.

💬 Do you know how many process switches your computer performs every second? The answer might surprise you! Guess in the comments — and like if you found this useful!

For attribution, corrections or content rights, contactadmin@besthub.dev

Report an issue with this article

Discussion 0

Write a comment

Sign in to join the discussion

No public comments on this page. Sign in to comment