Sub topic 3
What a Process Is and How It Sits in Memory
Assume you know basic C, pointers, virtual memory, and the difference between a CPU register and main memory. You should also be comfortable reading hexadecimal addresses.
A process is a running program
A program is a passive file containing instructions and data. A process is an active instance of that program. It includes:
- The program instructions and data.
- The current value of the program counter.
- CPU register values.
- A private virtual address space.
- Operating-system information such as its process identifier, or PID.
- Resources such as open files.
Two processes can run the same program. Each process has its own execution state and normally cannot directly read or modify the other process's memory.
The operating system gives each process a virtual address space. A virtual address is an address used by the process. The memory-management unit translates it to a physical address in RAM, often through page tables.
Typical process memory layout
A process address space is divided into regions. Exact locations vary between operating systems, compilers, and runs.
| Region | Common contents | Usual behavior |
|---|---|---|
| Text | Compiled instructions | Readable and executable |
| Read-only data | String literals and constants | Readable, usually not writable |
| Data and BSS | Initialized and zero-initialized global variables | Readable and writable |
| Heap | Memory from `malloc` | Grows as requested |
| Stack | Function frames and local variables | Changes with calls and returns |
The stack and heap often grow toward each other, but this is a convention rather than a universal physical layout. Shared libraries, memory-mapped files, and dynamically loaded regions may occupy additional areas.
Observing different objects
The following program prints addresses for objects in several regions. Do not compare the numerical ranges as a required rule. Address-space layout randomization can change them between executions.
#include <stdio.h>
#include <stdlib.h>
int global_initialized = 7;
int global_zero;
int main(void) {
static int static_local = 8;
int stack_local = 9;
int *heap_value = malloc(sizeof *heap_value);
if (heap_value == NULL) {
return 1;
}
*heap_value = 10;
printf("code: %p\n", (void *)main);
printf("global: %p\n", (void *)&global_initialized);
printf("bss: %p\n", (void *)&global_zero);
printf("static: %p\n", (void *)&static_local);
printf("stack: %p\n", (void *)&stack_local);
printf("heap: %p\n", (void *)heap_value);
free(heap_value);
return 0;
}A process can have multiple threads, but those threads share the process's address space. The process also has execution metadata maintained by the kernel. That metadata is examined when the process changes state or gives up the CPU.
Exercise
Where would an automatic variable declared inside a function usually be stored, and where would memory returned by malloc usually be stored?
Show answer
The automatic variable usually resides in the stack region. Memory returned by malloc resides in the heap region until it is released.