Processes
What is a Process?
A process is a program that is currently being executed.
This is one of the most important concepts in Operating Systems.
Program vs Process
A program is a passive set of instructions stored on storage.
A process is that program in execution.
Program ↓ Stored on SSD/HDD ↓ Loaded into RAM ↓ Executed by CPU ↓ Process
Example
Suppose you have:
chrome.exe
When Chrome is just stored on your disk, it is a program.
When you launch Chrome:
Chrome program ↓ Loaded into memory ↓ CPU executes instructions ↓ Chrome process
Process in the Operating System
The OS manages every running process.
For example:
Operating System | +-------------+-------------+ ↓ ↓ ↓ Chrome VS Code Terminal Process Process Process ↓ ↓ ↓ +-------------+-------------+ ↓ CPU
The OS decides:
- Which process gets CPU time
- How much memory a process gets
- Which files it can access
- Which devices it can use
- When it should stop
- How processes communicate
Process Components
A process is not just the program's instructions.
A process generally contains:
+---------------------------+ | Process | +---------------------------+ | Code / Text | | Data | | Heap | | Stack | | CPU Registers | | Program Counter | | Process State | | Resources | +---------------------------+
Let's understand them.
1. Text / Code Section
Contains the executable instructions of the program.
For example:
printf("Hello");
The compiled machine instructions are part of the process's code section.
2. Data Section
Contains global and static variables.
Example:
int count = 10;
count can be stored in the data section depending on how it is defined.
3. Heap
The heap is used for dynamic memory allocation.
For example, in C:
int *p = malloc(sizeof(int));
The dynamically allocated memory comes from the heap.
Conceptually:
Heap ↓ Dynamic memory ↓ Allocated during execution
4. Stack
The stack stores information associated with function calls.
For example:
void function() { int x = 10; }
Local variables and function-call information are commonly associated with the stack.
The stack also keeps track of nested function calls.
5. Program Counter
The Program Counter (PC) stores the address of the next instruction to be executed.
Example:
Instruction 100 → execute Instruction 104 → execute Instruction 108 → next
The PC points to the next instruction.
When the OS switches from one process to another, the current process's CPU state, including the program counter, needs to be saved.
6. CPU Registers
Registers contain temporary information required by the CPU.
Examples include:
- General-purpose registers
- Stack pointer
- Program counter
- Status/flags registers
The exact registers depend on the processor architecture.
Process Memory Layout
A simplified process memory layout looks like:
Higher Memory +-------------------+ | Stack | | ↓ | +-------------------+ | | | Free Space | | | | ↑ | +-------------------+ | Heap | +-------------------+ | Data | +-------------------+ | Code | +-------------------+ Lower Memory
The exact layout differs between operating systems and architectures, but this is a useful conceptual model.
Process States
A process doesn't continuously execute on the CPU.
It moves through different states.
The basic process states are:
- New
- Ready
- Running
- Waiting/Blocked
- Terminated
1. New
The process is being created.
New ↓ Ready
Example:
You launch a program.
2. Ready
The process is ready to run but is waiting for CPU time.
Ready Queue ---------------- Process A Process B Process C
The CPU scheduler chooses one of these processes.
3. Running
The process is currently executing on a CPU core.
Process A ↓ CPU
4. Waiting / Blocked
The process is waiting for an event, usually an I/O operation.
For example:
Process ↓ Request disk data ↓ Waiting ↓ Disk completes ↓ Ready
While waiting for slow I/O, the OS can give the CPU to another ready process.
5. Terminated
The process has finished execution.
Running ↓ Terminated
Process State Diagram
+------+ | New | +------+ | ↓ +-------+ | Ready | +-------+ | Dispatch ↓ +---------+ | Running | +---------+ / \ / \ I/O wait \ Exit ↓ ↓ +---------+ +-----------+ | Waiting | | Terminated| +---------+ +-----------+ | I/O completed | ↓ +-------+ | Ready | +-------+
A running process can also be preempted:
Running ↓ Preemption ↓ Ready
This happens when the scheduler decides another process should run.
Process Control Block (PCB)
The OS needs to keep information about every process.
This information is stored in a structure called the:
Process Control Block (PCB)
The PCB is one of the most important concepts in process management.
A simplified PCB contains:
+-----------------------------+ | Process Control Block | +-----------------------------+ | Process ID (PID) | | Process State | | Program Counter | | CPU Registers | | CPU Scheduling Information | | Memory Management Info | | I/O Status Information | | Open Files | | Accounting Information | +-----------------------------+
Process ID
Every process is normally assigned an identifier.
For example, in Linux:
ps
might show:
PID COMMAND 1201 bash 1532 firefox 2040 code
The PID uniquely identifies a process within the system's process namespace.
Why is PCB Important?
Suppose the CPU is executing Process A:
CPU ↓ Process A
The OS decides to switch to Process B.
It must save Process A's current execution information.
Process A ↓ Save CPU state ↓ PCB of A
Then:
PCB of B ↓ Restore CPU state ↓ CPU ↓ Process B
This is called a context switch.
Context Switch
A context switch occurs when the CPU switches from one process or execution context to another.
Example:
CPU | ↓ Process A | | Save context ↓ PCB A | | Load context ↓ PCB B | ↓ Process B
The saved context may include:
- Program counter
- CPU registers
- Stack pointer
- Processor status information
- Other architecture-specific execution state
Important
Context switching is necessary for multitasking, but it has overhead because the CPU spends time saving and restoring execution state rather than doing application work.
Process Creation
A process can create another process.
The creating process is called the:
Parent process
The new process is called the:
Child process
Example:
Parent Process | ↓ Child Process
A parent can create multiple children:
Parent / | \ ↓ ↓ ↓ Child1 Child2 Child3
Process Creation in Linux
Linux/Unix systems traditionally use mechanisms such as:
fork() exec()
fork()
Creates a new process.
Conceptually:
Parent | fork() | +------+ ↓ ↓ Parent Child
exec()
Replaces the current process image with another program.
For example:
Process ↓ exec() ↓ New program
This is why fork() and exec() are commonly discussed together in Unix process creation.
Parent and Child Processes
Consider:
fork();
After a successful fork(), there are two execution contexts:
Original Process | fork() | +---+---+ ↓ ↓ Parent Child
The child receives a separate process identity.
The exact memory behavior is implementation-dependent, but modern Unix-like systems commonly use copy-on-write so that memory pages are not immediately duplicated unnecessarily.
Process Termination
A process can terminate when:
- It finishes normally.
- It encounters a fatal error.
- Another process or the OS terminates it.
- It explicitly calls an exit mechanism.
Example:
Running ↓ exit() ↓ Terminated
In Unix/Linux, a process can use:
exit(0);
where 0 conventionally indicates successful termination.
Zombie Process
A zombie process is a child process that has terminated, but its parent has not yet collected its termination status.
Simplified:
Child ↓ Terminates ↓ Zombie ↓ Parent calls wait() ↓ Zombie entry removed
The terminated process no longer executes, but a small amount of process information remains so the parent can obtain its exit status.
Orphan Process
An orphan process is a process whose original parent has terminated while the child is still running.
Simplified:
Parent ↓ Terminates ↓ Child still running ↓ Child becomes orphaned
On Unix-like systems, an orphan is typically adopted/reparented by another process designated for this purpose, commonly PID 1 or a relevant subreaper depending on the system.
Process Scheduling
If many processes are ready:
Ready Queue P1 P2 P3 P4 P5
Only a limited number can execute at the same time depending on the number of CPU cores.
The CPU scheduler decides which process should run.
Ready Queue ↓ CPU Scheduler ↓ Selected Process ↓ CPU
Scheduling is a major topic in your roadmap and will be covered in detail later.
Process vs Program
This is a very common exam question.
| Program | Process |
|---|---|
| Passive | Active |
| Stored on storage | Executing/in execution |
| Static | Dynamic |
| Doesn't have a current execution state | Has a state |
| Doesn't require a PCB by itself | Managed using a PCB |
Example: program.exe | Running instance of that program |
Easy example
Microsoft Word installed ↓ Program Microsoft Word currently running ↓ Process
Process vs Thread
A process is a resource-owning execution environment, while a thread is an execution unit within a process.
Example:
Process +-------------------------+ | Code | | Data | | Heap | | Open files/resources | | | | Thread 1 | | Thread 2 | | Thread 3 | +-------------------------+
Multiple threads can belong to the same process and share many process resources.
We'll cover threads in the next topic.
Process Table
The OS maintains information about active processes.
Conceptually:
+------+---------+---------+ | PID | State | Program | +------+---------+---------+ | 101 | Running | Chrome | | 102 | Ready | VS Code | | 103 | Waiting | Music | | 104 | Ready | Bash | +------+---------+---------+
The actual implementation varies by operating system.
Process Resource Ownership
A process may have access to resources such as:
Process | +-- Memory +-- Open Files +-- Network Connections +-- Devices +-- CPU Time +-- Security Credentials
The OS controls these resources according to its policies and permissions.
Process Life Cycle
A complete simplified life cycle is:
Create ↓ +-----+ | New | +-----+ | ↓ +-------+ | Ready | +-------+ | ↓ +---------+ | Running | +---------+ / | \ / | \ I/O Preempt Exit ↓ ↓ ↓ +---------+ | +-----------+ | Waiting | | |Terminated | +---------+ | +-----------+ | | +-------+ ↓ Ready
Real-Life Example
Suppose you open a browser.
Step 1 — Program exists
Browser executable ↓ SSD
Step 2 — You launch it
SSD ↓ OS loads required code/data ↓ RAM
Step 3 — OS creates/manages process execution
Browser Process ↓ PCB ↓ Ready
Step 4 — Scheduler gives CPU
Ready ↓ Running ↓ CPU
Step 5 — Browser requests network data
Running ↓ Network I/O ↓ Waiting
Step 6 — Network response arrives
Waiting ↓ Ready ↓ Running
Step 7 — Browser closes
Running ↓ Terminated
Important Process Terms
| Term | Meaning |
|---|---|
| Process | Program in execution |
| PID | Process identifier |
| PCB | Data structure containing process information |
| Parent | Process that creates another process |
| Child | Process created by another process |
| Ready | Waiting for CPU |
| Running | Currently executing |
| Waiting | Waiting for an event/I/O |
| Terminated | Finished execution |
| Context Switch | Switching CPU from one process/context to another |
| Zombie | Terminated child whose status hasn't been collected |
| Orphan | Child whose original parent has terminated |
Exam Definition
A process is a program in execution, together with its current execution state, memory, CPU context, and allocated resources. The operating system manages processes using structures such as the Process Control Block (PCB).
Remember
Program = passive
Process = program + execution state + resources
And the most important flow:
Program ↓ Process Creation ↓ New ↓ Ready ↓ Running ↓ Waiting ↔ Ready ↓ Terminated