16 min read Beginner to Intermediate Lesson 16 of 29
Part 3 — Software & Operating Systems

Processes, Threads & CPU Scheduling: How Computers Multitask

A typical computer might only have 4 or 8 CPU cores. Yet in the background run 200 processes, 50 browser tabs, music streaming, and security services. How does a computer juggle millions of tasks simultaneously with limited silicon cores?

Task Switching Thousands of Times Every Second!

It feels as though the computer is playing music and compiling code at the exact same instant. In reality, the CPU switches between applications every 5 to 10 milliseconds at blinding speed.

This high-frequency scheduling magic is known as Preemptive Multitasking—orchestrated continuously by the operating system's Scheduler.

1–5. Program vs. Process and Memory Isolation

Program (Inert File)

A collection of stored instructions on disk (such as chrome.exe). It consumes no CPU cycles or active RAM on its own.

Process (Living Entity)

The active instantiation of a program in RAM. Contains its own virtual memory space, code, data, heap, stack, and system resource handles.

Process A (Virtual Memory) ──[❌ Protected Boundary ❌]── Process B (Virtual Memory)

6–9. Thread: The Worker Within a Process

If a process is an entire office building, a Thread is a worker inside that office. All threads share the building's common resources (memory space) but execute tasks independently.

Thread Task Distribution in a Browser:

  • 🎨 UI Thread: Handles mouse clicks, scrolls, and visual responsiveness.
  • 🌐 Network Thread: Downloads images, stylesheets, and web pages in the background.
  • ⚙️ Worker Thread: Processes heavy JavaScript computations and data parsing.

Race Conditions & Locks:

Because threads share identical memory addresses, two threads writing to the same variable simultaneously can corrupt data. To prevent this, developers use synchronization primitives like Mutexes and Locks.

12–13. Concurrency vs. Parallelism

Concurrency

Interleaving multiple tasks rapidly on a single core to make forward progress on all of them.

Parallelism

Executing multiple threads simultaneously on distinct physical CPU cores at the exact same moment in time.

14–18. CPU Scheduler and Context Switch

The OS Scheduler determines how much execution time (Time Slice or Quantum) each thread receives on a CPU core.

How Does a Context Switch Occur?
  1. When Thread A's time slice expires, a hardware timer interrupt fires.
  2. The CPU's registers and Program Counter (PC) are saved into Thread A's context block in RAM (Save State).
  3. The saved register state of the next ready thread (Thread B) is loaded into the CPU (Restore State).
  4. The CPU resumes execution from Thread B's restored Program Counter.
Thread A (Save State) ──(Context Switch)──→ Thread B (Run) ──(Context Switch)──→ Thread A (Resume)

23–27. Process Lifecycle States

NEW (Being Created) │ ▼ ┌───► READY (Waiting in Queue) ◄───┐ │ │ │ Time Slice │ Scheduler Assigns CPU │ I/O Done Expired ▼ │ └──── RUNNING (Executing on Core) │ │ │ I/O Wait / Block │ ▼ │ WAITING (Blocked on I/O) ────┘ │ ▼ TERMINATED (Execution Finished)

31–34. SMT & Hyper-Threading: Two Threads per Core

When a processor specification lists 8 Cores / 16 Threads, it means each physical execution core maintains two sets of register states and instruction pointers—known as Hyper-Threading on Intel and broadly termed SMT (Simultaneous Multithreading).

40–41. Inter-Process Communication (IPC)

Pipes Unidirectional data streams
Sockets Network & local IPC communication
Shared Memory Blazing fast shared buffers
Signals Asynchronous event notifications

46–47. Master Software-to-Hardware Execution Pipeline

USER LAUNCHES APPS │ ▼ OPERATING SYSTEM PROCESSES (Chrome, VS Code, Music Player, Services) │ ▼ THREADS OF EXECUTION │ ▼ OS SCHEDULER & READY QUEUE │ ▼ CPU PHYSICAL CORES (Fetch-Decode-Execute) │ ▼ RAM / CACHE / HARDWARE REGISTERS

🧠 Key Takeaways

  • Program vs Process: A program is passive code stored on disk; a process is an active executing instance in RAM.
  • Thread: An independent path of execution within a process sharing the same address space.
  • Process Isolation: Each process resides within private virtual memory to prevent memory corruption or unauthorized access.
  • CPU Scheduler: Manages time slices and orchestrates sub-millisecond context switches to deliver smooth multitasking.
  • Concurrency vs Parallelism: Rapid interleaving on shared cores vs true simultaneous execution across separate cores.
  • SMT / Hyper-Threading: Duplicates architectural state on physical cores to maximize pipeline utilization and throughput.
🚀 Next Lesson

Lesson 17 — Computer Graphics & GPU: From Binary to Screen Pixels

Learn how millions of pixel colors, 3D rendering pipelines, frame buffers, and thousands of parallel GPU cores generate vivid imagery on your display.