信号量创建失败:文件存在
semaphore creation failed: File exists
我是 c 的新手,在使用信号量时遇到了一些麻烦。
我试着像这样创建一个信号量:
sem_t *usedspace = sem_open(SEM_USEDSPACE, O_CREAT | O_EXCL, S_IRWXU, 0);
当我运行这个程序时,打印错误“信号量已用空间创建失败:文件存在”(errno)。
期待您的帮助。
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.
有可能当您第一次 运行 应用程序信号量创建成功时。然后你再次 运行 应用程序并且 O_EXCL
标记导致存在异常。
您需要进行清理。完成后释放信号量。
除此之外,
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.
解释是,你的注意力应该在什么地方。
如果你想打开信号量不管它是否存在,那么不要使用O_EXCL
。如果你想在完成后删除信号量,你可以使用semctl
函数或ipcrm
命令。
man page 表示,在 ERRORS
部分中,
Both O_CREAT and O_EXCL were specified in oflag, but a semaphore with this name already exists.
它还说,在 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.
充分描述了这意味着什么 here。
我是 c 的新手,在使用信号量时遇到了一些麻烦。
我试着像这样创建一个信号量:
sem_t *usedspace = sem_open(SEM_USEDSPACE, O_CREAT | O_EXCL, S_IRWXU, 0);
当我运行这个程序时,打印错误“信号量已用空间创建失败:文件存在”(errno)。
期待您的帮助。
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.
有可能当您第一次 运行 应用程序信号量创建成功时。然后你再次 运行 应用程序并且 O_EXCL
标记导致存在异常。
您需要进行清理。完成后释放信号量。
除此之外,
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.
解释是,你的注意力应该在什么地方。
如果你想打开信号量不管它是否存在,那么不要使用O_EXCL
。如果你想在完成后删除信号量,你可以使用semctl
函数或ipcrm
命令。
man page 表示,在 ERRORS
部分中,
Both O_CREAT and O_EXCL were specified in oflag, but a semaphore with this name already exists.
它还说,在 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.
充分描述了这意味着什么 here。