当前位置:网站首页>PV operation daily question - ticket sales

PV operation daily question - ticket sales

2022-06-10 10:46:00 liangsena


The classic synchronization problem of fried chicken !

One 、 Problem description

The car driver and the conductor must work together , On the one hand, the driver can only drive after the conductor closes the door , therefore , The conductor shall close the door and inform the driver to drive , Then the conductor sells tickets . On the other hand , Only when the car has stopped , The conductor can open the door to get on and off the passengers , Therefore, the driver shall inform the conductor after parking . Suppose there is a driver and two conductors on a certain bus , The car is currently stopping at the departure station to pick up passengers , Try semaphores and PV Operate to solve the problem .

The reason for the two conductors is that the bus used to have two carriages , So there are two conductors , The background of a relatively old topic ~


Two 、 problem solving

semaphore close1=0;
semaphore close2=0;
semaphore open1=0;
semaphore open2=0;

driver()
{
    
    while(1)
    {
    
        P(close1);	// The driver asked the conductor 1:“ Close the door ”
        P(close2);	// The driver asked the conductor 2:“ Close the door ”
         Driving a car ;
         Parking ;
        V(open1);	// The driver informed the conductor 1:“ The car stopped , You can open the door ”
        V(open2);	// The driver informed the conductor 2:“ The car stopped , You can open the door ”
    }
}

conductor1()
{
    
    while(1)
    {
    
         A guest - close - Sell ticket ;
        V(close1);      // Answer the driver :“ The door is closed , You can drive ”
         The bus is running ;
        P(open1);       // Asked the driver :“ It stopped ”
         Open door - Drop off ;
    }
}
conductor2()
{
    
    while(1)
    {
    
         A guest - close - Sell ticket ;
        V(close2);      // Answer the driver :“ The door is closed , You can drive ”
         The bus is running ;
        P(open2);       // Asked the driver :“ It stopped ”
         Open door - Drop off ;
    }
}


That's it , See you tomorrow !

原网站

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