Re: Using POSIX Named Semaphore After Fork
Re: Using POSIX Named Semaphore After Fork
- Subject: Re: Using POSIX Named Semaphore After Fork
- From: Steve Checkoway <email@hidden>
- Date: Tue, 27 Oct 2009 00:23:06 -0700
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
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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