Question

The problem I'm having is that the semaphores are not waiting on each other before the portion of the code is running. The output looks like:

Customer 1 arriving at lane 1 at 0 sec
Customer 1 in now number 1 at lane 1
Checkout1 now serving customer 1 for 10 sec
Customer 2 arriving at lane 2 at 3
Customer 2 in now number 1 at lane 2
Checkout2 now serving customer 2 for 15sec
Customer 3 arriving at lane 1 at 7 sec
Customer 3 in now number 2 at lane 1
Checkout1 now serving customer 3 for 8 sec
Customer 4 arriving at lane 2 at 9
Customer 4 in now number 2 at lane 2
Checkout2 now serving customer 4 for 75sec
Cusomter 1 has left checkout1
Customer 5 arriving at lane 1 at 12 sec
Customer 5 in now number 2 at lane 1
Checkout1 now serving customer 5 for 20 sec
Cusomter 3 has left checkout1
Cusomter 2has left checkout2
Cusomter 5 has left checkout1
Cusomter 4has left checkout2

The problem is that when the checkout1 is processing customer1, the customer is supposed to leave before another person is processed, however, the checkout1 then services another customer which is customer 3. Then near the end of the program, the people start actually leaving the checkouts. I'm pretty sure this is a problem with my semaphores.

Here is a dumbed down version of my code:

sem_t *mem_mutexCheckout1Count;
sem_t *mem_mutexCheckout2Count;
sem_t *mem_mutexCheckout1Line;
sem_t *mem_mutexCheckout2Line;

int *pmemCheckout1Line;
int *pmemCheckout2Line;


int main()
{
   for(int i = 0; i < myCustomers.size(); i++)
   {
        totalArrivalTime += myCustomers[i].arrival;
        if((pid = fork()) == 0)
        {
           InLine(myCustomers[i].serial, totalArrivalTime, myCustomers[i].processing);
           _exit(0);
        }
   }
}
void InLine(int serial, int arrivalTime, int time_interval)
{
    sleep(arrivalTime);
    if(*pmemCheckout1Line <= *pmemCheckout2Line)
    {
        cout << "Customer " << serial << " arriving at lane 1 at " << arrivalTime << " sec" << endl;
        sem_wait(mem_mutexCheckout1Line);
        *pmemCheckout1Line += 1;
        sem_post(mem_mutexCheckout1Line);
        cout << "Customer " << serial << " in now number " << *pmemCheckout1Line << " at lane 1" << endl;

        sem_wait(mem_mutexCheckout1Count);
        cout << "Checkout1 now serving customer " << serial << " for " << time_interval << " sec" << endl;
        sleep(time_interval);

        *pmemCheckout1Line -= 1;
        cout << "Cusomter " << serial << " has left checkout1" << endl; 
        sem_post(mem_mutexCheckout1Count);
    }
    else
    {
        cout << "Customer " << serial << " arriving at lane 2 at " << arrivalTime << endl;
        sem_wait(mem_mutexCheckout2Line);
        *pmemCheckout2Line += 1;
        sem_post(mem_mutexCheckout2Line);
        cout << "Customer " << serial << " in now number " << *pmemCheckout2Line << " at lane 2" << endl;

        sem_wait(mem_mutexCheckout2Count);
        cout << "Checkout2 now serving customer " << serial << " for " << time_interval << "sec" << endl;
        sleep(time_interval);
        *pmemCheckout2Line -= 1;

        cout << "Cusomter " << serial << "has left checkout2" << endl; 
        sem_post(mem_mutexCheckout2Count);
    }
}

My myCustomers vector looks like

Vectorindex-Customerserial-timeElapsedSincePrevCustomer-ProcessTime
-------------
[0] 1  0  10
[1] 2  3  15
[2] 3  4  8
[3] 4  2  75
[4] 5  3  20
Was it helpful?

Solution 2

I found out my semaphores were not in shared memory, hence the semaphores not working properly. I did:

mem_mutexCheckout1Count = (sem_t*) mmap(NULL, sizeof(mem_mutexCheckout1Count), PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);

To all my mutex's to fix.

OTHER TIPS

If you want to prevent any other customer to be processed before the customer who is being processed on the moment leaves, only use one semaphore, which is locked when a customer is being processed and unlocked when the customer is leaving

if(*pmemCheckout1Line <= *pmemCheckout2Line)
{

    cout << "Customer " << serial << " arriving at lane 1 at " << arrivalTime << " sec" << endl;

    sem_wait(mem_mutexCheckout1Line);

    *pmemCheckout1Line += 1;

    cout << "Customer " << serial << " in now number " << *pmemCheckout1Line << " at lane 1" << endl;

    cout << "Checkout1 now serving customer " << serial << " for " << time_interval << " sec" << endl;
    sleep(time_interval);

    *pmemCheckout1Line -= 1;
    cout << "Cusomter " << serial << " has left checkout1" << endl;

    sem_post(mem_mutexCheckout1Line);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top