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!
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.
Currently Running: Music Player (Spotify)
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?
- 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.
- 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?
- Timer Interrupt: A hardware timer chip fires an electronic interrupt signal to the CPU every few milliseconds.
- 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).
- 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.
- 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.
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 editLearning graph
Discussion
Loading discussion…
Sign in to ask a question.