当前位置:网站首页>PV operation daily question - Restaurant Service

PV operation daily question - Restaurant Service

2022-06-10 16:10:00 liangsena


It is similar to the problem of single log bridge , Later, there is the problem of single wooden bridge

One 、 Problem description

A restaurant serves Chinese food 、 Thai food and Spanish food , Customers can choose which kind of food to eat . The restaurant has the following regulations : The restaurant only offers one kind of cuisine at any one time , And it shall be provided according to the requirements of the first customer entering the restaurant , The last customer who chooses a certain dish can only provide another dish after leaving ; Customers who choose the current cuisine can enter the restaurant immediately , The restaurant allows multiple customers to eat at the same time , There is no limit to the quantity ; When other customers have to wait for their dishes to be offered , Then you can enter the restaurant for dinner in turn .
Please use PV The operation describes the above synchronization and mutual exclusion problems , The meaning of semaphore and initial value is given .


Two 、 problem solving

// Set three values :1: Chinese food ,2: Thailand cuisine ,3: Spanish food 
int i=1,2,3;
int count_i=0;     // The current number of people in a restaurant 
semaphore mutex=1;  // Restaurants offer mutually exclusive cuisines 
semaphore mutex_i=1;// Exclusive access count Variable 

Costumers_i()
{
    
    P(mutex_i);
    count-i++;
    if(count_i==1)
    {
    
        P(mutex);
    }
    V(mutex_i);
     having dinner ;
    P(mutex_i);
    count_i--;
    if(count_i==0)
    {
    
        V(mutex);
    }
    V(mutex);
}

The original answer is very long , Make a list of each one , Feel no need to , Just use variables instead of .

3、 ... and 、 twitter

Today was too tired , That's it ~

原网站

版权声明
本文为[liangsena]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101014232850.html