From semaphores: sem_wait throws an inexplicable error

I am working on a problem that we must use semaphores to solve. I have an array that contains two semaphores, gsemand, under certain conditions, causes a call sem_wait(&(gsem[me]))that must wait until this particular process wakes up. However, for some reason it gives me an error Bad file descriptor. I looked sem_wait, and the Open Group specification says that this is not a mistake sem_wait. This makes my entire program crazy, and I have no idea why this is failing.

EDIT: abusive code as requested.

120     sem_wait(&mutex);
121     if (inside[opp] > 0 || waiting[opp] > 0) {
122         sem_wait(&screen);
123         printf("%s %u waiting\n", names[me], t);
124         sem_post(&screen);
125         waiting[me]++;
126         sem_post(&mutex);
127         int hg = sem_wait(&(gsem[me]));
128         if (hg < 0)
129             printf("%s\n", strerror(errno));
130     } 

I should note that this is homework for which we must use semaphores. The professor calls it the "unisex bathroom." Only men and women can use it, but not at the same time. inside[opp]is the number of people of the opposite sex in the bathroom. waiting[opp]- number of the opposite sex, awaiting its use. screenis a semaphore that blocks access to stdout. The solution is based on the solution to the problem of readers / writers given in our textbook, which uses relay transmission.

I should also note that we first needed to write a solution in Ada, and then convert it to C. My Ada solution, and I translated it verbatim. I am sure this is a small syntax detail. Finally, I'm working on Snow Leopard if that helps.

+1
1

, Single Unix Spec . , , , , , , , / .

, .

1/ , sem_wait -1? , errno , , , . , , errno EBADF sem_wait.

2/ , , ?

3/ ? , , me ?

, , .


, Google sem_wait ebadf, - . , errno .

errno, ( errno).

, , , , .


, .

4/ sem_init . , . 2008 , OSX - sem_init, sem_open (. ), sem_, ( sem_init ( ) , , ).

5/ nofollow noreferrer โ†’ errno OSX, __error pthread_self errno ( current_thread->errno ). , , .

+6

All Articles