我试图允许其他用户的程序修改我的信号量(基本上删除它),但它失败了。
以下是我的信号量创建命令:
if ((semID = semget(key, 2, IPC_CREAT | 0777)) == -1) {
perror("Sem Creation:");
exit(2);
}
其他用户运行的程序仍然无法删除信号量错误“非所有者”
答案 0 :(得分:0)
here is what you need to know about semaphore creation/removal
Notice that a named semaphore is created using sem_open() NOT semget()
POSIX semaphores come in two forms: named semaphores and unnamed
semaphores.
Named semaphores
A named semaphore is identified by a name of the form
/somename; that is, a null-terminated string of up to
NAME_MAX-4 (i.e., 251) characters consisting of an initial
slash, followed by one or more characters, none of which are
slashes. Two processes can operate on the same named
semaphore by passing the same name to sem_open(3).
The sem_open(3) function creates a new named semaphore or
opens an existing named semaphore. After the semaphore has
been opened, it can be operated on using sem_post(3) and
sem_wait(3). When a process has finished using the semaphore,
it can use sem_close(3) to close the semaphore. When all
processes have finished using the semaphore, it can be removed
from the system using sem_unlink(3).
答案 1 :(得分:0)
IPC_RMID立即删除信号量集,唤醒在set上的semop(2)调用中阻塞的所有进程(返回错误并将errno设置为EIDRM)。调用进程的有效用户ID必须与信号量集的创建者或所有者匹配,否则调用者必须具有特权。
因此,尝试删除信号量的过程必须具有所有者或创建者的用户ID,或者是超级用户。
修改:您可以使用semctl()
和IPC_SET
选项设置信号量uid
结构的ipc_perm
字段(所有者ID)。这样您就可以将所有权转移到另一个用户ID(创建者ID不会更改)。请参阅我链接到的手册页,并阅读IPC_SET
。