简体   繁体   中英

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. Anything that happens before sem_wait on the child process will print out fine. However, the printf() after sem_wait will never print. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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