OS Fundamentals + Processes & Threads
OS Fundamentals + Processes & Threads
🎯 After Reading This Lesson
By the end of this lesson, you will be able to confidently explain the following three things.
- ▸✅ The four core responsibilities of an OS (process, memory, file system, I/O)
- ▸✅ User Mode vs Kernel Mode + syscall
- ▸✅ Why Node.js is fast despite being single-threaded
Keep the learning goals as a checklist and close the lesson only once you can answer all of them.
4 Things an OS Does
In one line: Operating System (OS) = a software layer that abstracts processes, memory, files, and I/O on top of hardware.
4 Core Responsibilities of an OS:
Boot → Launch KakaoTalk flow (6 steps):
1. Power ON — BIOS/UEFI → bootloader → kernel load
2. Kernel initialization — memory mapping, driver load, scheduler start
3. init process — PID 1, parent of all user processes (systemd / launchd)
4. Login shell — bash/zsh starts, environment variables loaded
5. App launch — fork() + exec("/Applications/KakaoTalk.app/...") → new process
6. Event loop — waiting for and handling keyboard/network input
> 💡 Launching KakaoTalk once = the OS silently performs the 6 steps above. Every time, in under 0.5 seconds.
Process vs Thread — Two Models of Concurrency
When to use what:
- ▸Process isolation: stability first (Chrome per-tab process), security isolation (Docker container), inter-language integration
- ▸Thread usage: lightweight concurrency (per-request thread in web servers), shared data processing (GUI event loop)
- ▸Modern trends: virtual threads (Java 21), goroutines (Go), async/await (Python, Node) — lightweight concurrency even lighter than threads
Common mistakes:
- ▸❌ Spawning 1,000 threads → context switch cost explodes
- ▸❌ Modifying shared data without a lock → race condition
- ▸✅ Use thread pool (ThreadPoolExecutor) + lock or async/await
Context Switching — The *Real Cost* of Concurrency
6 Steps of Context Switching (CPU switching from process A → B):
1. Interrupt — timer, I/O completion, or system call
2. Save A's state — registers, PC, stack pointer into the PCB (Process Control Block)
3. Invoke scheduler — select the next process to run (CFS, O(1), real-time)
4. Load B's PCB — restore registers and MMU mappings
5. Cache invalidation — partial flush of L1/L2 cache and TLB (Translation Lookaside Buffer)
6. Resume B — continue from where it was paused
Cost:
Overhead pitfalls:
- ▸❌ Too many threads (1000+) → CPU spends more time switching than doing real work
- ▸❌ CPU-bound work inside async code → blocks the main thread
- ▸✅ Recommended thread count ≈ number of CPU cores (roughly N + 1)
> 💡 Why is async fast? = Multiple tasks on a single thread with no context switching (event loop).
System Calls · Interrupts · Context Switching — One Page
User Space vs Kernel Space
OS memory has 2 regions:
- ▸User Space — where our app runs. Limited privileges. No direct access to memory, disk, or network.
- ▸Kernel Space — where the OS itself lives. Full hardware privileges.
For a user app to open a file — it cannot access the disk directly. It must request the OS to do it. That request is a system call.
System Call
open / read / write / close / fork / exec — approximately 300 on Linux. Every I/O or process creation is a syscall.
All file/network calls in high-level languages (Python, Java) internally invoke syscalls.
The Cost of a syscall
The transition from user space → kernel space is itself expensive (on the order of microseconds). That is why:
- ▸Buffering — instead of a syscall every time, accumulate and flush at once.
BufferedReader, the internal buffer ofconsole.log. - ▸Async I/O — Node.js, async/await — process other work while waiting for a syscall.
Interrupt vs Polling
Polling — Keep Asking
Wastes CPU time. Busy waiting.
Interrupt — Notify Me
When hardware sends a "ready!" signal, the CPU handles it immediately:
- ▸Keyboard input — the moment a key is pressed, an IRQ fires → kernel converts it to an event → delivers it to the app
- ▸Network packet arrival — NIC fires an interrupt → kernel buffers it → wakes the app
- ▸Timer expiry — the foundation of
setTimeout
"Polling is inefficient; interrupts are the standard" — nearly all I/O in modern OSes is interrupt-based.
Context Switching
When the CPU switches between processes/threads — it saves the current state (registers, PC, memory mappings) and restores the state of the next task.
Cost
- ▸Process switching — must also swap memory mappings. Expensive (~several μs).
- ▸Thread switching — shares the same memory. 5–10x lighter than process switching.
- ▸Function call — simple stack push. Nanosecond range.
Therefore — multithreading is faster than multi-processing.
Why Node.js Is Fast Despite Being Single-Threaded
Node.js "single thread + event loop":
Core idea:
1. I/O consumes 99% of the time (DB, API, disk)
2. While waiting on I/O, the main thread handles other requests
3. Context switching cost is nearly zero — there is only one thread
The "one thread per request" model (e.g., Apache): 10,000 requests = 10,000 threads = context switching explosion + memory explosion.
Node model: 10,000 requests = 1 main thread + 4–8 worker threads + event queue. Incomparably more efficient.
Summary — Connecting to Vibe Coding
- ▸fetch / file read = syscall → expensive → minimize (batching, caching)
- ▸Node.js / async/await = event-driven → excels at I/O-heavy servers
- ▸CPU-intensive work → Worker Thread → don't block the main event loop
If you are asked in an interview "Why is Node.js fast?" — recite the contents of this page and you will pass.
🤖 Try Asking AI Like This
Knowing the concepts in this lesson lets you give AI specific instructions. Instead of a vague "fix this," you make vocabulary-driven requests — that is the starting point of saving tokens.
- ▸"Show me the command to trace syscalls for this operation using strace (Linux)"
- ▸"Diagnose whether this code incurs high cost in user space or kernel space"
Why This Reduces Tokens
When you don't know the concepts, even after receiving an AI answer you have to ask "What does that mean?" again. That follow-up question eats up tokens. Learn the concept once, and the conversation ends in a single exchange.