user1305850
user1305850

Reputation: 141

Semaphore permission denied

I'm trying to create a simple semaphore that can be shared between processes. My main program calls the following function to create a semaphore.

#define SHAREDSEM "/sharedSem"
sem_t *sharedSem = sem_open(SHAREDSEM, O_CREAT, 0644, 1); 

However, I am getting the error "Permission Denied". I am running this code on Mac OS X, and I understand that it doesn't support unnamed semaphores. But I'm pretty sure sem_open is for named semaphores.

I've tried using different t_mode with no success.

Has anyone else ran into this issue and can help?

Upvotes: 1

Views: 11975

Answers (2)

anyone
anyone

Reputation: 1

I had this problem recently with OS X. The solution I found was to unlink the shared memory then recreate it again. You could also try rebooting since at least for Linux the POSIX style semaphores are kernel persistent. POSIX named semaphores have kernel persistence: if not removed by sem_unlink(3), a semaphore will exist until the system is shut down. The problem happens when you try to create the shared memory with giving improper permissions for the mode, or perhaps omitting the mode altogether. When you then correct the problem and try to open the semaphore it is still using the original semaphore that was persisted.

==> it has nothing to do with permission.. if you not specify mode the same user in other process cannot access to semaphore!!

specify mode:

sem_t *sharedSem = sem_open(SHAREDSEM, O_RDWR | O_CREAT , 0777, 1); 

Upvotes: 0

Urgo
Urgo

Reputation: 710

I had this problem recently with OS X. The solution I found was to unlink the shared memory then recreate it again. You could also try rebooting since at least for Linux the POSIX style semaphores are kernel persistent.

POSIX named semaphores have kernel persistence: if not removed by sem_unlink(3), a semaphore will exist until the system is shut down.

The problem happens when you try to create the shared memory with giving improper permissions for the mode, or perhaps omitting the mode altogether. When you then correct the problem and try to open the semaphore it is still using the original semaphore that was persisted.

So try doing:

#define SHAREDSEM "/sharedSem"
sem_unlink(SHAREDSEM);
sem_t *sharedSem = sem_open(SHAREDSEM, O_CREAT, 0777, 1); 

If this is the only place that you desire to create the semaphore then you can optionally add in the O_EXCL option which makes it fail if the semaphore already exists. This may be useful at least for debugging to see if this is the problem you are facing.

To do this try:

#define SHAREDSEM "/sharedSem"
sem_unlink(SHAREDSEM);
sem_t *sharedSem = sem_open(SHAREDSEM, O_CREAT | O_EXCL, 0777, 1); 

Note that in these examples I set the permissions to 0777 so that it is accessible by all. This is useful for debugging. In your final implementation remember to set it back to the proper permissions that you need.

Upvotes: 5

Related Questions