2014-05-04 66 views
-1

我遇到的问题是在代码部分运行之前信号量不会彼此等待。输出看起来像:信号量不会彼此等待

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 
+0

TL; DR:您知道您必须按顺序设置锁定/解锁尝试,以避免死锁? –

+0

@πάνταῥεῖ我认为这是sem_wait()和sem_post()的目的解锁它 –

+0

你可能会考虑使用['lock_guard'](http://en.cppreference.com/w/cpp/thread/lock_guard)机制来包装这些调用。 –

回答

0

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

mem_mutexCheckout1Count = (sem_t*) mmap(NULL, sizeof(mem_mutexCheckout1Count), PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 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); 
} 
+0

嘿,我改变它,但是,现在每个人都通过checkout1处理。它看起来像pmem正在增加。 –