在 C 中初始化一个信号量
Initialising a semaphore in C
我正在尝试创建一个通用函数以在 C 中创建信号量,但我不确定名为 mode_t mode
的第三个参数的值。我读到 read/write 是标准权限,我想接受它。我需要使用的价值是多少?
这是我的实现:
sem_t * semaphore_create(char * name, int value){
sem_t *sem;
sem = sem_open(name, O_CREAT , **what to put here ?** , value);
if(sem == SEM_FAILED){
perror("Error semaphore_create!");
exit(-1);
}
return sem;
}
sem = sem_open(name, O_CREAT , S_IRUSR | S_IWUSR, value);
将授予读写权限。
您可以查看 open(2) 其他模式选项的手册:
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
我正在尝试创建一个通用函数以在 C 中创建信号量,但我不确定名为 mode_t mode
的第三个参数的值。我读到 read/write 是标准权限,我想接受它。我需要使用的价值是多少?
这是我的实现:
sem_t * semaphore_create(char * name, int value){
sem_t *sem;
sem = sem_open(name, O_CREAT , **what to put here ?** , value);
if(sem == SEM_FAILED){
perror("Error semaphore_create!");
exit(-1);
}
return sem;
}
sem = sem_open(name, O_CREAT , S_IRUSR | S_IWUSR, value);
将授予读写权限。
您可以查看 open(2) 其他模式选项的手册:
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