Synchronization & Concurrency Interview Questions Operating System (original) (raw)

Last Updated : 1 Sep, 2025

Synchronization and concurrency are critical for managing processes that execute simultaneously in a multitasking environment. They ensure data consistency, prevent race conditions, and coordinate access to shared resources through mechanisms like semaphores, mutexes, monitors, and condition variables. Effective concurrency control improves performance while maintaining correctness, making it a key focus area for advanced OS interviews.

1. Explain the difference between deadlock, livelock, and starvation with real-world examples.

**Deadlock:

**Livelock:

**Starvation:

**Key Difference: Deadlock is a complete halt, livelock is active but unproductive, starvation is indefinite waiting due to unfair scheduling.

2. Why does priority inversion occur, and how can protocols like Priority Inheritance prevent it?

**Priority Inversion:

**Solution - Priority Inheritance Protocol (PIP):

**Example: NASA’s _Mars Pathfinder (1997) encountered system resets due to priority inversion in its VxWorks OS scheduler; enabling priority inheritance resolved the issue.

3. How does the Bakery Algorithm ensure fairness in process synchronization along with the drawbacks?

Each process picks a ticket number before entering the critical section (like standing in line at a bakery). The lowest ticket number gets entry first. If numbers tie, the process ID is used to break the tie.

**Fairness:

**Drawbacks:

4. Explain how a monitor differs from a semaphore in synchronization.

**Semaphore:

**Monitor:

**Example: Java’s synchronized methods/blocks implement monitor semantics.

5. What is a Spurious Wakeup in condition variables, and how should it be handled?

**Spurious Wakeup:

**Handling:

pthread_mutex_lock(&lock);
while (buffer_empty) {
pthread_cond_wait(&not_empty, &lock); // may wake up spuriously
}
consume_item();
pthread_mutex_unlock(&lock);

**Key Point for Interview:

6. In the Reader-Writer problem, why is writer starvation common, and how can it be prevented?

**Issue - Writer Starvation:

**Prevention Methods:

**Real-world Example: In databases, write locks are prioritized over read locks to ensure updates aren’t blocked indefinitely, preventing stale data issues.

7. How do condition variables in monitors help solve producer-consumer problems?

**Problem:

**Condition Variables in Monitors:

**Application in Producer–Consumer:

**Benefit:

8. Describe a situation where using a binary semaphore would cause deadlock but a monitor would not.

**Binary Semaphore Deadlock Case: Consider two processes P1 and P2 trying to access two shared resources R1 and R2 protected by binary semaphores S1 and S2:

**Reason: With semaphores, the programmer must explicitly manage acquire/release. If resources are not acquired in a consistent order, deadlock is possible.

**Why Monitors Avoid This: A monitor wraps the shared resources and their synchronization inside a structured abstraction:

Thus, in the same scenario, only one thread at a time would access the critical section guarded by the monitor, avoiding the deadlock situation.

9. How can spinlocks be more efficient than mutexes, and when are they harmful?

**Why Spinlocks Can Be More Efficient:

**When They’re Harmful:

**Rule of Thumb: Use spinlocks only for short, predictable waits (e.g., kernel interrupt handlers); otherwise, prefer mutexes.

10. Explain the role of the “Happens-Before” relationship in concurrency control.

A logical relationship used to reason about the order of visibility of operations in concurrent programs.

**Uses:

**Example (Java Memory Model):

**Key Point: Happens-before defines the rules of causality in concurrency, ensuring correctness across threads.

11. How does priority inversion occur in concurrent systems, and what are its real-world consequences? Explain solutions.

Priority inversion Occurs when a high-priority process is waiting on a lock/resource held by a low-priority process, but a medium-priority process keeps preempting the low-priority one. This prevents progress of the high-priority task

**Example: NASA’s Mars Pathfinder mission suffered resets due to unhandled priority inversion in its task scheduler.

**Solutions:

**Note: Without mitigation, system responsiveness, predictability, and correctness can degrade severely in real-time systems. Priority inversion is not just theoretical - real-time and embedded systems must handle it explicitly.

12. Differentiate between Busy Waiting and Blocking in synchronization. Which is better in high-concurrency systems?

**Busy Waiting (Spinlocks):

**Blocking:

**Trade-off:

**Rule of Thumb: _Spinlocks for nanosecond–microsecond waits, blocking for anything longer.

13. How do condition variables differ from semaphores in process synchronization? Give an example where semaphores fail but condition variables work.

**Semaphores:

**Condition Variables:

**Example where semaphores fail but condition variables work: Producer–Consumer problem

**Note: Use Semaphores for resource availability (e.g., # of slots/items). Use Condition Variables for state changes (e.g., buffer empty/full, predicate-based waits).

14. Explain the Dining Philosophers problem and how the Chandy/Misra solution improves over classical methods.

**Classical Problem:

**Chandy/Misra Solution:

**Benefits:

**Note: Chandy/Misra’s algorithm shifts from centralized locking to token-based distributed synchronization, ensuring fairness and avoiding deadlock.

15. What is the difference between Read–Write locks and Binary locks? When would Read–Write locks cause performance degradation?

**Binary Locks (Mutexes):

**Read–Write Locks (RW Locks):

**Performance Degradation in RW Locks:

**Note: RW locks outperform binary locks in read-heavy workloads, but mismanagement can lead to writer starvation.

16. How does OS handle synchronization in SMP (Symmetric Multiprocessing) systems differently from uniprocessor systems?

**Uniprocessor: Only one CPU executes at a time, so synchronization can often be ensured by disabling interrupts to prevent context switches during critical sections.

**SMP (Symmetric Multiprocessing): Multiple CPUs may execute concurrently and access shared memory. Disabling interrupts is insufficient since other CPUs can still run.

**SMP Challenges: Multiple CPUs may modify shared data concurrently. Synchronization primitives must be scalable to avoid bottlenecks.

17. Compare Monitor-based synchronization and Lock-based synchronization in terms of safety and maintainability.

**Lock-based: Programmer explicitly acquires/releases locks (mutexes, semaphores).

**Monitor-based: High-level abstraction where mutual exclusion is implicit. The language or runtime acquires the lock on method entry and releases on exit.

**Note: Monitors improve safety and maintainability, while locks offer flexibility but more risk of error.

18. What is a Barrier in concurrency? Explain a scenario where barriers can cause deadlock.

**Barrier in Concurrency:

**Deadlock Scenario with Barriers:

**Solution: Use timeout-based barriers (threads proceed or abort if some don’t arrive within a time limit). Adopt fault-tolerant designs where missing participants are skipped or replaced.

19. How do optimistic concurrency control techniques work in OS-level transaction management, and what is their drawback?

**Working: OCC assumes conflicts are rare. Transactions execute freely without locks, recording read/write sets. At validation (commit) time, the OS checks whether another transaction modified the same data.

**Advantages:

**Drawback:

Note: OCC lets transactions run without locks and checks for conflicts only at commit. It’s efficient in low-contention scenarios but suffers from high rollback overhead when many processes compete for the same data.

20. How does the OS implement fairness in semaphore-based synchronization to avoid starvation?

**Fairness in Semaphore-Based Synchronization

The OS avoids starvation in semaphores by using queue-based scheduling (FIFO) instead of arbitrary wakeups, guaranteeing fairness and predictable resource access.