Reputation: 10865
I have a problem with synchronization using posix semaphores. Here's the code I wrote
sem_t *sem1, *sem2;
void* test1() {
int rv;
while(1) {
rv = sem_wait(sem1);
printf("sem_wait(&sem1) = %d\n",rv);
printf("test1\n");
rv = sem_post(sem2);
printf("sem_post(&sem2) = %d\n\n",rv);
}
return NULL;
}
void* test2() {
while(1) {
rv = sem_wait(sem2);
printf("sem_wait(&sem2) = %d\n",rv);
printf("test2\n");
rv = sem_post(sem1);
printf("sem_post(&sem1) = %d\n\n",rv);
}
return NULL;
}
int main (int argc, const char * argv[]) {
pthread_t t1,t2;
sem1 = sem_open("sem1", O_CREAT | O_EXCL, 0644, 1);
if (sem1 == SEM_FAILED){
perror("sem1_init");
sem_close(sem1);
return;
}
sem2 = sem_open("sem2", O_CREAT | O_EXCL, 0644, 0);
if (sem2 == SEM_FAILED){
perror("sem2_init");
sem_close(sem1);
sem_close(sem2);
return;
}
pthread_create(&t1, NULL, &test1, NULL);
pthread_create(&t2, NULL, &test2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
What I expected is that, since I initialized sem1
to 1 and sem2
to 0, test1
would be the first function to run, and then they will alternate till the end of time.
Instead it's not working, I mean in the log I read many times "test1", many times "test2", for a while alternated and then again without any order.
Can someone tell me where is my error?
ps. don't know if it can be useful but I'm running MacOSX 10.6.7
EDIT:
I updated my code, removing all calls to sem_init
and sem_getvalue
(both not present under MacOS), using sem_open
to init the semaphores and it seems to work.
I found, however, a strange problem, probably because of my miscomprehension of sem_open
call: every time I restart the program if I reuse the same name for semaphores I get the error File exists
. How can I force to reuse the same identifier?
Besides. according to man pages, sem_wait
should return 0 if successful, -1 if not. What does it mean if I receive 1 (this happen always in test2
)?
Upvotes: 3
Views: 2504
Reputation: 305
If what you want is for the two functions to alternate, you should just use a single semaphore.
When one of your threads gets scheduled, for example t1, it locks sem1 and then keeps it and runs in a while loop until it gets interrupted. Because it's not doing a sem_post(&sem1), it doesn't give control to the other thread.
Update: Looking at this again, this code should work. I think the sem_init not being implemented on OSX is probably your issue.
Update: To answer your question about sem_wait returning 1, it looks like the real error value is returned in errno
. If the return value != 0
, then check there. My shot in the dark guess would be EINTR is what you are seeing.
Upvotes: 1