Operating Systems

Module 1

1. Process Management

Process states, PCB, CPU scheduling algorithms

Theory

Process Management

Core OS Concept - How the OS manages running programs


What You'll Learn

  1. Understand process states and transitions
  2. Learn CPU scheduling algorithms
  3. Calculate waiting time and turnaround time
  4. Compare scheduling algorithms

1. Process Basics

What is a Process?

A process is a program in execution. It includes:

  • Program code (text section)
  • Current activity (program counter, registers)
  • Stack (temporary data)
  • Data section (global variables)
  • Heap (dynamically allocated memory)

Process vs Program

ProgramProcess
Passive entity (file on disk)Active entity (running)
StaticDynamic
One programMany processes of same program

2. Process States

text
                    ┌─────────────────┐
    ┌──────────────►│      Ready      │◄──────┐
    │               └────────┬────────┘       │
    │                        │                │
    │              (Scheduler│Dispatch)       │
    │                        ▼                │
┌───┴────┐           ┌───────────────┐   ┌────┴─────┐
│  New   │           │   Running     │   │ Waiting  │
└────────┘           └───────┬───────┘   └──────────┘
                             │                ▲
              (I/O or event) │                │
                             └────────────────┘
                                    │
                                    ▼
                            ┌───────────────┐
                            │  Terminated   │
                            └───────────────┘

Process States

StateDescription
NewProcess is being created
ReadyWaiting to be assigned to CPU
RunningInstructions being executed
WaitingWaiting for I/O or event
TerminatedProcess finished execution

3. Process Control Block (PCB)

Each process has a PCB containing:

text
┌─────────────────────────────┐
│   Process Control Block     │
├─────────────────────────────┤
│ Process ID (PID)            │
│ Process State               │
│ Program Counter             │
│ CPU Registers               │
│ CPU Scheduling Info         │
│ Memory Management Info      │
│ I/O Status Info             │
│ Accounting Info             │
└─────────────────────────────┘

4. CPU Scheduling Algorithms

Key Terms

TermFormulaDescription
Arrival Time (AT)GivenWhen process enters ready queue
Burst Time (BT)GivenCPU time needed
Completion Time (CT)CalculatedWhen process finishes
Turnaround Time (TAT)CT - ATTotal time in system
Waiting Time (WT)TAT - BTTime spent waiting
Response TimeFirst run - ATTime to first response

1. FCFS (First Come First Serve)

text
Non-preemptive: Process runs until completion

Example:
Process | AT | BT
P1      | 0  | 4
P2      | 1  | 3
P3      | 2  | 1

Gantt Chart:
|   P1   |   P2   | P3 |
0        4        7    8

Results:
P1: CT=4, TAT=4-0=4, WT=4-4=0
P2: CT=7, TAT=7-1=6, WT=6-3=3
P3: CT=8, TAT=8-2=6, WT=6-1=5

Avg WT = (0+3+5)/3 = 2.67

Convoy Effect: Short processes wait behind long ones

2. SJF (Shortest Job First)

text
Select process with shortest burst time

Non-preemptive SJF:
Process | AT | BT
P1      | 0  | 7
P2      | 2  | 4
P3      | 4  | 1
P4      | 5  | 4

Gantt Chart:
|  P1  |P3|  P2  |  P4  |
0      7  8     12     16

At t=7: P2,P3,P4 are ready. P3 has shortest BT(1)

SRTF (Preemptive SJF): Can preempt if shorter job arrives

3. Round Robin (RR)

text
Preemptive with time quantum (q)
Each process gets q time units, then goes to back of queue

Process | AT | BT
P1      | 0  | 5
P2      | 1  | 3
P3      | 2  | 1

Time Quantum = 2

Gantt Chart:
|P1|P2|P3|P1|P2|P1|
0  2  4  5  7  8  9

P1: 2 units → P2: 2 units → P3: 1 unit (done)
→ P1: 2 units → P2: 1 unit (done) → P1: 1 unit (done)

Key: Smaller quantum = more context switches = more overhead

4. Priority Scheduling

text
Lower number = Higher priority (usually)

Process | AT | BT | Priority
P1      | 0  | 4  | 2
P2      | 0  | 3  | 1 (highest)
P3      | 0  | 2  | 3

Order: P2 → P1 → P3

Problem: Starvation (low priority waits forever)
Solution: Aging (increase priority over time)

5. Scheduling Algorithm Comparison

AlgorithmPreemptiveStarvationOptimal
FCFSNoNoNo
SJFNoYesYes (non-preemptive)
SRTFYesYesYes (preemptive)
Round RobinYesNoFair
PriorityBothYesDepends

Key Takeaways

ConceptKey Point
ProcessProgram in execution
PCBContains all process info
FCFSSimple but convoy effect
SJFOptimal but starvation
Round RobinFair, time quantum based
TATCompletion Time - Arrival Time
WTTurnaround Time - Burst Time
Practice

Test your understanding

📝

Practice Quiz

10 questions · 90s per question

Each question has a 90-second time limit. Unanswered questions will be auto-submitted when time runs out.