sem_wait && child process ?

Does it make sens to use a call like sem_wait() in a child process ?

Basically, I wrote a very basic code where in a main() function I first
create a semaphore (I use a non null value as second parameter) and then
fork a child process. The point is that when, in the chile process, I make a
call to sem_wait(&gSem) I get an error : Invalid parameter. &gSem represents
a pointer to a sem_t. It is a global variable in my code.

What is really weird is the fact that I can make the same kind of call (I
mean sem_wait(&gSem)) in the parent process and then everything works fine
then. I have no error on sem_wait nor on sem_post. It seems I’m missing one
point when I try to use the sem_wait function in the child process.

Is it legal to use sem_wait in a child process ? Any idea of what can be
wrong ? Many thanks for your support.


Philippe
Feel free to visit : www.baucour.com

Philippe BAUCOUR <philippe.baucour@wanadoo.fr> wrote:

Does it make sens to use a call like sem_wait() in a child process ?

Yes, but…

Basically, I wrote a very basic code where in a main() function I first
create a semaphore (I use a non null value as second parameter) and then
fork a child process. The point is that when, in the chile process, I make a
call to sem_wait(&gSem) I get an error : Invalid parameter. &gSem represents
a pointer to a sem_t. It is a global variable in my code.

What is really weird is the fact that I can make the same kind of call (I
mean sem_wait(&gSem)) in the parent process and then everything works fine
then. I have no error on sem_wait nor on sem_post. It seems I’m missing one
point when I try to use the sem_wait function in the child process.

Note that the child gets a COPY of the parents data segment… which
means it is doing a sem_wait() on a different (un-initialized) semaphore
from the semaphore in the parent.

Is it legal to use sem_wait in a child process ? Any idea of what can be
wrong ? Many thanks for your support.

As someone else noted, if you need more than one process to share a
semaphore, the semaphore must be in a shared memory area that both
processes can view – this includes the parent-forked child case.

-David

QNX Training Services
I do not answer technical questions by email.

“Philippe BAUCOUR” <philippe.baucour@wanadoo.fr> wrote in message
news:9renco$atv$1@inn.qnx.com

Basically, I wrote a very basic code where in a main() function I first
create a semaphore (I use a non null value as second parameter) and then
fork a child process. The point is that when, in the chile process, I make
a
call to sem_wait(&gSem) I get an error : Invalid parameter. &gSem
represents
a pointer to a sem_t. It is a global variable in my code.

You should keep you semaphore in a shared memory area so each process can
access the same semaphore.

-Adam