Home > Mobile >  What is the `0` equivalent oflag in <fcntl.h>?
What is the `0` equivalent oflag in <fcntl.h>?

Time:11-13

To open a semaphore, I've seen the following expression:

sem_t semaphore = sem_open("/sempath", 0);

The man page says that that integer at the end is the "oflag" and that I should read more about the oflags in fcntl.h but I can't figure out what oflag the number 0 get's mapped to.


What does the 0 mean in the code above? Is it O_RDWR?

More generally: What numbers do the flags in fcntl.h get mapped to and how can I find them?

CodePudding user response:

What does the 0 mean in the code above? Is it O_RDWR?

0 means no flags. The flags argument passed to sem_open and similar routines, such as open is the logical OR of single bits or bit-fields that are defined by the various symbols documented for these routines. When there are no flags, no bits are ORed into the argument, so its value should be the identity element of the OR operation, which is zero.

What numbers do the flags in fcntl.h get mapped to and how can I find them?

You can find them in fcntl.h or the files it includes. Or, more easily, you can write a program that prints them:

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>


#include <fcntl.h>


int main(void)
{
    printf("O_CREAT is 0x%" PRIxMAX ".\n", (uintmax_t) O_CREAT);
    printf("O_EXCL is 0x%" PRIxMAX ".\n", (uintmax_t) O_EXCL);
}

However, you should not rely on these symbols having particular values, especially across different POSIX implementations.

  •  Tags:  
  • c
  • Related