Course contentsShow
← Computer Science
Lesson 1294 of 2,87230. Process SynchronizationFree lesson

Wait (P) and Signal (V) Operations

Learning the atomic wait/down/P and signal/up/V operations that decrement and increment semaphore values.

Wait (P) and Signal (V) Operations

What you'll learn: You'll understand the two atomic operations that control access to semaphores—wait() (also called P or down) and signal() (also called V or up).

The Core Operations

Semaphores work through exactly two operations that modify their internal integer value atomically (meaning they cannot be interrupted mid-execution):

Wait Operation (also called P, from Dutch "proberen" = to test, or down):

  • Attempts to decrement the semaphore's value by 1
  • If the value is already 0, the calling thread/process blocks (waits) until the value becomes positive
  • Once positive, it decrements and the thread proceeds

Signal Operation (also called V, from Dutch "verhogen" = to increment, or up):

  • Increments the semaphore's value by 1
  • If any threads are blocked waiting on this semaphore, one is woken up and allowed to proceed

Simple Analogy

Think of a semaphore as a ticket dispenser at a busy restaurant:

  • Wait/P: Try to grab a ticket. If none available (count = 0), stand in line. When you get one, the count goes down.
  • Signal/V: Return your ticket when done. The count goes up, and the next person in line can grab it.

Why "Atomic" Matters

Both operations must be indivisible. No other thread can observe or interfere with the semaphore mid-operation. This atomicity is what prevents race conditions when multiple threads try to access the semaphore simultaneously.

The operations always come in pairs in correct code: every wait() eventually has a matching signal() somewhere (though not necessarily by the same thread, especially with counting semaphores).

Key Takeaway: wait() decrements and may block; signal() increments and may unblock—both execute atomically to coordinate safe access to shared resources.