简体   繁体   English

sem_wait无法继续

[英]sem_wait unable to continue

I have a process spawning off a few child processes. 我有一个产生几个子进程的进程。 They are all sharing one semaphore. 他们都共享一个信号量。

In my main program. 在我的主程序中。

sem_t *sharedSem = sem_open(SHAREDSEM, O_CREAT, 0600, 4); // Initial 1

In my child program. 在我的孩子程序中。

> sem_t * serverSem = sem_open(SHAREDSEM, 0);     
sem_wait(serverSem); 
printf("PRINTING\n");  
sem_post(sharedSem);

My observation is that the problem lies within sem_wait. 我的观察是问题出在sem_wait之内。 Anything that happens before sem_wait on the child process will print out fine. 在子进程上sem_wait之前发生的任何事情都可以正常打印。 However, the printf() after sem_wait will never print. 但是,sem_wait之后的printf()将永远不会打印。 Not even once. 一次也没有。

I've been at it for a few hours and I can't see my problem. 我已经呆了几个小时,看不到我的问题。 Can anyone lend a hand? 谁能伸出援手? Thanks. 谢谢。

I think that that behavior is caused by a missing sem_close() for the previous runnings. 我认为该行为是由先前运行时缺少sem_close()引起的。 Just assure that you correctly close the semaphore so that in future runs the starting value of the semaphore is correctly assigned and the sem_wait() should work. 只要确保您正确关闭了信号量,以便将来运行,就可以正确分配信号量的起始值,并且sem_wait()应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM