I’m experiencing a lot of difficulty getting Semaphores to work on a Linux based system in C.
The process of my application is such:
- Application starts
- Application forks into child/parent
- Each process uses
sem_openwith a common name to open the semaphore.
If I create the semaphore before forking, it works fine. However, requirements prevent me from doing so. When I try to call sem_open for the second time, I get a “Permission Denied” error (via errno).
Is it possible to do this in any way? Or is there any way to open the semaphore in one process and used a shared memory mechanism to share it with the child process?
Are you using the 4 parameter or 2 parameter version of sem_open?
Make sure to use the 4 parameter version and use a mode that will allow other processes to open the semaphore. Assuming all the processes are owned by the same user, a mode of 0600 (
S_IRUSR | S_IWUSR) will be sufficient.You may also want to verify that you umask is not masking out any of the necessary permissions.