A semaphore is a simple but powerful tool, proposed by Edsger Dijkstra, for both mutual exclusion and ordering between processes.
Semaphores
A semaphore S is an integer variable that, apart from its initialisation, is accessed only through two atomic operations. wait(S), also called P or down: while S ≤ 0, wait; then S = S − 1. signal(S), also called V or up: S = S + 1. A binary semaphore, with values 0 and 1, works like a mutex lock. A counting semaphore can take any non-negative value and controls access to a resource with several instances, being initialised to the number of instances. To avoid busy waiting, the OS implements wait() by blocking the process and putting it in a waiting queue for the semaphore, and signal() by waking one waiting process.
Definition: Semaphore. An integer synchronisation variable accessed only through the atomic operations wait (decrement, blocking if the value is not positive) and signal (increment, waking a waiting process).
Using Semaphores
For mutual exclusion, a semaphore mutex is set to 1, and each process does wait(mutex); critical section; signal(mutex). For ordering, to make statement S2 in P2 run after S1 in P1, a semaphore synch is set to 0. P1 does S1; signal(synch), and P2 does wait(synch); S2.
The Producer-Consumer (Bounded-Buffer) Problem
A producer puts items into a buffer of n slots, and a consumer takes them out. The producer must wait when the buffer is full, the consumer must wait when it is empty, and they must not change the buffer at the same time. Three semaphores solve it: mutex = 1 for exclusive access, empty = n counting empty slots, and full = 0 counting full slots. Producer: wait(empty); wait(mutex); add item; signal(mutex); signal(full). Consumer: wait(full); wait(mutex); remove item; signal(mutex); signal(empty). The order matters: if the producer did wait(mutex) before wait(empty) on a full buffer, it would hold the mutex while waiting, and the consumer could never enter, causing deadlock.
Readers-Writers Problem
In the readers-writers problem, many readers may read shared data at once, but a writer needs exclusive access. It is solved with a mutex, a write semaphore and a count of readers.
Table 3.3: Semaphores in the bounded-buffer problem
| Semaphore | Initial value | Purpose |
|---|---|---|
| mutex | 1 | Exclusive access to the buffer |
| empty | n | Number of empty slots |
| full | 0 | Number of filled slots |
Example 3.3: Semaphore values
Problem: A counting semaphore printers = 3 controls three printers. Processes A, B, C, D each call wait(printers), and then A calls signal(printers). What happens?
Solution:
- A: 3 → 2, gets a printer. B: 2 → 1. C: 1 → 0.
- D: value is 0, so D blocks.
- A calls signal: the semaphore wakes D, which gets A's printer.
Result: At most three processes use printers at once, and D waits until one is released.
#include <stdio.h>
#include <pthread.h>
#define N 3
int buf[N], in = 0, out = 0, cnt = 0;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t notfull = PTHREAD_COND_INITIALIZER;
pthread_cond_t notempty = PTHREAD_COND_INITIALIZER;
void *producer(void *a) {
for (int i = 1; i <= 5; i++) {
pthread_mutex_lock(&m);
while (cnt == N) pthread_cond_wait(¬full, &m);
buf[in] = i; in = (in + 1) % N; cnt++;
pthread_cond_signal(¬empty);
pthread_mutex_unlock(&m);
}
return a;
}
void *consumer(void *a) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
pthread_mutex_lock(&m);
while (cnt == 0) pthread_cond_wait(¬empty, &m);
sum += buf[out]; out = (out + 1) % N; cnt--;
pthread_cond_signal(¬full);
pthread_mutex_unlock(&m);
}
printf("consumer used 5 items, sum = %d\n", sum);
return a;
}
int main(void) {
pthread_t p, c;
pthread_create(&p, NULL, producer, NULL);
pthread_create(&c, NULL, consumer, NULL);
pthread_join(p, NULL); pthread_join(c, NULL);
return 0;
}
Output
consumer used 5 items, sum = 15
Common mistakes
- Reversing the order of wait(empty) and wait(mutex), which can cause deadlock.
- Calling signal on the wrong semaphore.
- Initialising empty to 0 instead of the buffer size.
Key takeaways
- wait(S) decrements and may block; signal(S) increments and may wake.
- A binary semaphore gives mutual exclusion; a counting one controls n instances.
- Bounded buffer: mutex = 1, empty = n, full = 0.
- The order of wait operations matters.
Practice questions
- What are the two operations on a semaphore?
- What are the initial values in the bounded-buffer solution?
- What is a counting semaphore used for?
Answers to the practice questions
- wait (P), which decrements and may block, and signal (V), which increments and may wake a process.
- mutex = 1, empty = n, full = 0.
- To control access to a resource that has several instances.
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit III
