Search code examples
multithreadingsemaphorepseudocode

Concurrent programming, how do i construct a semaphore?


i have a project where i have to write pseudocode for a semaphore, from the following text:

"Consider a scenario where a single taxi is taking Manchester United and Liverpool supporters from the city centre to the Saturday football game. The taxi can take four supporters at a time and it is always filled to capacity when carrying supporters. However the situation is never allowed to arise where the taxi contains one supporter of either team isolated on their own. The taxi carries out a number of trips and when it first arrives it randomly signals to one of the two waiting supporters’ queues. You can assume that there are always one or more supporters waiting in each queue when it is signaled. When a supporter enters the taxi and there is still some room for more supporters in the taxi the supporter (who has just joined the taxi) signal to another waiting supporter in one of the queues for them to also enter the taxi. Where possible the supporter will randomly select the queue to signal, however if necessary they will select a particular queue so as to ensure that no single supporter will be isolated in the taxi with opposing supporters.

The last supporter entering the taxi to complete a full cohort of four supporters in the taxi signals the taxi (driver) to take them to the football ground. The taxi takes them to their destination and then returns to repeat the cycle. Note that only the supporters (not the taxi driver) are able to differentiate between who is a Manchester United supporter and who is a Liverpool supporter."

I'm having trouble applying what notes i have, and what help there is online to this scenario.

Heres what ive done so far:

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

Solution

  • Here is what I could make-

    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