Sub topic 10
Semaphores: wait, signal, binary and counting
Assume you know process and thread basics, can read C-like pseudocode, and understand preemption by a CPU scheduler.
A semaphore is a synchronization object that stores a non-negative integer. Threads use two atomic operations:
wait(S)decreasesSwhenS > 0. IfS == 0, the calling thread blocks until another thread signals the semaphore.signal(S)increasesSand may wake one blocked thread.
The operation is atomic, so another thread cannot observe a partially completed decrement or increment.
Binary and counting semaphores
A binary semaphore has only two useful values, 0 and 1. It can protect a critical section:
semaphore mutex = 1
wait(mutex)
critical_section()
signal(mutex)Only one thread can pass wait(mutex) while the value is 1. Other threads wait until the first thread calls signal.
A counting semaphore can have any non-negative value. Its value represents available units of a resource.
| Semaphore type | Values | Typical use |
|---|---|---|
| Binary | 0 or 1 | One shared resource or an exclusion gate |
| Counting | 0 or more | Several identical resources or available items |
Unlike a mutex, a semaphore does not generally require the same thread to perform wait and signal. This makes semaphores flexible, but it also makes incorrect signaling easier.
Tracing semaphore operations
Start with S = 2.
- Thread A executes
wait(S). The value becomes1. - Thread B executes
wait(S). The value becomes0. - Thread C executes
wait(S). C blocks because no permit is available. - A executes
signal(S). One permit becomes available, and C may wake. - C completes its
wait, so the available count returns to0.
The exact internal value during wake-up depends on the implementation, but the logical result is that two threads can hold permits at once.
A counting semaphore in C
POSIX provides semaphores through sem_t. This program creates two permits, so at most two worker threads enter the protected region at once.
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
sem_t permits;
void *worker(void *argument) {
int id = *(int *)argument;
sem_wait(&permits);
printf("worker %d entered\n", id);
sleep(1);
printf("worker %d leaving\n", id);
sem_post(&permits);
return NULL;
}
int main(void) {
pthread_t threads[3];
int ids[3] = {1, 2, 3};
sem_init(&permits, 0, 2);
for (int i = 0; i < 3; i++)
pthread_create(&threads[i], NULL, worker, &ids[i]);
for (int i = 0; i < 3; i++)
pthread_join(threads[i], NULL);
sem_destroy(&permits);
return 0;
}The output order can vary, but no more than two workers should be inside the region simultaneously.
Common errors include calling signal too few times, causing permanent blocking, and calling it too many times, creating permits that do not correspond to real resources. Reversing synchronization operations can also cause deadlock. A semaphore protects the intended resource only when every access follows the same protocol.
Exercise
A counting semaphore starts at 3. Four threads each execute wait once, and the first three threads later execute signal once. How many threads remain blocked, and what is the final logical count?
Show answer
No thread remains blocked after the three signals. The final logical count is 2: three permits were consumed and three were returned, while the fourth thread consumed the remaining permit when it woke.