OS Notes
Understanding processes in operating systems — definition, process vs program, process creation, process memory layout, and the fundamental role of processes in modern computing.
Introduction
When you double-click on Chrome, what actually happens? The operating system does not simply "run the program." It creates a process — an active, living entity with its own memory space, resources, and execution state. Understanding processes is fundamental to understanding operating systems because nearly everything the OS does revolves around managing processes.
A process is the OS's unit of work. Just as a factory does not deal with raw blueprints but with active production jobs, the OS does not deal with static programs but with active processes. Every running application — your browser, text editor, music player, even system background tasks — is one or more processes being managed by the operating system.
Process vs Program
This distinction is crucial and appears in every OS exam:
Program: A passive entity — a file containing instructions stored on disk (like chrome.exe or /usr/bin/python3). It is a recipe, a blueprint, a set of instructions waiting to be executed.
Process: An active entity — a program in execution. It includes the program code PLUS the current state (program counter, registers, variables, open files, allocated memory).
| int main() { | Program code | |
|---|---|---|
| int x = 5; | ─Load──→ | x = 5 (current value) |
| x = x + 1; | PC → line 3 | |
| printf("%d", x); | Stack, Heap | |
| } | Open files: stdout |
Key insight: One program can have multiple processes. Opening Chrome three times creates three separate processes, each with its own memory and state. They share the same code but are independent entities.
Process Memory Layout
Every process has a well-defined memory structure divided into segments:
Text Segment
Contains the compiled machine code instructions. This section is read-only (to prevent accidental modification) and can be shared between multiple processes running the same program.
Data Segment
Contains global and static variables. Split into initialized data (variables with explicit initial values) and BSS (Block Started by Symbol — uninitialized globals, zeroed by OS).
Heap
Dynamically allocated memory (via malloc in C, new in C++/Java). Grows upward toward higher addresses. The programmer controls allocation and deallocation.
Stack
Stores function call frames — local variables, return addresses, and function parameters. Grows downward toward lower addresses. Automatically managed by the compiler and runtime.
#include <stdio.h>
#include <stdlib.h>
int global_init = 42; // Data segment (initialized)
int global_uninit; // BSS segment (uninitialized)
int main() { // Text segment (code)
int local_var = 10; // Stack
int *heap_var = malloc(sizeof(int)); // Heap
*heap_var = 20;
printf("Text: %p\n", (void*)main);
printf("Data: %p\n", (void*)&global_init);
printf("BSS: %p\n", (void*)&global_uninit);
printf("Heap: %p\n", (void*)heap_var);
printf("Stack:%p\n", (void*)&local_var);
free(heap_var);
return 0;
}Process Creation
Processes are created in several ways:
- System initialization — Background processes (daemons/services) start at boot
- User request — Double-clicking an icon or running a command
- Process spawning — An existing process creates a new one (fork in Unix, CreateProcess in Windows)
- Batch job initiation — Scheduled tasks or queued jobs
The fork() System Call (Unix/Linux)
In Unix-like systems, new processes are created using fork(), which creates an exact copy of the calling process:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Before fork: PID = %d\n", getpid());
pid_t pid = fork(); // Creates child process
if (pid < 0) {
// Fork failed
perror("Fork failed");
} else if (pid == 0) {
// Child process (fork returns 0 to child)
printf("Child: My PID = %d, Parent PID = %d\n",
getpid(), getppid());
} else {
// Parent process (fork returns child's PID to parent)
printf("Parent: My PID = %d, Child PID = %d\n",
getpid(), pid);
}
return 0;
}After fork(), both parent and child execute the same code from the point of the fork. The return value of fork() tells each process whether it is the parent or child.
Process Termination
Processes end in several ways:
- Normal exit — Process finishes its work (return from main, exit() call)
- Error exit — Process encounters a fatal error and exits voluntarily
- Killed by another process — Parent kills child, or OS kills misbehaving process (kill signal)
- Killed by OS — Segmentation fault, illegal instruction, resource limit exceeded
When a child process terminates before the parent collects its exit status, it becomes a zombie process — it is dead but still has an entry in the process table. When a parent terminates before its children, the orphaned children are adopted by the init process (PID 1).
Real-World Analogy
Think of a program vs process like a recipe vs cooking. A recipe (program) is a piece of paper with instructions. Cooking (process) is the active execution — you have ingredients on the counter (data), you are at a specific step (program counter), and you are using specific equipment (resources). You can cook the same recipe multiple times simultaneously (multiple processes from one program), and each cooking session is independent — burning one dish does not ruin the other.
Key Takeaways
- A process is a program in execution — an active entity with state, memory, and resources
- One program can spawn multiple independent processes
- Process memory is divided into text (code), data (globals), heap (dynamic), and stack (local)
- Processes are created via fork() in Unix or CreateProcess() in Windows
- Each process has a unique Process ID (PID) and a parent-child relationship
- Zombie processes are terminated children whose status has not been collected
- Understanding processes is foundational — scheduling, synchronization, and memory management all deal with processes
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Introduction to Processes.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, process, management, introduction, introduction to processes
Related Operating Systems Topics