Process Management in OS — PCB, States, Context Switch | Tutorials Logic
Process vs Program
| Feature | Program | Process |
|---|---|---|
| Definition | A passive entity - a set of instructions stored on disk | An active entity - a program in execution |
| Storage | Stored on disk (secondary memory) | Loaded in RAM (primary memory) |
| Lifetime | Permanent (until deleted) | Temporary (exists while running) |
| Resources | No resources allocated | Has CPU, memory, I/O resources |
| Multiple instances | One copy on disk | Multiple processes from one program |
Process Control Block (PCB)
The OS maintains a data structure called the Process Control Block (PCB) for each process. It contains all information about a process:
- Process ID (PID): Unique identifier for the process
- Process State: Current state (new, ready, running, waiting, terminated)
- Program Counter: Address of the next instruction to execute
- CPU Registers: Contents of all CPU registers (saved during context switch)
- Memory Management Info: Base/limit registers, page tables
- I/O Status: List of I/O devices allocated, open files
- Accounting Info: CPU time used, time limits, process priority
- Parent PID: PID of the parent process
Process States
A process transitions through the following states during its lifetime:
- New: Process is being created. PCB is being initialized.
- Ready: Process is loaded in memory and waiting for CPU time. In the ready queue.
- Running: Process is currently executing on the CPU. Only one process per CPU core can be in this state.
- Waiting (Blocked): Process is waiting for an event (I/O completion, signal, resource). Cannot use CPU.
- Terminated: Process has finished execution. Resources are being released.
State Transitions:
- New -> Ready: Process admitted to ready queue
- Ready -> Running: Scheduler dispatches process to CPU
- Running -> Ready: Preempted (time quantum expired or higher priority process arrives)
- Running -> Waiting: Process requests I/O or waits for event
- Waiting -> Ready: I/O completes or event occurs
- Running -> Terminated: Process completes or is killed
Context Switching
When the OS switches the CPU from one process to another, it performs a context switch:
- Save the state of the current process (registers, program counter) into its PCB
- Load the state of the next process from its PCB
- Resume execution of the next process
Context switching is pure overhead - no useful work is done during the switch. Modern CPUs have hardware support to speed up context switches.
Process Creation and Termination
Process Creation (fork()): In Unix/Linux, a new process is created using the fork() system call. The parent process creates a child process that is an exact copy of the parent. The child then typically calls exec() to replace its memory with a new program.
fork()returns 0 to the child, and the child's PID to the parent- Parent and child run concurrently after fork()
- Parent can wait for child using
wait()
Zombie Process: A process that has completed execution but its entry still exists in the process table because the parent hasn't called wait() yet.
Orphan Process: A process whose parent has terminated. The orphan is adopted by the init process (PID 1).
Level Up Your Operating system Skills
Master Operating system with these hand-picked resources