Interview Q&A / Operating Systems

INTERVIEW Q&A · 42 QUESTIONS

Operating Systems interview questions

Processes, threads, scheduling, memory and deadlocks, in plain words.

  1. What is a race condition, and what is the critical section problem?

    A race condition happens when two or more threads access shared data at the same time and the result depends on the exact timing of their steps. The critical section is the part of code that touches that shared data, and the critical section problem is designing a way so only one thread runs it at a time, with progress and bounded waiting.

    BeginnerIn-depth3 min read
  2. What is the difference between a mutex and a semaphore?

    A mutex is a lock that lets only one thread into a critical section at a time, and only the thread that locked it should unlock it. A semaphore is a counter that allows up to N threads to use a resource at once, and any thread can signal it, which also makes it useful for one thread to notify another.

    BeginnerIn-depth3 min read
  3. What is a deadlock and how can you prevent it?

    A deadlock is when two or more processes wait for each other forever, each holding something the other needs. It needs four conditions together; breaking any one of them, for example always taking locks in the same order, prevents it.

    IntermediateIn-depth2 min read
  4. What is a counting semaphore and when would you use one?

    A counting semaphore holds a non-negative integer that tracks how many units of a resource are free. wait() takes one unit (blocking at zero) and signal() returns one. It limits how many threads use a pool at once.

    BeginnerQuick answer1 min read
  5. How do you solve the producer-consumer problem using semaphores?

    Use three semaphores: empty (counts free slots, starts at N), full (counts filled slots, starts at 0) and mutex (starts at 1). Producers wait on empty, consumers wait on full, and mutex guards the buffer.

    IntermediateQuick answer1 min read
  6. What is a condition variable and why does wait() release the lock?

    A condition variable lets a thread sleep until some condition becomes true. wait() atomically releases the lock and sleeps so other threads can change the state; on wakeup the thread reacquires the lock and rechecks.

    IntermediateQuick answer1 min read
  7. What is a monitor in operating systems, and how is it different from a semaphore?

    A monitor bundles shared data with the procedures that access it, and the language guarantees only one thread is inside at a time. A semaphore is a bare counter that the programmer must use correctly everywhere.

    IntermediateQuick answer1 min read
  8. What is a spinlock, and when is it better than a lock that puts the thread to sleep?

    A spinlock makes a waiting thread loop on the CPU until the lock is free. It is better when the lock is held for a very short time, because sleeping and waking cost more than a brief spin.

    IntermediateQuick answer1 min read
  9. What is livelock, and how is it different from deadlock?

    In deadlock, processes are blocked and make no progress. In livelock they keep running and changing state in response to each other, but still make no progress, like two people repeatedly stepping aside in a corridor.

    IntermediateQuick answer1 min read
  10. What is the dining philosophers problem and how can it be solved?

    Five philosophers sit round a table with five forks, and each needs two adjacent forks to eat. If all pick up their left fork together, everyone waits forever. Solutions include a fork ordering, a limit of four seated, or an asymmetric pickup.

    IntermediateQuick answer1 min read
  11. What is the Test-and-Set instruction and how does it provide mutual exclusion?

    Test-and-Set is a hardware instruction that atomically reads a memory flag and sets it to 1. If it returned 0, the caller got the lock; if 1, someone else holds it. Because it is one indivisible step, two threads cannot both see 0.

    AdvancedQuick answer1 min read