Process management is function of an operating system, responsible for creating, scheduling, and terminating processes. It ensures efficient CPU utilization through scheduling algorithms like FCFS, SJF, Priority, and Round Robin. Key concepts include process states, context switching, inter-process communication (IPC), and synchronization.
1. How does the OS handle priority inversion, and why can it be a critical problem in real-time systems?
Priority inversion occurs when a high-priority process is waiting for a resource held by a lower-priority process, but a medium-priority process preempts the lower-priority one, delaying the high-priority task indirectly. This is critical in real-time systems where deadlines are strict.
Example:
- Process H (High priority) waits for a lock held by Process L (Low priority).
- Process M (Medium priority) runs and keeps preempting L, preventing H from progressing.
Solution techniques:
- Priority Inheritance: Temporarily boost L’s priority to H’s priority until it releases the resource.
- Priority Ceiling Protocol: Assign resources a priority equal to the highest priority of processes that might lock them, preventing unexpected blocking.
Without these, deadline misses or system stalls can occur in mission-critical tasks.
2. Explain how context switching works at the kernel level and why it’s considered an expensive operation.
Context switching is the act of storing the CPU state of a running process and loading the CPU state of another process. At the kernel level, it involves:
- Saving registers, program counter (PC), and stack pointer (SP) of the current process into its PCB.
- Updating scheduling queues.
- Loading the PCB of the next process and restoring its registers, PC, and SP.
Why expensive?
- Requires multiple memory accesses for PCB storage/restoration.
- Flushes CPU pipelines and TLBs, affecting performance.
- Induces cache misses as the new process’s memory footprint may differ.
- Minimizing unnecessary context switches is critical for high-throughput systems.
3. A system implements both preemptive and non-preemptive scheduling. Give a scenario where non-preemptive scheduling can outperform preemptive scheduling.
Non-preemptive scheduling can outperform in batch processing where context-switch overhead dominates and task durations are predictable.
Example:
- In an environment with long CPU-bound jobs and minimal I/O, preempting for fairness causes more context switches than progress.
- Non-preemptive policies like SJF can minimize turnaround time and maximize throughput because jobs run to completion without frequent interruptions.
- Real-time analogy: In embedded firmware updates, uninterrupted execution prevents corruption due to mid-task preemption.
4. How does the OS avoid starvation in priority scheduling algorithms?
Starvation occurs when low-priority processes never get CPU time because high-priority processes keep arriving.
Avoidance strategies:
- Aging: Gradually increase a process’s priority the longer it waits, ensuring it eventually reaches the top priority.
- Priority Queue Rotation: After a certain time quantum, temporarily drop the priority of a high-priority process to allow others execution.
- Hybrid Scheduling: Combine priority with round-robin to give all processes some CPU share. These techniques balance responsiveness for urgent tasks with fairness for long-waiting jobs.
5. Compare cooperative multitasking and preemptive multitasking in terms of process management complexity and stability.
Cooperative multitasking: Processes voluntarily yield control; OS does not forcibly take CPU.
- Pros: Simpler kernel, fewer context switches, better cache locality.
- Cons: A poorly designed process can monopolize CPU, causing unresponsiveness.
Preemptive multitasking: OS scheduler interrupts processes to enforce time sharing.
- Pros: Better responsiveness, fair CPU distribution.
- Cons: More complex kernel, higher overhead, possible race conditions without synchronization.
In modern systems, preemptive multitasking is preferred despite the complexity, as it guarantees system responsiveness.
6. In multi-core processors, how does the OS decide whether to migrate a process to another core, and what are the trade-offs?
OS uses load balancing algorithms in the scheduler to decide migration:
When? If one core is idle or underloaded while others are overloaded.
Trade-offs:
- Pros: Improves CPU utilization and throughput.
- Cons: Process migration can cause cache invalidation, TLB flushes, and increased memory access latency if the process’s data is not in the new core’s cache (NUMA effects).
Schedulers like Linux’s CFS use heuristics to minimize unnecessary migrations while balancing load.
7. What’s the difference between a process state transition “Blocked → Ready” and “Running → Ready” in terms of scheduling?
Blocked -> Ready:
- Triggered by the completion of an event (e.g., I/O finishes).
- The process re-enters the ready queue and competes for CPU time.
Running -> Ready:
- Triggered by preemption when a higher-priority process arrives or when a time quantum expires.
- OS decides whether to resume the same process later or schedule another.
This distinction matters because “Blocked -> Ready” transitions are event-driven, while “Running -> Ready” transitions are scheduler-driven.
8. Why is process creation more expensive in Unix-like systems than thread creation?
In Unix-like OS:
- Process creation (
fork) duplicates the entire process address space (though copy-on-write optimizes this), sets up a new PCB, file descriptors, and kernel structures. - Thread creation (
pthread_create) shares address space, file descriptors, and code segment, requiring only a thread control block (TCB) setup.
Thus, processes require more kernel work (memory mapping, resource allocation) compared to threads, making thread creation significantly faster.
9. How does the OS ensure atomicity when multiple processes request the same I/O device?
OS uses mutual exclusion mechanisms at the device driver level:
- Spinlocks or semaphores prevent multiple processes from issuing conflicting commands.
- I/O scheduling orders requests to prevent deadlock (e.g., elevator algorithm for disks).
- Blocking queues hold requests until the device is free.
Atomicity ensures no partial I/O operations occur that could corrupt data or produce inconsistent results.
10. Explain the “thundering herd problem” in process synchronization and how OS mitigates it.
Occurs when multiple processes/threads are waiting for an event (e.g., socket readiness) and all are awakened when the event happens, but only one can proceed, causing excessive context switches and CPU waste.
Mitigation:
- Eventfd/epoll in Linux awakens only a subset or single waiter.
- Accept mutex in web servers ensures only one process handles incoming connections.
- Kernel-level load balancing to distribute work evenly.
11. Explain the concept of process starvation in OS. How can aging be used to prevent it?
Starvation occurs when a process waits indefinitely for CPU time because higher-priority processes keep getting scheduled. This is common in priority scheduling where lower-priority processes may never execute. Aging is a prevention technique where a process’s priority increases gradually the longer it waits.
Example: A low-priority process might start with priority 1 but gain +1 every 5 seconds until it competes fairly with higher-priority processes. This ensures fairness without drastically affecting overall performance.
Aging is especially important in systems where certain jobs must not be delayed beyond a deadline.
12. Differentiate between preemptive and non-preemptive scheduling with real-time implications.
- Preemptive Scheduling: The CPU can be taken away from a process if a higher-priority process arrives (e.g., Round Robin, Priority with preemption). It’s essential in real-time systems where immediate response is needed.
- Non-preemptive Scheduling: Once a process starts execution, it runs until completion or voluntarily gives up the CPU (e.g., FCFS, SJF without preemption).
In hard real-time systems, preemption ensures deadlines are met, but it introduces context-switch overhead. Non-preemptive systems are simpler but unsuitable for urgent tasks.
13. How does the OS handle orphan and zombie processes?
- Zombie Process: Process has completed execution but still has an entry in the process table because its parent hasn’t read its exit status (via wait() in UNIX). It consumes minimal resources but clutters process tables.
- Orphan Process: Parent terminates before the child. In UNIX, such processes are adopted by init (PID 1), which calls wait() to clean them up.
The OS uses process reaping to remove zombies and process adoption mechanisms for orphans, ensuring no dangling processes persist indefinitely.
14. Explain how context switching affects CPU performance.
Context switching saves the state (registers, PC, stack pointer) of the current process and loads the state of the next scheduled process.
- Overhead: It uses CPU cycles without doing productive work.
- Impact: High context-switch frequency reduces effective CPU throughput.
Optimizations include reducing unnecessary preemptions and grouping similar tasks to minimize cache misses. In real-time systems, context switch time is a critical factor for meeting deadlines.
15. How do inter-process communication (IPC) methods differ for message-passing vs shared-memory systems?
- Message Passing: Processes communicate via OS-managed channels (e.g., pipes, sockets, message queues). Easier to implement across distributed systems but slower due to kernel intervention.
- Shared Memory: Processes share a memory segment. Faster but requires synchronization (mutexes, semaphores) to avoid race conditions.
Hybrid systems combine both, shared memory for bulk data and message passing for control signals, to balance speed and safety.
16. What is the difference between process suspension and termination?
- Suspension: Temporarily halts execution but retains the process in memory or secondary storage. The process can be resumed later (e.g., OS suspending a background task to free CPU).
- Termination: Process completes or is killed; its resources are freed, and it cannot be resumed
Suspension is reversible, termination is final. OS must manage suspended processes carefully to avoid deadlocks or priority inversions.
17. How does the OS implement process synchronization to avoid race conditions?
The OS uses mechanisms like mutex locks, semaphores, monitors, and spinlocks to control access to shared resources.
Example: In the producer-consumer problem, semaphores manage buffer access, one semaphore tracks empty slots, another tracks filled slots. The OS ensures atomicity of these operations, preventing inconsistent states like multiple consumers accessing the same item simultaneously.
18. Describe the concept of process affinity and its types. Why is it important?
Process affinity (CPU pinning) binds a process to one or more specific CPUs to optimize cache performance.
- Soft Affinity: OS prefers the same CPU but may schedule on another if needed.
- Hard Affinity: Process is strictly bound to specified CPUs.
Affinity reduces cache misses and improves performance for CPU-intensive tasks but can reduce load balancing efficiency.
19. How does the OS handle deadlock in process management?
The OS can:
- Prevent: Remove at least one Coffman condition (e.g., by not allowing hold-and-wait).
- Avoid: Use algorithms like Banker’s Algorithm to ensure safe state.
- Detect & Recover: Periodically check for cycles in the resource allocation graph and preempt resources or kill processes to resolve
Choice depends on system type, real-time OS often prefer prevention or avoidance over detection.
20. Explain the difference between process and thread scheduling.
- Process Scheduling: Chooses which process from the ready queue gets CPU time. Involves heavy context switches (saving/restoring full PCB).
- Thread Scheduling: Chooses which thread within a process gets CPU time. Lightweight as threads share memory and resources
Multithreaded scheduling improves concurrency but may cause priority inversion if threads of different priorities share locks.