operating-systems concurrency

Definition

Binary Semaphore

A binary semaphore is a semaphore restricted to values . It is used primarily to implement mutual exclusion for access to a single shared resource.

Operations

A binary semaphore supports two atomic operations:

OperationSemanticsEffect
wait(S)AcquireIf , set and proceed; otherwise block until
signal(S)ReleaseSet , potentially unblocking a waiting process

Mutual Exclusion Protocol

Semaphore S = 1                    // resource initially free

Void process()
    while (true) {
        wait(S);                   // acquire lock
        
        /* --- Critical Section --- */
        
        signal(S);                 // release lock
        
        /* --- Remainder Section --- */
    }

State Machine

The semaphore alternates between two states:

  • : resource available, next wait succeeds immediately
  • : resource held, processes block on wait

Comparison with Mutex

Ownership Distinction

A binary semaphore is functionally similar to a mutex, but differs in ownership semantics:

  • Mutex: Only the process that locked the mutex may unlock it (ownership property)
  • Binary semaphore: Any process may call signal(S), enabling different signalling patterns