ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > zstu-(2021-2022)-1 > student homework directories > >
2019329621175-张子行homework-6 Version 0
👤 Author: by 1148873699qqcom 2021-12-23 09:20:34
Suppose that there is a thriving buffet restaurant in the city center with a pizza section with a window that offers only one type of pizza (e.g., durian pizza, fruit pizza, etc.). Kitchen 1 serves only durian pizzas, kitchen 2 serves only fruit pizzas, and customers at the first table get only durian pizzas and customers at the second table get only fruit pizzas. Only when the window is empty, the kitchen can put the finished pizza in the window; only when there is the pizza they need in the window, the customer will come to the window to pick it up.

Relationship analysis. From the fact that only one type of pizza can be put into the window at a time, it is clear that the relationship between the kitchen and the customer is mutually exclusive. The kitchen and the customer are synchronous, and the two pairs of processes must be connected. There is no mutually exclusive and synchronous relationship between the guests, because they are selecting conditional execution and cannot be concurrent.

Semaphore setup. First set the semaphore window as a mutually exclusive semaphore, indicating whether to allow pizza into the window, with an initial value of 1, indicating that it is allowed and only one copy is allowed to be put in. Signal durian indicates whether there is durian pizza in the window, the initial value is 0, which means the window is empty and not allowed to take, if durian=1 can take. Signal fruit indicates whether there is fruit pizza in the plate, the initial value is 0, which means the window is empty and not allowed to take, if fruit=1 can be taken.

 
semaphore window=1, durian_pizza=0, fruit_pizza=0;
kitchen_1() {
while (1) {
prepare durian pizza;
P(window) ;
put the durian_pizza on the window;
V(durian_pizza);
}
}
kitchen_2() {
while(1) {
prepare fruit pizza;
P(window);
put the fruit_pizza on the window;
V(fruit_pizza);
}
}
customer_1(){
while(1){
P(fruit_pizza) ;
take an fruit_pizza from the window;
V(window);
eat the fruit_pizza;
}
}
customer_2(){
while(1) {
P(durian_pizza);
take an durian_pizza from the window;
V(window);
eat the durian_pizza;
}
}

Please login to reply. Login

Reversion History

Loading...
No reversions found.