SEM_CLOSE(3P)

๐Ÿ’– SEM_CLOSE(3P) โ€” ok bestie, let's close this semaphore ๐Ÿ’–

POSIX Programmer's Manual energy, coming right at you ๐Ÿซถ

๐Ÿšจ PROLOG (read this first, fr)

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux at all ๐Ÿ˜ญ. So likeโ€ฆ vibe check your platform first.


โœจ NAME โœจ

sem_close โ€” close a named semaphore ๐ŸŽ€


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <semaphore.h>

int sem_close(sem_t *sem);

๐Ÿ’… DESCRIPTION ๐Ÿ’…

okay so sem_close() is basically you telling the system "hey bestie, this process is DONE with this named semaphore" โ€” the one pointed at by sem. That's the whole vibe. You're done, you're logging off, you're closing the tab ๐Ÿฆ‹.

Now the important tea โ˜•:

  • If you call sem_close() on an unnamed semaphore (aka one that was created by sem_init()), the effects are undefined. Not "probably fine", not "eh whatever" โ€” undefined. Don't do it ๐Ÿ’€.
  • sem_close() shall deallocate any system resources that the system allocated for this process's use of this semaphore โ€” like, "make available for reuse by a subsequent sem_open() by this process" level of deallocate ๐Ÿ”ฅ.
  • The effect of this process using the semaphore pointed to by sem after that is undefined. Use-after-close is not the move, periodt.
  • If any threads in the calling process are currently blocked on the semaphore, the behavior is undefined. Justโ€ฆ don't ๐Ÿ˜ญ.
  • If the semaphore has not been removed with a successful call to sem_unlink(), then sem_close() has no effect on the state of the semaphore. Closing โ‰  deleting, iykyk.
  • But! If sem_unlink() has been successfully invoked for name after the most recent call to sem_open() with O_CREAT for this semaphore, then once all processes that have opened the semaphore close it, the semaphore is no longer accessible โœจ. Last one out turns off the lights ๐Ÿ‘‘

๐Ÿซถ RETURN VALUE ๐Ÿซถ

  • Slayed successfully? You get a zero back ๐Ÿ’š.
  • Otherwise you get -1, and errno is set to tell you what went wrong. Rude but informative.

๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

The sem_close() function may fail if:

  • EINVAL โ€” the sem argument is not a valid semaphore descriptor. She said "who is that? never met her" ๐Ÿ™ˆ.
The following sections are informative. (Translation: the real spec is above, this is the bonus content ๐Ÿ’–)

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

None. Literally none. Nothing. Empty. A vibe void ๐Ÿ•ณ๏ธโœจ


๐Ÿ“ APPLICATION USAGE ๐Ÿ“

None. Also nothing. We're keeping it mysterious ๐Ÿ’…


๐Ÿงš RATIONALE ๐Ÿงš

None. No thoughts, head empty, spec says nothing ๐ŸŒธ


โญ FUTURE DIRECTIONS โญ

None. The future is unwritten, bestie ๐Ÿ”ฎ


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

  • semctl()
  • semget()
  • semop()
  • sem_init()
  • sem_open()
  • sem_unlink()

Also the Base Definitions volume of POSIX.1โ€2017, <semaphore.h> ๐Ÿ“š


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


IEEE/The Open Group ยท 2017 ยท SEM_CLOSE(3P) โ€” ok that's the whole page bestie, go forth and close your semaphores responsibly ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹๐ŸŒธ๐Ÿ’…๐Ÿซถ

๐Ÿ“ฆ where this page comes from