当前位置:网站首页>PV operation daily question - black and white chess question (variant)

PV operation daily question - black and white chess question (variant)

2022-06-10 16:10:00 liangsena


One 、 Problem description

There is a box with an equal number of black and white chessmen various 100 gold , Now use the automatic sorting system to separate the black and white pieces , There are two processes in the system P1 and P2.P1 Responsible for sorting white chess pieces ,P2 Is responsible for sorting the black chess pieces , The two must be sorted alternately , Press Black first white then In sequence , And it shall not be stopped before the end of sorting . use PV Operation solves the problem .


Two 、 problem solving

semaphore flag1=1;
semaphore flag2=0;
int blackNum=100;
int whiteNum=100;

P2()
{
    
    while(1)
    {
    
        P(flag2);
        P(blackNum);
         Sort a black piece ;
        V(flag1);           // Signal to P1
    }
}
P1()
{
    
    while(1)
    {
    
        P(flag1);
        P(whiteNum);
         Sort a white piece ;
        V(flag2);           // Signal to P2
    }
}

3、 ... and 、 twitter

Not much. , Only the execution order and the number of pieces are given , Slip away ~

原网站

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

随机推荐