You can open the same text editor twice. Two windows, two documents, and typing in one does nothing to the other. Yet there is only one editor file on your disk. So what, exactly, are the two things on your screen?
Both are processes, and the file they came from is a program. A program is a file: a list of instructions and some starting data, sitting on disk doing nothing. A process is a program that is running: the instructions are loaded into memory, the CPU is working through them, and the kernel keeps a record of it. One program can be many processes at once, just as one recipe can be many dinners.
Every process gets its own address space
When the kernel starts a process, it gives it an address space: the range of memory addresses the process is allowed to use. To the process it looks like a private, empty machine. Two processes can both use the address 0x1000 and never see each other’s data (Module 6 shows how the hardware pulls that trick off).
Inside the address space, the kernel arranges four regions:
- Text: the program’s machine code. Read-only.
- Data: global variables, which exist for the whole run.
- Heap: memory you ask for while the program runs, with
mallocin C. It grows towards higher addresses. - Stack: one frame per function call, holding that call’s local variables and its return address. It grows towards lower addresses.
The heap and the stack grow towards each other, and the free gap between them is what both can use. Press play and watch a small C program use all four. The addresses are samples; what matters is which region each thing lands in, and which way each region grows.
A small C program, region by region
-
The program is loaded
The kernel reads the program file and builds an address space for it. The first region holds the program's machine code, called the text. It is read-only: the program cannot change its own instructions.
- Data
- 0 variables
- Heap
- 0 blocks
- Stack
- 0 frames
-
Globals go in the data region
Global variables exist for the whole run, so they get a fixed place in the data region: total, count. Their addresses do not change while the program runs.
- Data
- 2 variables
- Heap
- 0 blocks
- Stack
- 0 frames
-
main starts: the first stack frame
Running a function needs room for its local variables and its return address. That room is a stack frame, and the first one belongs to main. The stack sits at the top of the address space.
- Data
- 2 variables
- Heap
- 0 blocks
- Stack
- 1 frame
-
malloc(16) takes 16 bytes from the heap
malloc asks for memory that stays until the program frees it. The heap block lands above the previous one, so the heap grows upwards. The pointer p1 itself is a local variable in main's frame.
- Data
- 2 variables
- Heap
- 1 block
- Stack
- 1 frame
- Heap in use
- 16 bytes
-
malloc(32) takes 32 bytes from the heap
malloc asks for memory that stays until the program frees it. The heap block lands above the previous one, so the heap grows upwards. The pointer p2 itself is a local variable in main's frame.
- Data
- 2 variables
- Heap
- 2 blocks
- Stack
- 1 frame
- Heap in use
- 48 bytes
-
Call work(3): a new frame
main calls work(3). The call pushes a new frame, and it goes below main's frame: the stack grows downwards, towards the heap.
- Data
- 2 variables
- Heap
- 2 blocks
- Stack
- 2 frames
-
Call work(2): a new frame
work calls itself with 2. Every call pushes another frame, each one lower than the last.
- Data
- 2 variables
- Heap
- 2 blocks
- Stack
- 3 frames
-
Call work(1): a new frame
work calls itself with 1. Every call pushes another frame, each one lower than the last.
- Data
- 2 variables
- Heap
- 2 blocks
- Stack
- 4 frames
-
Call work(0): a new frame
work calls itself with 0. Every call pushes another frame, each one lower than the last.
- Data
- 2 variables
- Heap
- 2 blocks
- Stack
- 5 frames
-
The calls return: frames are popped
When a function returns, its frame is simply given up. The same memory is reused by the next call. Only main's frame is left.
- Data
- 2 variables
- Heap
- 2 blocks
- Stack
- 1 frame
-
main returns: the process ends
When main returns, the program exits and the kernel takes back every region: text, data, heap and stack. Memory you forgot to free is returned too, but only now.
- Data
- 2 variables
- Heap
- 2 blocks
- Stack
- 1 frame
At the deepest call the gap between heap and stack was at its smallest. When the calls returned, the frames were popped and that room was free again. Press Try it to change the number of globals, malloc calls and nested calls, and watch which region reacts.
Two things are worth noticing. First, p1 and p2 are pointers: small variables that hold the address of something else. The pointers live on the stack, the 16 and 32 bytes they point to live on the heap. Second, a function’s local variables are gone after it returns, because the frame is simply given up. Memory from malloc stays until you call free.
Check it on a real machine
This C program puts one thing in each region and prints where it landed. Do not worry about the exact syntax: counter is a global, local is a local variable, malloc(64) asks for a heap block, and main is a function, so its address is in the text region. The program then compares the addresses itself.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int counter = 7; /* a global: data */
int main(void) {
int local = 1; /* a local: stack */
int *block = malloc(64); /* a heap block */
uintptr_t code = (uintptr_t)main;
uintptr_t data = (uintptr_t)&counter;
uintptr_t heap = (uintptr_t)block;
uintptr_t stack = (uintptr_t)&local;
printf("code (main) %#lx\n", (unsigned long)code);
printf("data (counter) %#lx\n", (unsigned long)data);
printf("heap (malloc) %#lx\n", (unsigned long)heap);
printf("stack (local) %#lx\n", (unsigned long)stack);
printf("code below data: %s\n", code < data ? "yes" : "no");
printf("data below heap: %s\n", data < heap ? "yes" : "no");
printf("heap below stack: %s\n", heap < stack ? "yes" : "no");
free(block);
return 0;
}
Output (one run)
code (main) 0x5566e8578159 data (counter) 0x5566e857b028 heap (malloc) 0x5566fff0e010 stack (local) 0x7fffd351f31c code below data: yes data below heap: yes heap below stack: yes
The four addresses above are from one run on the build machine, and yours will be different: Linux deliberately shifts the regions to new random places every time a program starts. That is called address space layout randomisation (ASLR), and it makes attacks that rely on knowing where things are much harder. The order of the regions stays the same, and that is why the three comparisons always say yes.
In the real world
Every Linux process has a file that shows its regions, /proc/PID/maps, and a process can read its own as /proc/self/maps. This program does exactly that and prints only the heap and the stack. The numbers are the start and end of each region, and they will differ on every run.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *block = malloc(64); /* make sure the heap exists */
char line[256];
FILE *f = fopen("/proc/self/maps", "r");
while (f && fgets(line, sizeof line, f)) {
unsigned long lo, hi;
if (strstr(line, "[heap]") || strstr(line, "[stack]")) {
sscanf(line, "%lx-%lx", &lo, &hi);
printf("%-7s %#lx to %#lx\n", strstr(line, "[heap]") ? "[heap]" : "[stack]", lo, hi);
}
}
if (f) fclose(f);
free(block);
return 0;
}
Output (one run)
[heap] 0x556c19d6e000 to 0x556c19d8f000 [stack] 0x7ffde7299000 to 0x7ffde72bb000
On your own machine, cat /proc/self/maps shows the whole list, including the program’s text and data and the shared libraries it uses. Run it twice and compare: the addresses change. Windows and macOS randomise too. :::
One program, two processes
Start the same program twice and you get two processes with two completely separate address spaces. Each has its own data region with its own copy of every global, its own heap and its own stack. If one changes a global, the other never notices. They only share the program’s text, and that is read-only, so the kernel can keep a single copy of it in memory for both.
Quick check
Quick check
-
Every call pushes a frame. The stack starts at the top of the address space and grows down, towards the heap.
-
A global exists from before main starts until the program ends, so it has a fixed place in the data region. Locals go on the stack and malloc blocks go on the heap.
-
The file is one program. Each time you run it the kernel builds a new process with its own text, data, heap and stack, and neither can see the other's variables.
-
This is ASLR. The order of the regions stays the same, but where they start is chosen at random each run.
Recap
- A program is a file. A process is a program that is running, with its own address space.
- The address space has four regions: text (code), data (globals), heap (
malloc, grows up) and stack (one frame per call, grows down). - The free gap between heap and stack is shared by both, and shrinks as they grow.
- Pointers are variables. A pointer can live on the stack and point at a heap block.
- Each process has its own address space, and the kernel randomises where the regions start (ASLR).
Next: the kernel needs more than an address space to manage a process. It keeps a record of everything about it, and moves the process between states.
Join the discussion