Sub topic 3
What a Process Is and How It Sits in Memory
This chapter assumes you can read basic C (functions, pointers, malloc) and know that a CPU has registers and a program counter. You do not need the earlier chapters on system calls in detail. You only need to know that the kernel runs programs on the CPU.
Program versus process
A program is a passive file on disk: machine code plus initial data, for example /bin/ls. A process is a program in execution. It is an active entity with its own program counter, registers, memory, and open files.
One program can back many processes. If two users each run ls, there are two processes and one program file. Each process has its own state and its own copy of the writable data.
A process consists of:
- The code being executed (the text section).
- The current activity: the program counter and the register contents.
- A stack for function calls and local variables.
- A data section for global variables.
- A heap for memory allocated at run time.
- Kernel-side bookkeeping, such as open file descriptors. A later chapter covers the Process Control Block (PCB), the kernel structure that holds this bookkeeping.
The virtual address space
Each process sees its own private, contiguous range of addresses called its virtual address space. The hardware and the kernel translate these virtual addresses to physical memory. Two processes can both use address 0x601040 without clashing, because the address maps to different physical locations in each. This also gives isolation: a process cannot name another process's memory.
The usual layout puts low addresses at the bottom and high addresses at the top:
The diagram is ordered from the highest address (top) to the lowest address (bottom). The stack and the heap grow toward each other across the unused gap.
What lives in each region
| Region | Contents | Size fixed at | Typical permission |
|---|---|---|---|
| Text | Compiled instructions, often string literals too | Load time | Read, execute |
| Data | Globals and `static` variables with a nonzero initial value | Load time | Read, write |
| BSS | Globals and `static` variables without an initializer, or initialized to zero | Load time | Read, write |
| Heap | Blocks returned by `malloc`, `calloc`, `realloc` | Run time | Read, write |
| Stack | Activation records (frames) for each active function call | Run time | Read, write |
BSS (Block Started by Symbol) takes no space in the executable file, only a recorded size. The loader zero-fills that much memory at start-up. This is why an uninitialized global is guaranteed to be 0 in C.
The stack
Every function call pushes a stack frame (also called an activation record). A frame typically holds:
- the function's parameters,
- the return address,
- the saved registers,
- the local variables.
Returning from the function pops the frame. Because frames are pushed and popped in last-in, first-out order, recursion works naturally: each call gets its own copy of the locals.
The stack is bounded, commonly 8 MB on Linux. Infinite or very deep recursion exhausts it, which is a stack overflow. The process then typically receives a segmentation fault.
The heap
Heap memory is requested explicitly and lives until it is freed or the process ends. It does not disappear when the function that allocated it returns. Two common errors follow from this:
- A memory leak: allocating without calling
free. - A dangling pointer: using a block after it has been freed.
The heap grows upward as the allocator asks the kernel for more memory, using system calls such as brk or mmap.
Observing the layout
This program prints one address from each region.
#include <stdio.h>
#include <stdlib.h>
int initialized = 10; /* data */
int uninitialized; /* bss */
void show(int depth) {
int local = depth; /* stack */
printf("stack, depth %d: %p\n", depth, (void *)&local);
if (depth < 2) {
show(depth + 1);
}
}
int main(void) {
int *heap = malloc(sizeof *heap); /* heap */
if (heap == NULL) {
return 1;
}
printf("text (main): %p\n", (void *)main);
printf("data: %p\n", (void *)&initialized);
printf("bss: %p\n", (void *)&uninitialized);
printf("heap: %p\n", (void *)heap);
show(0);
free(heap);
return 0;
}On a typical 64-bit Linux system you will see the following:
- The text address is the lowest, followed by data, then bss, then heap.
- The stack addresses are far higher than the others.
- Each deeper call prints a smaller stack address, which shows the stack growing downward.
The exact numbers change on every run because of address space layout randomization (ASLR), a security feature that shifts region base addresses. The relative order does not change.
You can also inspect a compiled file with the size command. It reports the text, data, and bss sizes of the executable, which are exactly the regions fixed at load time.
Where does this variable live?
Placement depends on how a variable is declared. These rules answer most GATE-style questions:
- A global variable goes to data if initialized with a nonzero value, otherwise to BSS.
- A
staticlocal variable behaves like a global for storage. It lives in data or BSS, not on the stack, and it keeps its value between calls. Only its name is local. - An ordinary local variable and a function parameter live in the stack frame.
- A pointer variable follows these rules like any other variable, but the block it points to is separate. In
int *p = malloc(40);, the 8-byte pointerpis on the stack (if local) while the 40 bytes are on the heap. - A string literal such as
"hi"is stored in a read-only region. Writing to it is undefined behavior.char s[] = "hi";instead copies the characters into a writable array.
Common errors
- Returning the address of a local variable. The frame is popped on return, so the pointer dangles.
- Believing two processes share globals. Each process has its own data section. Even after
fork, parent and child changes are invisible to each other. The thread chapter later contrasts this with threads. - Assuming virtual addresses equal physical addresses. Equal virtual addresses in different processes refer to different memory.
- Assuming heap memory is zeroed.
mallocdoes not zero it, butcallocdoes. BSS is zeroed by the loader. - Confusing a program with a process. Loading the same executable twice creates two processes, each with its own address space.
Exercise
A C program declares int a = 5; and int b; at file scope. Inside main it declares static int c;, int d;, and int *e = malloc(100);. State where each of a, b, c, d, the pointer e, and the block that e points to is stored.
Show answer
a is in the data section because it has a nonzero initializer. b is in BSS because it has no initializer. c is a static local, so it is in BSS (it has no initializer). d is an ordinary local, so it is on the stack. The pointer e is a local variable, so it is on the stack. The 100-byte block that e points to is on the heap.
Exercise
Function f is called recursively with no base case and each frame uses 64 bytes. The stack limit is 8 MB. Roughly how many calls succeed, and what happens next?
Show answer
8 MB is 8 × 1024 × 1024 = 8,388,608 bytes. Dividing by 64 gives about 131,072 calls, slightly fewer in practice because each frame also holds a return address and saved registers. After that the stack overflows and the process typically receives a segmentation fault and is terminated.