Starting from:

$25

CSE344 - System Programming - Homework 3 - v4 - Semaphores - Solved

You will prepare a small bakers simulation inspired by a classic synchronization problem. Let’s assume for the sake of simplicity that one needs exactly 4 ingredients for preparing güllaç: - milk (M)

-  flour (F)

-  walnuts (W) - and sugar (S).

6 x Chef
There are 6 chefs in the main street of Gebze, and each has an endless supply of two distinct ingredients and lacks the remaining two:

chef0 has an endless supply of milk and flour but lacks walnuts and sugar, chef1 has an endless supply of milk and sugar but lacks flour and walnuts, chef2 has an endless supply of milk and walnuts but lacks sugar and flour, chef3 has an endless supply of sugar and walnuts but lacks milk and flour, chef4 has an endless supply of sugar and flour but lacks milk and walnuts, chef5 has an endless supply of flour and walnuts but lacks sugar and milk.

Every chef sits in front of her/his store and waits for the remaining two ingredients to arrive in order to go and prepare güllaç. Once the güllaç is ready they deliver it to the wholesaler and continue to wait for more ingredients. In case of termination, each chef process will return (and by “return” I actually mean return, not print on screen) the total number of desserts it has prepared so far.

1 x Wholesaler
There is also a wholesaler that every once in a while delivers two distinct ingredients out of the four to the street of the chefs, lets the chefs know that the ingredients have arrived and then waits for the dessert to be ready.

However, do not assume that the wholesaler knows which chef needs which ingredients. So do not make the mistake of signaling directly from the wholesaler only the chef corresponding to the delivered ingredients. Example: if the wholesaler delivers F and M, don’t just signal/sem_post
the chef waiting for F and M.
 
Once the güllaç is ready, the wholesaler takes it for sale and the chefs must wait again for the next visit of the wholesaler.

The wholesaler will read the ingredients to deliver from an input file containing two capital letters at each line, representing the ingredients to deliver; e.g.

MS

FM

WS SM

etc

Assume the file has valid content and at least 1 row. The ingredients will be stored in a character array taking place in a shared memory segment; if the wholesaler for instance delivers SM, the array will contain ‘S’ and ‘M’. After the wholesaler is done (i.e. he has no more ingredients to deliver, he must notify the chefs that the procedure is over – how? That’s up tp you.) It will collect the return values of the chef processes, and print the total number of desserts delivered to him.

How
You will implement the program as the chefs and the wholesaler in the form of 6+1 processes that print on screen their activities. You will implement two version of the program, one solving it with named semaphores and another solving it with unnamed semaphores.

./hw3named -i inputFilePath -n name 

./hw3unnamed -i inputFilePath

-i: denotes the input file path (absolute or relative) hw3named: must use named semaphores for synchronization (you can include additional args if necessary). hw3unnamed: must use unnamed semaphores for synchronization.

Output
Chefs (print the contents of the character array containing the ingredients at the end of every output line):

Initially (replace the bold parts; e.g. i becomes the chef id, ingredient becomes Milk, etc):

chefi (pid XYZ) is waiting for ingredient1 and ingredient2

After obtaining the ingredients (replace the bold parts):

chefi (pid XYZ) has taken the ingredient1 chefi (pid XYZ) has taken the ingredient2 chefi (pid XYZ) is preparing the dessert chefi (pid XYZ) has delivered the dessert

At exit (replace the bold parts):

chefi (pid XYZ) is exiting

The wholesaler  (replace the bold parts):

the wholesaler (pid XYZ) delivers ingredient1 and ingredient2

the wholesaler (pid XYZ) is waiting for the dessert the wholesaler (pid XYZ) has obtained the dessert and left the wholesaler (pid XYZ) is done (total desserts: 34) 

More products