Processes and threads are the active units of execution in an operating system. PSC questions often test process states, PCB, context switch, thread benefits, race condition, semaphore/monitor, CPU scheduling algorithms and deadlock conditions.
Engineering Definitions
Process
Standard definition: A program in execution with its own address space and execution context.
Exam meaning: Execution भइरहेको program जसको memory/context हुन्छ।
Thread
Standard definition: A lightweight execution unit within a process sharing process resources.
Exam meaning: Process भित्रको lightweight execution path।
Critical section
Standard definition: A code region that accesses shared resource and must not be executed by multiple threads/processes simultaneously.
Exam meaning: Shared resource access गर्ने protected code part।
Deadlock
Standard definition: A condition where processes wait forever for resources held by each other.
Exam meaning: Processes एक-अर्काले hold गरेको resource कुरेर permanently blocked हुनु।
Concept Teaching
An OS must multiplex CPU safely among many processes and threads. Scheduling improves responsiveness and throughput. Synchronization prevents race conditions. Deadlock handling ensures resource allocation does not freeze the system.
Process and PCB
PCB stores everything OS needs to stop and resume a process.
- Process states include new, ready, running, waiting/blocked and terminated.
- PCB contains process ID, state, program counter, registers, scheduling info, memory info and open files.
- Context switch saves current process context and loads another.
- Context switching is overhead but enables multitasking.
Threads
Threads share process resources but have separate execution state.
| Aspect | Process | Thread |
|---|---|---|
| Address space | Separate | Shared within process |
| Creation cost | Higher | Lower |
| Communication | IPC required | Shared memory easier |
| Failure isolation | Better | One thread can affect process |
| Context switch | Heavier | Lighter |
Synchronization
Synchronization protects shared data.
- Race condition occurs when result depends on timing/interleaving.
- Mutual exclusion allows one execution in critical section.
- Semaphore is integer synchronization primitive with wait/signal.
- Mutex is lock for mutual exclusion.
- Monitor combines shared data with procedures and condition variables.
- Classical problems include producer-consumer, readers-writers and dining philosophers.
CPU Scheduling
Scheduling chooses which ready process gets CPU.
| Algorithm | Idea | Tradeoff |
|---|---|---|
| FCFS | First come first served | Simple but convoy effect |
| SJF | Shortest job first | Optimal average waiting if known, starvation risk |
| Priority | Highest priority first | Starvation possible |
| Round Robin | Time quantum rotation | Good response, quantum choice matters |
| Multilevel queue | Separate queues by class | Policy complexity |
Deadlock
Deadlock requires four Coffman conditions.
- Mutual exclusion.
- Hold and wait.
- No preemption.
- Circular wait.
- Prevention breaks one condition.
- Avoidance uses safe state idea such as Banker algorithm.
- Detection finds deadlock and recovery kills/rolls back/preempts.
Engineering Mechanism
- Process enters ready queue.
- Scheduler selects process/thread for CPU.
- Context switch loads selected context.
- Threads execute and may enter critical sections.
- Synchronization primitives control shared resource access.
- Resource allocation can cause deadlock if conditions hold.
- OS prevents, avoids, detects or recovers from deadlock.
Diagrams / Models To Draw
- Draw process state transition diagram.
- Draw PCB fields.
- Draw ready queue and CPU scheduler.
- Draw critical section with semaphore wait/signal.
- Draw resource allocation graph deadlock cycle.
Formulas, Algorithms and Rules
- Turnaround time = completion time – arrival time.
- Waiting time = turnaround time – burst time.
- Response time = first CPU time – arrival time.
- Deadlock conditions = mutual exclusion + hold/wait + no preemption + circular wait.
| Concept | Purpose | Exam trap |
|---|---|---|
| Process | Execution unit with resources | Not same as program file |
| Thread | Lightweight execution | Shares process memory |
| Semaphore | Synchronization counter | Wrong wait/signal order causes bugs |
| Scheduler | CPU allocation | Algorithm tradeoffs |
| Starvation | Indefinite waiting | Aging can help |
| Deadlock | Permanent resource wait | Needs four conditions |
Exam Point
- Process is program in execution.
- Thread shares address space with sibling threads.
- Race condition requires shared data and timing dependence.
- Round robin time quantum is important.
- Deadlock conditions must all hold.
- Starvation and deadlock are different.
Worked Example
In producer-consumer, producer must not insert into full buffer and consumer must not remove from empty buffer. Semaphores empty, full and mutex coordinate capacity count and mutual exclusion around buffer access.
Subjective Answer Pattern
- Define process/thread.
- Explain process states and PCB.
- Discuss synchronization and race condition.
- Compare scheduling algorithms.
- Explain deadlock conditions and handling methods.
- Add formulas for scheduling if asked.
Common Engineering Mistakes
- Confusing program and process.
- Saying threads never need synchronization because they share memory.
- Ignoring context-switch overhead.
- Mixing starvation and deadlock.
- Forgetting one Coffman condition.
MCQ Revision
- What is PCB?
- Which algorithm uses time quantum?
- What is race condition?
- How many Coffman conditions?
- What is aging used for?
- Semaphore wait/signal are used for what?
Final Summary
- OS manages processes and threads for multitasking.
- Scheduling balances response, throughput and fairness.
- Synchronization prevents race conditions.
- Deadlock occurs when resource waits form an unbreakable cycle.
- Scheduling metrics and deadlock conditions are high-yield.