Mohamed Boudiaf University – M’sila Operating Systems II
Department of Computer Science 2025-2026
3rd year, Computer Science - SI
Tutorial Session N°4: Monitors
Objective: Understand and apply process synchronization using monitors through exercises on managing
shared resources, coordinating threads, and solving classic concurrency problems.
Propose a monitor-based synchronization algorithm for each of the following exercises:
Exercise 1: (Positive Shared Counter)
Consider a shared integer counter that is initially zero. Two types of processes can access the counter:
incrementers, which increase its value, and decrementers, which decrease its value. A decrementer may
only execute if the counter is greater than zero; otherwise, it must wait until the counter becomes positive.
Exercise 2: (Barber Shop)
Consider a barber shop with N chairs for waiting customers, and a chair C for the customer being served,
and a barber B.
If there are no customers, the barber B sleeps in chair C.
When a customer arrives while the barber B is sleeping, the customer wakes him up, sits in C, and
gets a haircut.
If a customer arrives while the barber is busy: if one of the N chairs is free, the customer sits and
waits; otherwise, the customer leaves.
When the barber finishes a haircut and there are waiting customers, he serves the next customer.
Exercise 3: (Swimming Pool)
An Olympic swimming pool can receive swimmers from three synchronized swimming teams: T1, T2, and
T3. To organize training sessions, the rule is: at any given time, the pool can accommodate any number of
swimmers but from at most 2 teams. For example, swimmers from teams T1 and T3 can train
simultaneously, but if a swimmer from team T2 wants to enter, they must wait until all swimmers from one
team have left the pool.
[1 / 2]
Mohamed Boudiaf University – M’sila Operating Systems II
Department of Computer Science 2025-2026
3rd year, Computer Science - SI
Exercise 4: (Bridge Load)
A bridge can support a maximum load of 20 tons. The bridge is crossed by trucks weighing 20 tons and
cars weighing 5 tons. You are asked to manage access to the bridge so that:
The maximum bridge load is respected.
Priority is given to cars: when a car and a truck request access simultaneously, the car must be
allowed first, provided the bridge’s maximum capacity is not exceeded.
Exercise 5: (ATM Alternation)
An ATM can be used by only one person at a time. When there are women and men waiting, a strict
alternation must be ensured: after a woman uses the ATM, it must be a man’s turn (if a man is waiting),
otherwise women continue. The same applies for men. Initially, the turn is given to women.
Exercise 6: (single railway track)
Two cities A and B are connected by a single railway track. We consider two classes of processes: trains
traveling from A to B (T-AB) and trains traveling from B to A (T-BA). There may be an unspecified number
of processes (i.e., trains). The traffic rules are as follows:
The track must never be used simultaneously by two trains traveling in opposite directions.
The track may be used simultaneously by one or more trains, as long as they are all traveling in the
same direction.
Trains traveling from B to A have priority.
GOOD LUCK
[2 / 2]