A bakery shop has to provide a stream of muffins for customers. The muffins are made by a baker in the kitchen and placed on a conveyor belt. The conveyor belt carries the muffins to where the customers are waiting to buy them. This scenario has been simulated using two processes: the baker and customer, and a shared conveyor belt implemented as a circular array called conveyor, where each space in the array can hold one muffin. There are two shared general semaphores, empty and full, and a mutex buffer_mutex. In this scenario, there is only multiple bakers and a single customer.
The pseudo-code for the baker is as follows. The baker makes use of an integer variable in for noting the next available space on the conveyor belt.
- while(true){
- muffin = makeMuffin(); // Create a muffin
- wait(empty);
- wait(buffer_mutex);
- conveyor[in] = muffin; // Put the muffin on the conveyor belt
- in = (in + 1) mod n;
- signal(buffer_mutex);
- signal(full);
- }
The pseudo-code for the customer is as follows. The customer makes use of an integer variable out for noting the next location on the conveyor belt that contains a muffin.
- while(true){
- wait(full);
- muffin = conveyor[out]; // Get a muffin from the conveyor belt
- conveyor[out] = null;
- out = (out + 1) mod n;
- signal(empty);
- eat(muffin); // Eat the muffin
- }
What will happen if the order of semaphores in the customer changes. So, we have in line 2 signal(empty) and in line 6 wait(full).