0

Obviously O_CREAT and O_EXCL are not required when opening an existing semaphore.

O_CREAT is required when creating a new semaphore.

O_EXCL is only meaningful when OR-ing with O_CREAT, specifying that if a semaphore with the given name already exists, then an error is returned.

Linux manual page for sem_open said that

Definitions of the flags values can be obtained by including <fcntl.h>

but I did not find any flag in fcntl.h that told me how to open an existing semaphore.

1 Answer 1

3

Consider the following example:

#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    const char* const sem_name = "lock.sem";

    if (argc == 1) {
        sem_t* const sem = sem_open(sem_name, O_CREAT, 0644, 0);

        if (sem == NULL) {
            perror("sem_open");
            return 1;
        }

        sem_wait(sem); // Will block
        sem_close(sem);

        sem_unlink(sem_name);
    } else {
        sem_t* const sem = sem_open(sem_name, 0);

        if (sem == NULL) {
            perror("sem_open");
            return 1;
        }

        sem_post(sem); // Will unblock the other process
        sem_close(sem);
    }

    return 0;
}

I'm using the argument count to control the behavior of the program.

If I supply no parameters (i.e., when argc == 1), then the program will open the semaphore, creating it if it does not already exist; it initializes the value of the semaphore to 0. It then does a sem_wait() on the sem object. Since the semaphore was initialized to 0, this causes the process to block.

Now if I run a second instance of this same program, but this time with any non-zero number of arguments (i.e., when argc != 1), then the program will open the semaphore, but will not create it if it does not already exist. Note here that I pass 0 for the oflag parameter (i.e., I'm not passing any flags). The program then does a sem_post(), incrementing the semaphore from 0 to 1. This unblocks the first process. Both processes will close their references to the semaphore, and terminate.

If I understand your question correctly, the second case is what you're looking for.

If I try to run the second case first (i.e., when there isn't a running instance of the first case), then I get:

$ ./a.out foo
sem_open: No such file or directory

That's comming from the call to perror() because a semaphore with the given name does not exist.

2
  • Manual page does not mention the situation where oflag is 0. Can you tell me where to find it?
    – Andy Lin
    Commented May 29, 2021 at 18:23
  • 1
    @AndyLin Flags are bit fields (i.e., each bit in some integral type represents one possible option). 0 is the absence of any flag. Commented May 29, 2021 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .