π§ What is a Process?
A process is a program in execution.
Itβs the running instance of your code β complete with its own memory, state, and system resources.
Think of a process as a living, breathing version of your static code.
π§© Key Characteristics of a Process
| Feature | Explanation |
|---|---|
| π§ Code (Text) Segment | The actual program code (machine instructions) |
| π¦ Data Segment | Global and static variables |
| π Heap | Dynamically allocated memory (via malloc, new) |
| π Stack | Function calls, local variables, return addresses |
| πͺͺ PID (Process ID) | Unique identifier assigned by OS |
| β³ State | Running, Ready, Blocked, Terminated |
| β±οΈ Program Counter (PC) | Address of next instruction |
| π CPU Registers | Store temporary data |
| π Isolation | Processes donβt share memory by default |
π§± Process vs Program
| Program | Process |
|---|---|
| Passive entity (just code) | Active entity (in execution) |
| Stored on disk (.exe, .class) | Loaded in RAM |
| Doesnβt do anything | Consumes CPU, memory, I/O |
| Can have multiple instances | Each instance is a separate process |
π Process Lifecycle States
ββββββββββ
β New β β Created by OS
ββββ¬ββββββ
β
ββββββββββ
β Ready β β Waiting to be scheduled
ββββ¬ββββββ
β
ββββββββββ
βRunning β β On the CPU now
ββββ¬ββββββ
βββββββ΄ββββββ
β β
ββββββββββββ βββββββββββββ
β Blocked β β Terminatedβ
ββββββββββββ βββββββββββββ
-
New: Process is created (e.g.,
fork()in Linux) -
Ready: Waiting in the queue for CPU
-
Running: Actively executing instructions
-
Blocked: Waiting for I/O (e.g., file read)
-
Terminated: Finished or killed
π οΈ Process Control Block (PCB)
The OS uses a PCB to keep track of each process.
| Contains |
|---|
| PID |
| CPU registers |
| Program counter |
| Scheduling info |
| Memory limits |
| I/O info |
| Process state |
π§ͺ Real-World Example
$ firefox-
The OS:
-
Loads the Firefox binary
-
Allocates memory for code, heap, stack
-
Assigns PID (e.g., 10234)
-
Tracks it in the scheduler
-
Now Firefox is a process.
π§ Interview-Ready Definition:
A process is a program in execution. It includes the program code, current activity (registers, program counter), memory segments (heap, stack, data), and resources assigned by the OS. Processes are isolated from each other and managed via Process Control Blocks (PCBs).