π OS: Process Management (Lec 9, 10, 11)
β
What is a Process?
- A process is a program in execution. OS handles
processes.
β
How does OS create a process?
Steps:
- Load program & static data into memory.
- Allocate runtime stack (for function calls/variables).
- Allocate heap (for dynamic memory).
- Setup I/O tasks (open files, etc.).
- Give control to the
main()
function.
β
Process Attributes & PCB (Process Control Block)
Processes Architecture
OS tracks all processes using a Process Table.
Each process has an entry called PCB, storing:
π Use-case: During context switch, PCB is updated to
save/restore process state.
β
Process States (Life Cycle)
These are stages of a process:
State |
Description |
NEW |
Process is being created |
READY |
Loaded into RAM, waiting for CPU |
RUNNING |
Actively executing on CPU |
WAITING |
Waiting for I/O |
TERMINATED |
Finished execution |
π Some intermediate actions:
- Preemption (from Running to Ready)
- I/O request (from Running to Waiting)
β
Process Queues
- Job Queue: NEW processes in disk; managed by LTS (Long
Term Scheduler).
- Ready Queue: READY processes in RAM; managed by STS (Short
Term Scheduler).
- Waiting Queue: WAITING processes; doing I/O.
β
Schedulers in Operating System
Scheduler |
Role |
Controls |
LTS (Long-Term Scheduler) |
Loads new processes from disk to RAM |
Controls Degree of Multiprogramming |
STS (Short-Term Scheduler) |
Selects processes from Ready Queue to run on CPU |
Controls CPU Assignment |
MTS (Medium-Term Scheduler) |
Temporarily removes (swaps out) processes from RAM |
Controls Memory Usage |
Why Schedulers are Needed:
- To manage which processes get CPU, memory, and other resources.
- To ensure smooth multitasking by the OS.
- To improve performance, responsiveness, and efficiency.
Why Different Schedulers:
- LTS manages how many processes are in RAM at once (prevents overload).
- STS decides which process runs next on the CPU (like a traffic cop).
- MTS pauses processes when RAM is full, resuming them later (saves memory).
β
Swapping (By MTS)
- Temporarily swap out a process from RAM to disk to free memory.
- Later, swap it back in when needed.
π Example: If RAM is full, MTS might suspend a low-priority process
and bring it back later.
β
Context Switching
When CPU switches from one process to another:
- Save current process state (in PCB)
- Load new process state (from PCB)
Overhead: No actual work is done during switch.
π Analogy: Like saving your game before someone else plays.
β
Process Types
1. Orphan Process
- Parent died, child still running.
- Adopted by init process (PID 1).
2. π§ Zombie Process
- A Zombie process is a child process that has finished executing (
exit()
), but still has
an entry in the process table.
- This happens because the **parent process has not yet read the childβs exit status** using
wait()
.
- It doesnβt use memory or CPU, but it keeps a slot in the process table (wastes resources).
π― Real-life Analogy:
A child finishes their exam (child process finishes), but the teacher (parent) hasnβt checked their
paper yet. Until the teacher checks it, the childβs name stays on the pending list.
π Code Example:
int pid = fork();
if (pid == 0) {
// Child process
exit(0);
} else {
sleep(10); // Parent delays
wait(); // Parent finally reads child status
}
β
Until the parent calls wait()
, the child becomes a zombie.
β
If the parent never calls wait()
, the zombie stays until the parent exits or is killed.
β
Preemptive vs Non-Preemptive Scheduling
Type |
Description |
Non-Preemptive |
CPU is not taken forcefully; process runs till done or waits |
Preemptive |
CPU can be taken anytime for a higher priority process |
π Example: You're using PC β Urgent system update interrupts β You're paused
(Preemptive)
π Summary Table
Concept |
Keyword/Idea |
Process |
Program in execution |
PCB |
Stores process info |
Scheduler Types |
LTS, STS, MTS |
Queues |
Job, Ready, Waiting |
Context Switch |
Save/Restore state |
Swapping |
Move in/out of RAM |
Orphan Process |
Parent dead, child alive |
Zombie Process |
Child dead, waiting to be reaped |
Preemptive |
Forcefully switch |
Non-preemptive |
Wait till end |
Multiprogramming |
Multiple in memory |
Multitasking |
Time-sharing |
π§ Quick Interview Prep Notes (One-Liner)
- Process vs Program β Program is static, process is dynamic.
- PCB β Structure storing process details.
- Ready Queue β Holds all ready-to-run processes.
- Zombie β Dead child process not yet reaped.
- Orphan β Child whose parent exited.
- Swapping β Temporarily moves process out of RAM.
- Context Switch β Save and restore CPU state.
- LTS vs STS vs MTS β Controls when to bring, assign, and swap.
- Preemptive β Can interrupt running process.
- Multiprogramming β Multiple loaded.
- Multitasking β Time sharing of CPU.