Skip to content
Step 7 of 105–7 minutes

How Does a Computer Run Multiple Programs?

Right now, your computer or phone is playing background music, refreshing browser tabs, downloading updates, and watching for mouse clicks—all at the same time. Yet, your machine may have only 4 or 8 CPU cores.

How can a single CPU core execute hundreds of applications without freezing? The secret is an illusion created by the Operating System called Preemptive Multitasking and Time-Sharing.

Why Multitasking Exists

In the 1980s, early operating systems (like MS-DOS) could only run one program at a time. If a program started printing a document or waiting for a slow floppy disk, the entire computer froze and locked you out until it finished! Modern operating systems ensure the CPU never sits idle while waiting.

The Master Chef Analogy

Imagine a solitary master chef in a restaurant kitchen with 3 customer orders: The chef does not finish Order #1 completely before touching Order #2. Instead, they sear the steak for 30 seconds, quickly chop carrots for the soup while the pan sizzles, and check the dessert oven. Because the chef switches between tasks so rapidly, every customer feels like their dish is being cooked continuously!

OS Multitasking Simulator

Single CPU Core Time-Sharing & Context Switching

A single CPU core can only execute one instruction at a time. To run Spotify, VS Code, and Chrome together, the Operating System divides time into tiny slices (milliseconds) and rapidly switches between them.

Physical CPU Core #0

Currently Running: Music Player (Spotify)

Time Slice Remaining: 3 ticksActive PC: 0x00R0: 120R1: 44100
PID: 101DECODE_AUDIO_FRAME [0x40]
Music Player (Spotify)
RUNNING
Process Control Block (PCB)
Saved PC:0x00
Register R0:120
Register R1:44100
Code Instructions:
▶ DECODE_AUDIO_FRAME [0x40]
APPLY_EQUALIZER (Gain +2dB)
SEND_PCM_BUFFER_TO_DAC
POLL_PLAYBACK_CONTROLS
Code Editor (VS Code)
WAITING (READY)
Process Control Block (PCB)
Saved PC:0x00
Register R0:42
Register R1:80
Code Instructions:
▶ POLL_KEYBOARD_BUFFER
RUN_SYNTAX_HIGHLIGHTER
CHECK_LINTER_DIAGNOSTICS
UPDATE_CURSOR_POSITION
Web Browser (Chrome)
WAITING (READY)
Process Control Block (PCB)
Saved PC:0x00
Register R0:200
Register R1:1920
Code Instructions:
▶ FETCH_NETWORK_CHUNK
PARSE_HTML_TOKEN_STREAM
RECALCULATE_DOM_STYLES
PAINT_PAGE_VIEWPORT
Ready. Click "Start CPU Time-Slicing" to watch the CPU multitask between 3 apps.

Process vs Thread: What Is the Difference?

A Process (Isolated House)

A process is an independent running application (like Spotify or Chrome). Each process has its own completely isolated memory address space. If one process crashes or encounters a bug, the operating system terminates it without affecting other running applications.

A Thread (Worker in the House)

A thread is a lightweight path of execution inside a process. All threads of a single process share the exact same Heap and Data segments in memory. For example, in a video game, one thread handles 3D physics while another thread streams background audio simultaneously.

What Is Shared vs. Private in Multi-Threading?

Shared Across All Threads in a Process:
  • Code Segment: All threads run the same program binary.
  • Heap & Data: Dynamically allocated objects and global variables.
  • OS Resources: Open files, network sockets, window handles.
Private to EACH Individual Thread:
  • Program Counter (PC): Each thread tracks its own current instruction.
  • Registers: Each thread holds its own immediate calculation math.
  • Call Stack: Each thread has its own private stack for local variables!

The Secret Weapon: The Context Switch

How does the CPU switch between apps without losing your place?

  1. Timer Interrupt: A hardware timer chip fires an electronic interrupt signal to the CPU every few milliseconds.
  2. Save State: The CPU pauses the active application and copies its Program Counter (PC) and registers into a data structure in RAM called the Process Control Block (PCB).
  3. Restore State: The OS Scheduler picks the next waiting application, loads its saved PCB into the CPU registers, and points the PC to its saved location.
  4. Resume: The next application continues running without ever realizing it was paused!

Common Beginner Misconceptions

  • "Threads share everything inside a process, including local variables."
    Reality: If threads shared local variables, function calls would corrupt each other! While threads share the Heap and Data segments, every thread has its own private Call Stack, its own Program Counter (PC), and its own CPU registers.
  • "A quad-core CPU can only run 4 programs."
    Reality: A quad-core CPU can execute 4 instructions at the exact same physical microsecond, but through preemptive time-slicing it easily juggles 200 processes and 2,000 threads smoothly!
  • "Multitasking has zero performance cost."
    Reality: Every context switch takes time (saving registers to RAM, invalidating CPU caches, switching MMU page tables). If you run too many heavy programs at once, the CPU spends more time switching between apps than doing useful work—a condition called thrashing.
Spot the Misconception — Identify the Incorrect Statement

Which of the following statements about multi-threading and processes is FALSE (a misconception)?

Lesson 07 Key Takeaways

  • Time-sharing divides CPU time into millisecond slices so apps seem to run at once.
  • A Process has an isolated memory space; Threads share memory within a process.
  • Context switching saves and restores registers via the Process Control Block (PCB).
Notessaved locally+

Notes stay in this browser. They are not uploaded.

Suggest an edit

Learning graph

Discussion

0 comments

Loading discussion…

Sign in to ask a question.