थोडक्यात सारांश
This topic goes deeper into how an operating system manages processes. It covers the process concept and process states, the process control block (PCB), context switching, threads, inter-process communication, the critical-section problem and its solutions (semaphores), classic synchronisation problems, and deadlocks with their conditions, prevention, avoidance (Banker's algorithm) and detection.
महत्त्वाचे मुद्दे
- A process is a program in execution. A program is passive (a file); a process is active (has a program counter, registers, stack, data).
- Process states: new → ready → running → waiting (blocked) → ready … → terminated. The scheduler moves a process from ready to running; an I/O request moves it to waiting.
- PCB stores everything about a process: process ID, state, program counter, CPU registers, scheduling information, memory information, open files, I/O status.
- Context switch: saving the state of the running process in its PCB and loading the state of the next one. It is pure overhead.
- Thread: the smallest unit of CPU execution inside a process. Threads of one process share code, data and files but have their own stack and registers. Multithreading improves responsiveness and resource sharing.
- Inter-process communication (IPC): shared memory and message passing.
- Race condition: the result depends on the order in which processes access shared data.
- Critical section: the part of code that accesses shared data. A solution must satisfy mutual exclusion, progress and bounded waiting.
- Semaphore (Dijkstra): an integer variable accessed only by atomic wait() (P) and signal() (V) operations. Binary semaphore (mutex) and counting semaphore.
- Classic problems: producer–consumer (bounded buffer), readers–writers, dining philosophers.
- Deadlock: a set of processes each waiting for a resource held by another, so none can proceed.
- Four necessary conditions (Coffman): mutual exclusion, hold and wait, no preemption, circular wait. All four must hold together.
- Handling deadlock: prevention (break one condition), avoidance (Banker's algorithm keeps the system in a safe state), detection and recovery (kill a process or preempt resources), or ignore it (ostrich approach).
महत्त्वाच्या संज्ञा
- Process (प्रक्रिया) – program in execution.
- PCB – data structure the OS keeps for each process.
- Mutual exclusion – only one process in the critical section at a time.
- Safe state – state in which the OS can give every process its maximum need in some order.
- Starvation – a process waits indefinitely while others are served.
सूत्रे
- Banker's algorithm: Need = Max − Allocation
- A process Pᵢ can run if Needᵢ ≤ Available; after it finishes, Available = Available + Allocationᵢ
- Semaphore operations:
५ महत्त्वाचे प्रश्न
Q1. Differentiate between a process and a thread.
A process is an independent program in execution with its own memory space; creating and switching processes is costly. A thread is a lightweight unit inside a process that shares code, data and files with other threads of the same process; switching is cheaper.
Q2. State the four necessary conditions for deadlock.
Mutual exclusion (a resource used by one process at a time), hold and wait (a process holds some resources while waiting for others), no preemption (resources cannot be taken away forcibly), and circular wait (a closed chain of processes, each waiting for the next).
Q3. Five processes P0–P4, one resource type, Available = 3. Allocation = (1, 2, 2, 1, 0), Max = (4, 3, 7, 2, 4). Is the system safe?
- Need = Max − Allocation = (3, 1, 5, 1, 4)
- Available 3 → P1 (need 1) runs; Available = 3 + 2 = 5
- P3 (need 1) runs; Available = 5 + 1 = 6
- P0 (need 3) runs; Available = 6 + 1 = 7
- P4 (need 4) runs; Available = 7; P2 (need 5) runs; Available = 9
- Safe sequence: P1, P3, P0, P4, P2 → system is safe.
Q4. Draw and explain the process state diagram.
- New: process is being created.
- Ready: waiting in the ready queue for the CPU.
- Running: instructions are being executed.
- Waiting: waiting for an I/O or event.
- Terminated: finished.
- Transitions: admitted (new → ready), dispatch (ready → running), interrupt/time-out (running → ready), I/O wait (running → waiting), I/O done (waiting → ready), exit (running → terminated).
Q5. Explain the producer–consumer problem and its solution using semaphores.
- Producer puts items in a bounded buffer of size n; consumer removes them.
- Semaphores: mutex = 1 (mutual exclusion), empty = n (free slots), full = 0 (filled slots).
- Producer: wait(empty); wait(mutex); add item; signal(mutex); signal(full).
- Consumer: wait(full); wait(mutex); remove item; signal(mutex); signal(empty).
- This prevents overflow, underflow and race conditions.
परीक्षेसाठी टीप
Banker's algorithm and process-state diagram questions are almost certain. In Banker's problems, write the Need table first and show Available after each process.