laker001
laker001

Reputation: 417

Initialising a semaphore in C

I'm trying to create a generic function to create a semaphore in C, but i'm unsure about the value of the third argument, named mode_t mode. I read the read/write are the standard permissions, and i would like to go with that. What's the value i need to use ?

Here's my implementation:

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;

}

Upvotes: 0

Views: 1184

Answers (1)

P.P
P.P

Reputation: 121347

sem = sem_open(name, O_CREAT , S_IRUSR | S_IWUSR, value);

Will give read and write permissions.

You can see the open(2) manual for other mode options:

  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

Upvotes: 1

Related Questions