Shared data:
semaphore agent;
semaphore tobacco,paper,match;
code:
do{
wait(agent);
if(tobacco and paper){
wait(matches);
// critical section (roll and smoke)
signal(matches);
}else if(paper and matches){
wait(tobacco);
// critical section (roll and smoke)
signal(tobacco);
}else{
wait(paper);
// critical section (roll and smoke)
signal(paper);
}
signal(agent);
}while(true);
Please check @championswimmer-t