Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Cigarette-Smokers Problem. Consider a system with three smoker processes and

ID: 3540864 • Letter: T

Question

The Cigarette-Smokers Problem. Consider a system with three smoker processes and one agent
process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and
smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches. One
of the smoker processes has paper, another has tobacco, and the third has matches. The
agent has an infinite supply of all three materials. The agent places two of the ingredients
on the table. The smoker who has the remaining ingredient then makes and smokes a
cigarette, signaling the agent on completion. The agent then puts out another two of the
three ingredients, and the cycle repeats. Write a program to synchronize the agent and the
smokers.

Explanation / Answer

The shared data structures are
var a: array [0...2] of semaphore finitially = 0g
agent: semaphore finitially = 1g
The agent process code is as follows:
rep
repeat
Set i, j to a value between 0 and 2.
wait(agent);
signal(a[i]);
signal(a[j]);
until false;
Each smoker process needs two ingredients represented by integers r and s eachwith value
between 0 and 2.

rep
repeat
wait(a[r]);
wait(a[s]);