The "sandwich makers problem" is based on the "cigarette smokers problem", originally described in 1971 by Suhas Patil, with the aim to highlight the issue of synchronization using semaphores.
The system consists of a total of four processes/threads: three processes/threads representing sandwich makers and one process/thread for the supplier. Each sandwich maker has an unlimited supply of one of the three ingredients needed to make a sandwich: bread, cheese, or ham. Each maker has a different ingredient, meaning one has only bread, another only cheese, and the third only ham. The supplier has all three components in unlimited quantities. The supplier randomly selects two different components, places them on the table, and signals the sandwich makers that the ingredients are available. Only the sandwich maker who lacks the ingredients placed on the table is allowed to take both, signal the supplier, then assemble and eat the sandwich. The supplier then places two new ingredients on the table, and the process repeats. The task is to correctly synchronize the sandwich makers and the supplier.
Supplier() {
repeat {
...
(s1, s2) = randomly_select_two_different_ingredients
place_ingredients_on_table(s1, s2)
...
} forever
}
SandwichMaker(p) {
(r1, r2) = ingredients_maker_does_not_have(p)
repeat {
...
if (ingredients_on_table_match_needed(r1, r2) == YES) {
take_ingredients(r1, r2)
...
assemble_and_eat_sandwich()
...
}
} forever
}
# ./a.out Sandwich Maker 1: has bread Sandwich Maker 2: has cheese Sandwich Maker 3: has ham Supplier places: bread and cheese Sandwich Maker 3: takes ingredients and ... Supplier places: ham and cheese Sandwich Maker 1: takes ingredients and ... ^C #