Operating Systems: Hiring a "Chief Manager" for Your Computer
Preface
With a perfect CPU and unlimited memory, is a computer ready to use? In the previous chapter, we witnessed how transistors combine to form a powerful CPU. But even with the most top-tier hardware, if you let it work directly, even displaying a single letter on screen would require writing hundreds of lines of obscure machine instructions. Not only is this cumbersome, it's also extremely dangerous — one small mistake and your code could overwrite someone else's data.
To solve these nightmares, the Operating System (OS) was born. It is the most magnificent layer of "software" standing between you and the cold hardware. In this chapter, we'll set aside obscure code and use accessible analogies to see how this "super butler" tames chaotic hardware into submission.
What will you learn from this article?
After completing this chapter, you will gain:
- Troubleshooting ability: When encountering "program frozen" or "out of memory" issues, analyze causes from the operating system level
- Deeper terminology understanding: Understand what problems "multiprocessing," "virtual memory," and "file permissions" solve
- Systems thinking: Understand that programs don't run in isolation — they interact closely with the OS, other processes, and hardware resources
- Foundation for further learning: Build a foundation for concurrent programming, system tuning, and container technology
| Chapter | Content | Core Concepts |
|---|---|---|
| Chapter 1 | Process Management | CPU time-division multiplexing, round-robin scheduling |
| Chapter 2 | Memory Management | Virtual memory, paging mechanism |
| Chapter 3 | File Systems | File organization, directory structure |
0. Big Picture: What Happens Without an Operating System?
Imagine you open a "computing factory" (your computer) with immense potential. The factory has one all-powerful, tireless top worker (CPU), a vast warehouse (memory), and countless shipping containers (hard drives).
If you don't hire a factory manager (operating system):
- CPU Monopoly Crisis: The CPU can only do one thing at a time. If someone is using it to play music, anyone else wanting to browse the web? Sorry — everyone must wait until the music player voluntarily yields the CPU.
- Memory Stampede Accidents: WeChat and a game are both using the warehouse (memory). Without a security guard designating areas, the game might accidentally place its equipment data in WeChat's box, causing WeChat to crash instantly.
- Hard Drive Maze: The hard drive hardware is just giant discs etched with 0s and 1s. To find yesterday's photo, you'd have to precisely remember it's stored at "platter 1, track 56, sector 8." Nobody can memorize such anti-human coordinates.
To solve these three nightmares, the operating system deploys its three magic weapons: process management, memory management, and file systems.
1. Process Management: CPU Time-Division Multiplexing
When you use your computer, you often have WeChat running, music playing, and typing all at the same time. But if your computer actually has only one CPU core, how does it do all three things simultaneously?
The answer: It doesn't do them simultaneously. Instead, the operating system performs frantic "time management."
1.1 What Is a "Process"?
Every running program is called a process. You can think of it as a "project team" with its own code (task list), its own memory data (project budget), queuing up for CPU attention.
1.2 Round-Robin Scheduling
To prevent any rogue software from monopolizing the CPU, the operating system slices CPU time into extremely small segments (about 10 milliseconds) and distributes them to each process in turn. Because the switching is so fast, it feels like "simultaneous execution."
2. Memory Management: Virtual Address Space
Having solved the problem of taking turns using the CPU, the next issue is memory space. Without management, if all software writes data directly to physical RAM, mutual overwriting stampedes are inevitable.
2.1 Virtual Memory
The operating system tells each process a big lie: "Hey, you have exclusive use of all available memory on this entire computer. Use it freely!"
From the process's perspective, its memory space is always contiguous and clean. It peacefully writes data into it.
2.2 Page Table Mapping
In reality? The operating system secretly stuffs data into various fragmented gaps in real physical memory. This has two absolutely genius benefits:
- Absolute security: WeChat can only ever see its own space and can't tamper with other programs' data
- Fragment utilization: No matter how fragmented physical memory gets, the virtual space mapped to each process remains orderly
3. File Systems: Organizing Persistent Storage
If you buy a brand-new hard drive, it's essentially a barren expanse of storage units. If you want to save a photo, the hard drive will only ask: "Tell me which byte you want to store it at?"
3.1 What Does a File System Do?
- Partition the hard drive: Cut the hard drive into countless fixed-size blocks (typically 4KB)
- Maintain a ledger: Record which blocks are full and which are empty
- Translate paths: Convert
D drive/Photos/Pet.jpginto "blocks 3, 7, and 11"
This is why renaming a file is instant (only the name in the ledger changes), while copying a file takes a long time (actual hard drive data blocks must be read and written).
4. Three-Way Coordination: The Complete Process of Launching a Program
We've now learned about the operating system's three major modules separately. Let's see how they work together when you double-click to open a program:
Whether you click a desktop icon or execute print("Hello World") in code, it all depends on this complex behind-the-scenes machinery. The reason we can surf the digital world so effortlessly is entirely because the underlying operating system bears the burden for us.
Further Reading
If you find the operating system's various "management tricks and deception tactics" fascinating, you can explore these advanced topics:
- Processes and Threads: If a process is a project team, then "threads" are the employees doing the work within it
- Concurrency and Locks: When two processes compete for the same resource simultaneously, how to prevent deadlocks
- System Calls: The "service windows" the operating system provides to upper-layer applications