Appearance
2. Memory Layout: Stack, Heap, Code Segment, Registers, PC, File Descriptors, IPC
High Address
[ Stack ] grows downward
[ Heap ] grows upward
[ Data Segment ] global/static vars
[ Code Segment ] your actual code
Low Address1. Stack — stores function calls, local variables, return addresses. LIFO, automatically managed (push on call, pop on return). Each thread has its own stack.
2. Heap — dynamic allocation, explicitly requested at runtime (malloc/free in C, new/delete in C++, garbage-collected in Java/Python). Shared across all threads of a process. Forgetting to free → memory leak.
3. Code (Text) Segment — compiled instructions, read-only, shared across threads.
4. Registers — tiny, ultra-fast CPU-internal storage (e.g., RAX for arithmetic results, RSP stack pointer, RBP base pointer). Each thread has its own set — saved/restored on context switch.
5. PC (Program Counter) — holds the address of the next instruction; increments after each instruction, updated on jumps/calls. Per-thread.
6. File Descriptors (FD) — integers representing open files/resources. 0→stdin, 1→stdout, 2→stderr. Shared across threads of the same process.
7. IPC — since processes have isolated memory, they need special mechanisms to talk: pipes, message queues, shared memory, sockets, signals, semaphores. Threads skip all this — they just read/write shared heap directly (with synchronization).
| Concept Per Process Per Thread Where | |||
|---|---|---|---|
| Stack | One per thread | RAM | |
| Heap | Shared | RAM | |
| Code Segment | Shared | RAM | |
| Registers | Per thread | CPU | |
| PC | Per thread | CPU | |
| File Descriptors | Shared | OS Kernel |