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 Th

ID: 3909397 • 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

we have three chefs say three process namely chef-1 , chef-2 , chef-3 . as deadloack is occuring here because they have one ingrident with them . they need other two ingridents to buy from market . so market can be compared to critical region as this problem is of synchronization . it can be solved using semaphores .

semaphores for synchronizing threads is used here .and other two threads are applied lock till one chef-1 finishes its shopping and come out of market (critical region) then chef-2 lock is unlocked and it can go . so by this one by one they access their shared variables (ingredients ).

semaphore has two operations

1. wait()

2. signal()

initially semaphore value is positive. when one chef is is market other two has to wait . and as soon as it finishes its work signal () is carried out and semaphore's value turned to negative