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

Q2 Propose a synchronization solution to the following recipe cooking problem. T

ID: 3908474 • Letter: Q

Question

Q2 Propose a synchronization solution to the following recipe cooking problem. Three chefs (threads) want to cook a recipe of meet, rice and vegies. Each chef has infinite supply of one of the recipe ingredients and will need the other two ingredients from the supermarket. Assume chef1 has meat, chef2 has rice, and chef3 has vegies. The supermarket (thread) puts two of the ingredients, chosen randomly, on sail every day. Chefs are strictly buying ingredients when on sail, which means only one chef will be able to cook every day. How can we use semaphores to solve this problem? All chef thread should be blocked waiting for supermarket sail, only one chef will be allowed to cook. Assume that every recipe ingredient, namely meet, rice, and vegies has its own semaphore. Only one chef can acquire the ingredient. Every chef will need to wait for two ingredients signaled by the supermarket thread Hint: there is an obvious potential for deadlocks waiting for ingredients by chefs. Try to prevent the deadlock using either: 1- Preventing circular wait using a certain order of ingredient acquisition 2- Preventing hold and wait by releasing one ingredient semaphore before acquiring the other. Note: you have to prove that your solution actually works by providing an example where only the proper chef ends up cooking the recipe.

Explanation / Answer

sem_wait(slots){

Semaphores s[3]={rice,meat,veggies};

return rand(s);

}

Item(data)

ingrd=sem_wait(items);

mutex_lock(mutex);

buffer[ insertPtr ] = ingrd;

insertPtr = (insertPtr + 1) % N;

mutex_unlock(mutex);//unlocking after getting item//

sem_signal(items);

cook(item)

ingrd1=sem_wait(items); //semaphore//

mutex_lock(mutex);

i=1;

while(s[i]!=ingrd&&i<=3){

i++;

}

chef=i;//ith chef will cook//

result = buffer[removePtr];

removePtr = (removePtr +1) % N;

mutex_unlock(mutex);

sem_signal(slots);

e.g.

If randomly rice and veggies are chosen then item(data) will keep working till meat is also in buffer.

here last item is meat so chef1 will cook.