sem_open(3)

✨ sem_open(3) ✨

okay bestie, so you've found yourself in the Library Functions Manual section, and you're about to meet sem_open(3) β€” the girl who creates and opens named semaphores. she's lowkey the main character of the POSIX semaphore friend group. let's get into it 🫢


πŸ’– NAME πŸ’–

sem_open β€” initialize and open a named semaphore

πŸ“š LIBRARY πŸ“š

POSIX threads library (libpthread, -lpthread)

πŸŽ€ SYNOPSIS πŸŽ€

#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <semaphore.h>

sem_t *sem_open(const char *name, int oflag, ...
                /* mode_t mode, unsigned int value */ );

🌸 DESCRIPTION 🌸

sem_open() is giving two girlboss jobs in one: it creates a brand-new POSIX semaphore, or it opens one that already exists. the semaphore is identified by name. and if you wanna know all the juicy rules about how name gets constructed? that's a whole other episode β€” go read sem_overview(7), iykyk.

the oflag argument is the vibe-setter: it's the flags that control how the call behaves. (you get the definitions of the flag values by including <fcntl.h>, periodt.) so here's the tea:

  • If O_CREAT is specified in oflag, then the semaphore gets created if it doesn't already exist. ✨
  • The owner (user ID) of the semaphore is set to the effective user ID of the calling process.
  • The group ownership (group ID) is set to the effective group ID of the calling process.
  • If both O_CREAT and O_EXCL are specified in oflag, then an error gets returned if a semaphore with the given name already exists. no dupes allowed bestie, we don't do that here πŸ’…

now, if O_CREAT is specified in oflag, plot twist: you gotta supply two additional arguments. yes ma'am, two of them:

  • mode β€” specifies the permissions to be placed on the new semaphore, exactly as for open(2). (you can grab the symbolic definitions for the permissions bits by including <sys/stat.h>.) the permissions settings are masked against the process umask. and listen β€” both read and write permission should be granted to each class of user that will access the semaphore. everybody eats, that's the rule πŸ“
  • value β€” specifies the initial value for the new semaphore.
😭 and here's the part that gets people: if O_CREAT is specified, and a semaphore with the given name already exists, then mode and value are ignored. completely. she said "i already know her" and moved on.

🌈 RETURN VALUE 🌈

  • On success, sem_open() returns the address of the new semaphore β€” you hold onto this address and use it when you call other semaphore-related functions. it's like her number, keep it πŸ’œ
  • On error, sem_open() returns SEM_FAILED, with errno set to indicate the error.

πŸ’€ ERRORS πŸ’€

  • EACCES β€” the semaphore exists, but the caller does not have permission to open it. πŸ™ˆ
  • EEXIST β€” both O_CREAT and O_EXCL were specified in oflag, but a semaphore with this name already exists.
  • EINVAL β€” value was greater than SEM_VALUE_MAX.
  • EINVAL β€” name consists of just "/", followed by no other characters.
  • EMFILE β€” the per-process limit on the number of open file descriptors has been reached.
  • ENAMETOOLONG β€” name was too long.
  • ENFILE β€” the system-wide limit on the total number of open files has been reached.
  • ENOENT β€” the O_CREAT flag was not specified in oflag and no semaphore with this name exists; or, O_CREAT was specified, but name wasn't well formed.
  • ENOMEM β€” insufficient memory. 😭

πŸ‘‘ ATTRIBUTES πŸ‘‘

for an explanation of the terms used in this section, see attributes(7).

  • Interface: sem_open()
  • Attribute: Thread safety
  • Value: MT-Safe

so yeah, she's MT-Safe β€” multithread-safe, iconic behaviour, no cap πŸ”₯

⭐ STANDARDS ⭐

POSIX.1-2008.

πŸ¦‹ HISTORY πŸ¦‹

POSIX.1-2001.

🧚 SEE ALSO 🧚

sem_close(3), sem_getvalue(3), sem_post(3), sem_unlink(3), sem_wait(3), sem_overview(7)


and that's the whole page bestie, you ate that πŸ’…

sign-off time: this little love letter to semaphores comes from the Linux man-pages crew, version 6.20, dated 2026-02-08. go forth and semaphore responsibly πŸŽ€

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms