Re: Using POSIX Named Semaphore After Fork
Re: Using POSIX Named Semaphore After Fork
- Subject: Re: Using POSIX Named Semaphore After Fork
- From: Abdulla Kamar <email@hidden>
- Date: Tue, 27 Oct 2009 18:32:29 +1100
Oh sorry, I should have said something. It turned out to be an issue in my code, it worked fine under Linux but there was a race in Mac OS X.
On Tue, Oct 27, 2009 at 6:23 PM, Steve Checkoway
<email@hidden> wrote:
On Oct 25, 2009, at 8:11 PM, Abdulla Kamar wrote:
Is it possible to use a named semaphore after a fork without reopening it in
the child process? This doesn't seem to be working for me, but may be an
issue with something I'm doing.
I've never used named semaphores before, but the following seems to work for me. This seems like it'd be better on the darwin-dev list, though.
#include <stdio.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
sem_t *sem = sem_open( "foo", O_CREAT, 0666, 0 );
puts( "open" );
pid_t pid = fork();
int ret;
if( pid == -1 )
{
perror( "fork" );
return 1;
}
if( pid == 0)
{
puts( "child is posting" );
ret = sem_post( sem );
if( ret == -1 )
perror( "child sem_post" );
ret = sem_close( sem );
if( ret == -1 )
perror( "child sem_close" );
return 0;
}
puts( "parent is waiting" );
ret = sem_wait( sem );
if( ret == -1 )
perror( "parent sem_wait" );
else
puts( "parent finished waiting" );
pid = wait( &ret );
if( pid == -1 )
perror( "waitpid" );
ret = sem_unlink( "foo" );
if( ret == -1 )
perror( "sem_unlink" );
ret = sem_close( sem );
if( ret == -1 )
perror( "parent sem_close" );
return 0;
}
--
Steve Checkoway
"Anyone who says that the solution is to educate the users
hasn't ever met an actual user." -- Bruce Schneier
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
--
Thank you
Abdulla
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden