Reputation: 13
I am new to c and having some troubles with semaphores.
I try to create a semaphore like this:
sem_t *usedspace = sem_open(SEM_USEDSPACE, O_CREAT | O_EXCL, S_IRWXU, 0);
When I run this program the error "semaphore usedspace creation failed: File exists" is printed (errno).
I am looking forward for your help.
Upvotes: 0
Views: 2369
Reputation: 3190
If you want to open the semaphore regardless of whether or not it exists, then don't use O_EXCL
. If you want to remove the semaphore after you're done, you can use the semctl
function or the ipcrm
command.
The man page says, within the ERRORS
section,
Both O_CREAT and O_EXCL were specified in oflag, but a semaphore with this name already exists.
It also says, in the second paragraph of the DESCRIPTION
If both O_CREAT and O_EXCL are specified in oflag, then an error is returned if a semaphore with the given name already exists.
What this means is described adequately here.
Upvotes: 0
Reputation: 663
The documentation says:
O_CREAT
This flag is used to create a semaphore if it does not already exist. If O_CREAT is set and the semaphore already exists, then O_CREAT has no effect, except as noted under O_EXCL. Otherwise, sem_open() creates a named semaphore. The O_CREAT flag requires a third and a fourth argument: mode, which is of type mode_t, and value, which is of type unsigned. The semaphore is created with an initial value of value. Valid initial values for semaphores are less than or equal to {SEM_VALUE_MAX}.
The user ID of the semaphore shall be set to the effective user ID of the process. The group ID of the semaphore shall be set to the effective group ID of the process; however, if the name argument is visible in the file system, the group ID may be set to the group ID of the containing directory. The permission bits of the semaphore are set to the value of the mode argument except those set in the file mode creation mask of the process. When bits in mode other than the file permission bits are specified, the effect is unspecified.
After the semaphore named name has been created by sem_open() with the O_CREAT flag, other processes can connect to the semaphore by calling sem_open() with the same value of name.
O_EXCL
If O_EXCL and O_CREAT are set, sem_open() fails if the semaphore name exists. The check for the existence of the semaphore and the creation of the semaphore if it does not exist are atomic with respect to other processes executing sem_open() with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, the effect is undefined.
If flags other than O_CREAT and O_EXCL are specified in the oflag parameter, the effect is unspecified.
It is possible that when you first run the application semaphore creation was successful. Then you run the app again and O_EXCL
flag caused the existance exception.
You need to do the cleanup. Release the semaphore when you're done with it.
Other than that,
This flag is used to create a semaphore if it does not already exist. If O_CREAT is set and the semaphore already exists, then O_CREAT has no effect, except as noted under O_EXCL. Otherwise, sem_open() creates a named semaphore.
explanation is, what your attention should at be.
Upvotes: 0