Processes & threads
A process is a running program with its own private memory. A thread is a lighter unit of execution that lives inside a process and shares that process's memory with sibling threads.
- Processes are isolated (a crash in one does not kill another); threads in a process are not.
- Creating a thread is cheaper than a process; switching between threads is faster too.
- Threads share memory (fast communication, but needs synchronization); processes need IPC to talk.
CPU scheduling
The CPU can run one thing at a time per core, so the OS scheduler decides who runs next. The goal: keep the CPU busy while feeling responsive to users.
| Algorithm | Idea | Watch out for |
|---|---|---|
| FCFS | First come, first served | A long job blocks everyone (convoy effect) |
| SJF | Shortest job first | Long jobs can starve; needs to know job length |
| Round Robin | Each gets a fixed time slice, then rotate | Time slice too big = FCFS; too small = overhead |
| Priority | Highest priority runs first | Low-priority starvation (fix with aging) |
Deadlocks
A deadlock is when processes wait on each other forever, none can proceed. It needs all four of these conditions at once:
- Mutual exclusion - a resource is held by only one process at a time.
- Hold and wait - a process holds one resource while waiting for another.
- No preemption - resources cannot be forcibly taken away.
- Circular wait - a cycle of processes each waiting on the next.
Because you need all four, breaking any one prevents deadlock. A common practical fix: always acquire locks in a fixed global order, which removes circular wait.
Synchronization: mutex vs semaphore
When threads share data, you must stop them corrupting it by acting at once. That shared, must-be-exclusive code is the critical section.
| Mutex | Semaphore | |
|---|---|---|
| Meaning | A lock: one holder at a time | A counter allowing N at a time |
| Ownership | Owned by the locker (only it unlocks) | No ownership; anyone can signal |
| Use for | Mutual exclusion (one resource) | Limiting access to N resources / signaling |
Virtual memory & paging
Virtual memory gives each process the illusion of a large, private, contiguous memory, even though physical RAM is limited and shared. The OS splits memory into fixed-size pages and maps virtual pages to physical frames.
When a process touches a page that is not in RAM, a page fault occurs and the OS loads it from disk. Too many page faults (constant swapping) is called thrashing, and it destroys performance.
Final quiz
Ten questions across everything above. Aim for 7+ before you call this subject interview-ready.
What to do next
Each section gives you the theory with a real-world analogy, then a short quiz so you can check yourself immediately. At the very end there is a final quiz that scores you out of 10. Do not just read, answer the quizzes out loud too, that is closer to the real interview. Want a concept re-explained? Tap ✦ Ask AI. This is the deep dive companion to the CS Fundamentals guide.