Goktug
Goktug

Reputation: 923

semflg Value of semget Function

Generally, some constant flag values located in sys/types.h file are passed to semget() function as a third argument. However, a piece of code which is given to us for a project contains such a semget() function call :

sem_id = semget(SEMKEY, 1, 0) // accessing pre-defined semaphore

semid is a global variable, and SEMKEY is a constant.

What does it mean to pass zero to semflg value ? Can we say that when accessing a semaphore which was defined previously, we set semflg parameter to zero ?

Upvotes: 0

Views: 239

Answers (1)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36391

Not exactly because other flags may be set even if IPC_CREAT is not but then are of no use. POSIX just says (about opening):

A semaphore identifier with its associated semid_ds data structure and its associated set of nsems semaphores (see ) is created for key if one of the following is true:

  • The argument key is equal to IPC_PRIVATE.

  • The argument key does not already have a semaphore identifier associated with it and (semflg & IPC_CREAT) is non-zero.

So, of course, semflg=0 imply no creation. But no creation does not imply semflg=0.

Upvotes: 1

Related Questions