Operating Systems · Module 2 — Processes & Threads
Program vs process; the address space
A program is a file. A process is that program while it is running.
Sign in to track your score
gcc is one file on Aisha's 512 GB SSD. One copy. It has been sitting there since she installed it. She compiles matrix.c. Now gcc is PID 2317, using memory and a CPU core.
She opens a second terminal and compiles again. Now there are two of them, PID 2317 and a new one. Same single file on disk. Two separate things in RAM, each with its own half-finished work.
So what exactly got duplicated? The file did not.
Why & what
The difference. A program is a file. A process is that program while it is running.
- Program: passive. It has no "next instruction" because it is not going anywhere. One copy on disk.
- Process: active. It has a next instruction, a set of half-computed values, and a private chunk of the 8 GB of RAM. One per launch.
A useful comparison: the program is a recipe. The process is you, in a kitchen, halfway through the recipe with flour on your hands. Two people can cook the same recipe at once. There is still one recipe.
The address space. When the OS creates a process, it hands it a private range of memory addresses. This is called its address space. It is split into four parts, and every one of them matters later in this course.
- Code — the actual gcc instructions, copied in from the SSD. Marked read-only, so a bug cannot rewrite the program while it runs.
- Data — global variables. Their size is known before the program starts, so this section never changes size.
- Heap — memory the program asks for while running. When gcc builds a tree of everything it read from matrix.c, that tree lives here. The heap grows upward.
- Stack — local variables and the trail of function calls. Every function call pushes a new block on; every return pops it off. The stack grows downward.
The heap and the stack grow toward each other with free space in between. If they ever meet, the process has run out of memory.
Why put them at opposite ends? Because neither one's final size is known in advance. Facing them toward each other lets whichever needs more take more.
Why it is private. Module 1 said the OS is a referee that stops programs harming each other. The address space is how that promise is kept. The compiler's address 5000 and the browser's address 5000 are different physical locations in the 8 GB. The compiler cannot reach the browser's data even by accident, because it has no way to name it. Module 6 shows the hardware trick that makes this work.
How it works
When Aisha runs gcc matrix.c:
- The OS reserves an address space. A fresh range of addresses, belonging to nobody else.
- It copies the code section in from the SSD and marks it read-only.
- It sets up the data section with the global variables the program declared.
- It places an empty stack at the high end and an empty heap at the low end, with free space between them.
- It points the program counter at the first instruction and marks the process ready to run. PID 2317 now exists.

Common confusion
"Two processes running the same program share memory." They do not. Run gcc twice and you get two complete address spaces. Two stacks, two heaps. Neither can see the other's data.
(The OS may quietly share the read-only code section between them to save RAM, since neither can change it. But every writable part is separate.)
"A big heap means a memory leak." Not necessarily. A leak is heap memory the program asked for and then forgot to give back. A growing heap on its own is just a program doing work — gcc reading a large file will grow its heap and then release it all at the end.
Interview angle
"What is the difference between a program and a process?" This is asked in almost every OS interview, and the one-line answer is enough: a program is a passive file on disk; a process is that program in execution, with its own memory and its own place in the instruction stream. The follow-up is usually "what is in a process's memory?" Name the four sections and say one true thing about each. The single detail interviewers listen for is that the stack and heap grow toward each other.
- 1.
Aisha runs gcc in two terminals at once. How many heaps exist?
- 2.
Which section of a process's address space holds the trail of function calls?