信号量不在彼此等待

时间:2014-05-04 02:56:44

标签: c++ posix

我遇到的问题是,在代码部分运行之前,信号量不会相互等待。输出如下:

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

问题在于,当checkout1正在处理customer1时,客户应该在处理另一个人之前离开,但是,checkout1然后为另一个客户3提供服务​​。然后在程序结束时,人们开始实际上离开了结帐。我很确定这是我信号量的问题。

这是我的代码的一个愚蠢的版本:

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);
    }
}

我的myCustomers矢量看起来像

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

2 个答案:

答案 0 :(得分:0)

如果您希望在当前正在处理的客户离开之前阻止任何其他客户处理,则仅使用一个信号量,当客户正在处理时锁定该信号量并在客户离开时解锁

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);
}

答案 1 :(得分:0)

我发现我的信号量不在共享内存中,因此信号量不能正常工作。我做了:

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

要修复我的所有互斥锁。