在C中初始化信号量

时间:2015-04-26 18:04:34

标签: c posix

我正在尝试创建一个泛型函数来在C中创建一个信号量,但我不确定第三个参数的值,名为mode_t mode。我读取读/写是标准权限,我想继续。我需要使用什么价值?

这是我的实施:

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;

}

1 个答案:

答案 0 :(得分:1)

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