Processes & Memory
Processes and memory are the heart of every operating system. Understanding how the kernel multiplexes CPU time, isolates memory, and lets processes communicate is key to writing efficient and reliable software.
A process moves through states as it executes: new, ready, running, waiting, and terminated. The scheduler decides which ready process runs next.
| 1 | New → Ready → Running → Waiting → Terminated |
| 2 | ↑_____________| |
On Unix-like systems, new processes are created with fork(), which clones the current process, and exec(), which replaces the process image with a new program.
| 1 | #include <unistd.h> |
| 2 | #include <sys/wait.h> |
| 3 | |
| 4 | int main() { |
| 5 | pid_t pid = fork(); |
| 6 | if (pid == 0) { |
| 7 | execlp("ls", "ls", "-la", NULL); |
| 8 | } else { |
| 9 | wait(NULL); |
| 10 | } |
| 11 | return 0; |
| 12 | } |
Threads are lighter than processes. They share the same address space but have independent stacks and registers. This makes data sharing easy but requires synchronization to avoid race conditions.
info
Virtual memory gives each process the illusion of a large, private address space. The Memory Management Unit (MMU) translates virtual addresses to physical addresses using page tables.
| Term | Meaning |
|---|---|
| Page | Fixed-size memory block (e.g., 4 KB) |
| Page table | Maps virtual to physical pages |
| Page fault | Page not in RAM, must be loaded |
| Swap | Disk space used as overflow RAM |
| TLB | Cache for recent virtual-to-physical translations |
| 1 | Pipes unidirectional byte stream |
| 2 | Named pipes persistent pipe with a file name |
| 3 | Message queues kernel-managed message lists |
| 4 | Shared memory fastest; requires synchronization |
| 5 | Sockets network-capable, local or remote |
| 6 | Signals software interrupts |