πŸ“˜ OS: Process Management (Lec 9, 10, 11)

βœ… What is a Process?

βœ… How does OS create a process?

Steps:

  1. Load program & static data into memory.
  2. Allocate runtime stack (for function calls/variables).
  3. Allocate heap (for dynamic memory).
  4. Setup I/O tasks (open files, etc.).
  5. Give control to the main() function.

βœ… Process Attributes & PCB (Process Control Block)

Processes Architecture

Processes Architecture

OS tracks all processes using a Process Table.

Each process has an entry called PCB, storing:

Process Control Block
πŸ‘‰ Use-case: During context switch, PCB is updated to save/restore process state.

βœ… Process States (Life Cycle)

These are stages of a process:

Process States
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:

βœ… Process Queues

  1. Job Queue: NEW processes in disk; managed by LTS (Long Term Scheduler).
  2. Ready Queue: READY processes in RAM; managed by STS (Short Term Scheduler).
  3. 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)

Swapping
πŸ“Œ Example: If RAM is full, MTS might suspend a low-priority process and bring it back later.

βœ… Context Switching

Context Switching

When CPU switches from one process to another:

Overhead: No actual work is done during switch.

πŸ“Œ Analogy: Like saving your game before someone else plays.

βœ… Process Types

1. Orphan Process

2. 🧟 Zombie Process

🎯 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.