并发编程,我如何构造一个信号量?
Concurrent programming, how do i construct a semaphore?
我有一个项目,我必须根据以下文本为信号量编写伪代码:
"考虑这样一个场景,一辆出租车将曼联和利物浦的支持者从市中心带到周六的足球比赛。出租车一次可以搭载四名支持者,并且在搭载支持者时总是满载。然而,出租车载有任何一方的一名支持者被孤立的情况是绝对不允许发生的。出租车会进行多次旅行,当它第一次到达时,它会随机向两个等待支持者队列中的一个发出信号。你可以假设在发出信号时,每个队列中总是有一个或多个支持者在等待。当一名支持者进入出租车并且出租车上仍有更多支持者的空间时,支持者(刚刚加入出租车)向另一名等待者发出信号支持者在其中一个队列中让他们也进入出租车。在可能的情况下,支持者将随机 select 队列发出信号,但如有必要,他们将 select 特定队列以确保没有 si反对派支持者将被隔离在出租车里。
最后一名进入出租车的支持者完成了出租车中四名支持者的完整队列,向出租车 (driver) 发出信号,将他们带到足球场。出租车将他们带到目的地,然后 returns 重复这个循环。请注意,只有支持者(不是出租车 driver)才能区分谁是曼联支持者和利物浦支持者。"
我在应用我的笔记时遇到问题,也无法在线获得对这种情况的帮助。
这是我到目前为止所做的:
int numManInTaxi = 0; //current no. of ManU supporters in taxi
int numLivInTaxi = 0;
sem MaxUnitedFans = 4; // // max no. of supporters that can fit in taxi
sem MaxLivFans = 4;
sem MMutexSem = 1;
sem LMutexSem = 1;
CO
for (count = 1 to ManUSupporter){
ManUProcess[count];
//
for (count = 1 to LivSupporter){
LivProcess[count];
OC
} /*end main process
process ManUProcess [i = 1 to N]{
P(MMutexSem); // mutually exclusice access for United supporter
numManUInTaxi++;
if ((numManInTaxi+numLivInTaxi) < 4)
{
if (numManuInTaxi == 3) { // signal the Man queue
numManUInTaxi++;
} else if ((numManUInTaxi ==1) && (numLivInTaxi==2)){
numManUInTaxi++;
} else if ( (numManInTaxi == 2) &&(numLivInTaxi==1)) {
V(MMutexSem);
NumLivInTaxi++;}
//end ManU supporter semaphore
这是我能做的-
int numManInTaxi = 0; //current no. of ManU supporters in taxi
int numLivInTaxi = 0;
int seats=4; //no of seats available empty
sem taxi=1; //to wait for taxi to send signal
sem seats=0; //signal generated by taxi or passenger for next passenger
wait(taxi); //taxi is available
{
signal(seat); //taxi generates signal
while(wait(seats)) //check if there are seats available
{
int i=rand()%2; //random number 0 or 1 used to select between 2 queues
if(i==0)
numManInTaxi++; //passenger supports ManU
else
numLivInTaxi++; //passenger supports Liv
seats--;
if(seats>1) //passenger generates signal for next passenger to enter
signal(seat);
}
/*Now three seats are filled and we have one empty seat left which is to
be filled such that there is no lone supporter of a team in taxi*/
signal(seat); //signal for last seat
wait(seat); //last passenger recieves the signal
seats--;
if(numManInTaxi==1) //if only one supporter belongs to ManU
numManInTaxi++;
else
numManInTaxi++;
}
//taxi drops passengers
numManInTaxi=0;
numManInTaxi=0;
seats=4;
signal(taxi); //taxi is ready for next trip
我有一个项目,我必须根据以下文本为信号量编写伪代码:
"考虑这样一个场景,一辆出租车将曼联和利物浦的支持者从市中心带到周六的足球比赛。出租车一次可以搭载四名支持者,并且在搭载支持者时总是满载。然而,出租车载有任何一方的一名支持者被孤立的情况是绝对不允许发生的。出租车会进行多次旅行,当它第一次到达时,它会随机向两个等待支持者队列中的一个发出信号。你可以假设在发出信号时,每个队列中总是有一个或多个支持者在等待。当一名支持者进入出租车并且出租车上仍有更多支持者的空间时,支持者(刚刚加入出租车)向另一名等待者发出信号支持者在其中一个队列中让他们也进入出租车。在可能的情况下,支持者将随机 select 队列发出信号,但如有必要,他们将 select 特定队列以确保没有 si反对派支持者将被隔离在出租车里。
最后一名进入出租车的支持者完成了出租车中四名支持者的完整队列,向出租车 (driver) 发出信号,将他们带到足球场。出租车将他们带到目的地,然后 returns 重复这个循环。请注意,只有支持者(不是出租车 driver)才能区分谁是曼联支持者和利物浦支持者。"
我在应用我的笔记时遇到问题,也无法在线获得对这种情况的帮助。
这是我到目前为止所做的:
int numManInTaxi = 0; //current no. of ManU supporters in taxi
int numLivInTaxi = 0;
sem MaxUnitedFans = 4; // // max no. of supporters that can fit in taxi
sem MaxLivFans = 4;
sem MMutexSem = 1;
sem LMutexSem = 1;
CO
for (count = 1 to ManUSupporter){
ManUProcess[count];
//
for (count = 1 to LivSupporter){
LivProcess[count];
OC
} /*end main process
process ManUProcess [i = 1 to N]{
P(MMutexSem); // mutually exclusice access for United supporter
numManUInTaxi++;
if ((numManInTaxi+numLivInTaxi) < 4)
{
if (numManuInTaxi == 3) { // signal the Man queue
numManUInTaxi++;
} else if ((numManUInTaxi ==1) && (numLivInTaxi==2)){
numManUInTaxi++;
} else if ( (numManInTaxi == 2) &&(numLivInTaxi==1)) {
V(MMutexSem);
NumLivInTaxi++;}
//end ManU supporter semaphore
这是我能做的-
int numManInTaxi = 0; //current no. of ManU supporters in taxi
int numLivInTaxi = 0;
int seats=4; //no of seats available empty
sem taxi=1; //to wait for taxi to send signal
sem seats=0; //signal generated by taxi or passenger for next passenger
wait(taxi); //taxi is available
{
signal(seat); //taxi generates signal
while(wait(seats)) //check if there are seats available
{
int i=rand()%2; //random number 0 or 1 used to select between 2 queues
if(i==0)
numManInTaxi++; //passenger supports ManU
else
numLivInTaxi++; //passenger supports Liv
seats--;
if(seats>1) //passenger generates signal for next passenger to enter
signal(seat);
}
/*Now three seats are filled and we have one empty seat left which is to
be filled such that there is no lone supporter of a team in taxi*/
signal(seat); //signal for last seat
wait(seat); //last passenger recieves the signal
seats--;
if(numManInTaxi==1) //if only one supporter belongs to ManU
numManInTaxi++;
else
numManInTaxi++;
}
//taxi drops passengers
numManInTaxi=0;
numManInTaxi=0;
seats=4;
signal(taxi); //taxi is ready for next trip