Chapter 3

Semaphores and Producer-Consumer

Use wait/signal operations to avoid race conditions and coordinate bounded buffers.

Estimated time: 28 min

Semaphore is a counter-based gatekeeper for shared resources.

Semaphore patterns appear in producer-consumer and many other synchronization problems.

Semaphore Operations

From notes

Clear explanation

`wait` acquires permission (or blocks), and `signal` releases permission (or wakes a waiter).

What it really means

Like entry tokens at a lab with limited seats.

Example

Binary semaphore behaves like lock/unlock for critical section access.

Key takeaway

Semaphores combine synchronization and blocking without busy wait.

Bounded Buffer Pattern

From notes

Clear explanation

Producer waits on empty slots then mutex; consumer waits on full slots then mutex; both signal in reverse logic.

What it really means

You need both room availability and exclusive table access before placing/removing items.

Key takeaway

Correct semaphore order prevents overflow, underflow, and race conditions.

  • - Confusing mutex semaphore and counting semaphore
  • - Wrong order of wait calls causing deadlock
  • - Not separating mutual exclusion from empty/full constraints
  • - wait(P/down) decrements; signal(V/up) increments
  • - Use mutex for critical section and counting semaphores for capacity
  • - Producer-consumer needs empty, full, and mutex coordination

Exam lens for this topic

What evaluators usually expect in structured exam answers.

Must-use keywords

  • - semaphore
  • - producer consumer
  • - bounded buffer
  • - wait
  • - signal

Answer flow

  • - Write exact definition in first line
  • - Explain mechanism in ordered bullets
  • - Add one short example or scenario
  • - Close with key takeaway and one exam keyword

Mini quiz

Quick self-check from this topic before moving ahead.

1. In producer-consumer, which semaphore tracks available empty slots?

Practice Questions

  • Discuss bounded buffer Producer-Consumer using semaphore.

    Source: Summer 2023 Q3(C) OR

    Answer focus: Algorithm with empty/full/mutex logic and explanation.

  • Explain Producer Consumer problem using Semaphore with algorithm.

    Source: Summer 2024 Q3(B)

    Answer focus: Detailed sequence and correctness conditions.

Practice from papers (end-of-topic set)

These paper questions map directly to this topic. Solve now, then compare your structure with linked topics.

Question Bank Linked Here

Open all questions

How to answer linked exam questions

Full question bank