Operating System - HP-UX
1847873 Members
2879 Online
104021 Solutions
New Discussion

Re: sem_wait and forked processes

 
Frank Bell_1
New Member

sem_wait and forked processes

Do shared semaphores work in user programs under HPUX 11.00?

Scenario:
Daemon creates semaphore using sem_init(sem, 1, 1) and grabs it using sem_wait OK.
Forks a child.
Child waits on the semaphore (sem_wait) until parent says "ready to go".
Parent does some work and releases the semaphore (sem_post) to inform the child to continue (execv a program).
Under HPUX, the child never wakes up ... sits on the sem_wait indefinitely.

I suspect that the fork is creating a brand new private copy of the semaphore (whilst in the locked_by_parent state) and so is never being cleared (i.e., it is not really a shared semaphore after all). Any ideas please?
1 REPLY 1
Frank Bell_1
New Member

Re: sem_wait and forked processes

In case anyone else hits this problem, it appears to be solved by using mmap to explicitly allocate the memory for the semaphore as "shared" ...

sem_t *sem;
void *shmemblk;
...
shmemblk = mmap(NULL, sizeof(sem_t)
, PROT_READ | PROT_WRITE
, MAP_SHARED | MAP_ANONYMOUS
, -1, 0)
...
sem = (sem_t *) shmemblk;
...
sem_init(sem, 1, 1)
...

Many thanks to Andy Bennett (HP) for this information.