operating-systems concurrency

Definition

Binary Semaphore

A binary semaphore is a specialised type of semaphore that can only take the values 0 or 1. It is primarily used to implement mutual exclusion for a single shared resource.

Usage for Mutual Exclusion

To protect a critical section, a binary semaphore is initialised to 1.

// Initialisation
init(S, 1);
 
// Process Pi:
wait(S); // Acquire lock
    // --- Critical Section ---
signal(S); // Release lock
    // --- Remainder Section ---

Comparison

While a binary semaphore is functionally similar to a mutex, in some operating systems, a mutex has an “ownership” property (only the process that locked it can unlock it), whereas a binary semaphore can be signaled by any process.