# Processes and Threads

> Learn what a process is, how threads differ, how the OS switches between them, and the role of system calls and the kernel. Free notes, key concepts, resources...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/processes-and-threads
Last updated: 2026-11-28

Learn what a process is, how threads differ, how the OS switches between them, and the role of system calls and the kernel.

Beginner · about 6 hours · difficulty 2/5

An operating system manages hardware and gives programs a clean, isolated environment. Most interview OS questions start here, so get the vocabulary precise.

### Core ideas

- **Kernel vs user mode**: user programs cannot touch hardware directly; they ask the kernel via **system calls** (`read`, `write`, `fork`, `exec`). Switching into kernel mode has a cost.
- **Process**: a running program with its own address space (code, data, heap, stack), open files and a **Process Control Block** (PCB) holding its state, registers and scheduling info.
- **Process states**: new, ready, running, waiting (blocked), terminated. Be able to draw the transitions.
- **Thread**: a unit of execution inside a process. Threads share code, heap and open files but each has its own stack and registers. Creating and switching threads is cheaper than processes.
- **Context switch**: saving one task's registers and loading another's. It is pure overhead, which is why too many threads can slow a system down.
- `fork()` creates a child process that is a copy of the parent (with **copy-on-write** pages); `exec()` replaces the program. A classic question: how many times does "hello" print after three `fork()` calls? (8.)

### Also know

- Zombie process (finished, parent has not called `wait`) vs orphan process (parent died first).
- User-level vs kernel-level threads, and multithreading models.
- Inter-process communication: pipes, shared memory, message queues, sockets, signals.

Good enough to move on: you can explain process vs thread with what each shares, draw the state diagram, and describe what happens during a context switch.

## Key concepts

- **Kernel, user mode and system calls**
- **Process, PCB and address space**
- **Process states and transitions**
- **Threads vs processes**
- **Context switching**
- **fork, exec and wait**
- **Zombie and orphan processes**
- **Inter-process communication**

## Mini project

On Linux or WSL, write a C program that forks children, has each run a different command via exec, and waits for them; then write a two-thread program with pthreads or std::thread and print which values are shared and which are per-thread.

## Check yourself

What exactly do two threads of the same process share, what do they not share, and why is a thread switch cheaper than a process switch?

## Free resources

- [Operating Systems: Three Easy Pieces](https://pages.cs.wisc.edu/~remzi/OSTEP/) (OSTEP)
- [MIT 6.S081 / 6.1810 Operating System Engineering](https://pdos.csail.mit.edu/6.828/) (MIT)
- [GeeksforGeeks: Operating Systems tutorial](https://www.geeksforgeeks.org/operating-systems/) (GeeksforGeeks)

Previous: [Behavioral Patterns](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/behavioral-patterns) · Next: [CPU Scheduling](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpu-scheduling)

## More in Operating Systems

- [Processes and Threads](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/processes-and-threads)
- [CPU Scheduling](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpu-scheduling)
- [Concurrency and Synchronization](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/concurrency-synchronization)
- [Deadlocks](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/deadlocks)
- [Memory Management and Virtual Memory](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/memory-management)
- [File Systems, Storage and I/O](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/file-systems-and-io)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
