homework-4 2017329621045 Version 0 |
|
👤 Author: by mhl0414163com 2019-10-05 02:20:03 |
think about your possible case about process synchronization, and give out a solution for the problem.
The two waiters wait for a meal at a window. Two chefs serve through this window. The window can only produce one dish at a time. The waiter a is responsible for the chef's dish, and the waiter b is responsible for the chef b's dish.
Constraints:
a) Two chefs compete for the window, when the chef a puts food on the window, the chef b waits, or vice versa;
b) the chef a and the waiter a want to synchronize, the chef a tells the waiter a to take it after putting a dish; Waiter a will inform the window that the window is available after taking the food;
c) the chef b and the waiter b are to be synchronized, the chef a informs the waiter b after taking a dish; and the waiter b takes the food and informs the window that it is available;
S represents the critical resource window, the initial value is 1; chef a and waiter a need a semaphore to synchronize S1=0, chef b and waiter b need a semaphore to synchronize S2=0;
Var S, S1, S2:semorphore=1,0,0;
chef a:
Repeat
cooking;
Wait(S);
putting a dish;
Single(S1);
Until false;
chef b:
Repeat
cooking;
Wait(S);
putting a dish;
Single(S2);
Until false;
waiter a:
Repeat
Wait(S1);
takes the dish;
Single(S);
Serving customers;
Until false;
waiter b:
Repeat
Wait(S2);
takes the dish;
Single(S);
Serving customers;
Until false;