semaphore shmget()semflg = 384做什么?

时间:2016-04-01 17:21:46

标签: c semaphore

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

key_t  key    = IPC_PRIVATE;
int    id;
int    semflg = 384;

id = shmget( key, 8192, semflg );

有人能告诉我在semflg中有什么影响384的价值吗?

1 个答案:

答案 0 :(得分:1)

shmget 的联机帮助页说:

  

除上述标志外,最低有效9位          shmflg指定授予所有者,组和的权限          其他。这些位具有相同的格式,并且具有相同的含义          open(2)的mode参数。

让我们查看打开

的联机帮助页
The following symbolic constants are provided for mode:
- S_IRWXU  00700 user (file owner) has read, write, and execute permission
- S_IRUSR  00400 user has read permission
- S_IWUSR  00200 user has write permission
- S_IXUSR  00100 user has execute permission
- S_IRWXG  00070 group has read, write, and execute permission
- S_IRGRP  00040 group has read permission
- S_IWGRP  00020 group has write permission
- S_IXGRP  00010 group has execute permission
- S_IRWXO  00007 others have read, write, and execute permission
- S_IROTH  00004 others have read permission
- S_IWOTH  00002 others have write permission
- S_IXOTH  00001 others have execute permission

整数值 384 以二进制编码为 110000000 ,对应标志 600 ,即S_IRUSR|S_IWUSR

  • 用户具有写入权限
  • 用户已获得读取权限

无论是谁写了这段代码,他的生命都耗费了2分钟,浪费了更多其他人的生命,包括我在内。

祝贺。 :)

编辑:感谢@Fabio Turati指出了一个重大错误;)