首页 > 解决方案 > 如何使用 sem_open 为不同的用户访问相同的信号

问题描述

我使用 sem_open() 和 root 用户创建了全局信号量:

sem_t* sem = sem_open("ghMutex", O_CREAT, 0644, 1);

所以当我这样做时: ls -al /dev/shm/

-rw-r--r-- 1 根根 32 Jan 23 00:23 sem.ghMutex

现在我在 Linux 中有一个不同的用户(比如 User1),它再次调用相同的函数 sem_open 来创建信号量:

sem_t* sem = sem_open("ghMutex", O_CREAT, 0644, 1);

在最后一个函数调用中,我收到错误 EACCES(权限被拒绝),因为此名称的信号量已使用 root 用户创建。

我的问题:

1)如何为不同的用户创建具有相同名称的信号量?

2) 在 sem_open() 中对信号量进行命名约定以便多个用户可以访问的最佳实践是什么?

标签: semaphore

解决方案


You are setting readonly permissions for the other user.

0644 means:

  • 6 (read and write) for the owner
  • 4 (readonly) for other group users
  • 4 (readonly) for anyone else

According to the man pages:

Both read and write permission should be granted to each class of user that will access the semaphore.


推荐阅读