SEM_INIT(3POSIX)

🌸 SEM_INIT πŸ’…

okay bestie, so this one's from the POSIX Programmer's Manual β€” that's the whole manual, and it's SEM_INIT(3POSIX) in case you were wondering what to call it when you cite it in your essay. no cap.


✨ PROLOG ✨

real talk before we even start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so if you're on Linux, go consult the corresponding Linux manual page for the details of Linux behavior. or like, the interface might not even be implemented on Linux at all. πŸ’€ so check first, queen.


πŸŽ€ NAME πŸŽ€

sem_init β€” initialize an unnamed semaphore


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned value);

πŸ’– DESCRIPTION πŸ’–

so the sem_init() function shall initialize the unnamed semaphore referred to by sem. and the value of the initialized semaphore shall be value. like, that's the starting count, bestie.

following a successful call to sem_init(), the semaphore may be used in subsequent calls to sem_wait(), sem_timedwait(), sem_trywait(), sem_post(), and sem_destroy(). and this semaphore shall remain usable until the semaphore is destroyed. so it's a whole journey, not just a one-time thing. πŸ’…

if the pshared argument has a non-zero value, then the semaphore is shared between processes. in this case, any process that can access the semaphore sem can use sem for performing sem_wait(), sem_timedwait(), sem_trywait(), sem_post(), and sem_destroy() operations. it's giving community semaphore. 🌈

if the pshared argument is zero, then the semaphore is shared between threads of the process. any thread in this process can use sem for performing sem_wait(), sem_timedwait(), sem_trywait(), sem_post(), and sem_destroy() operations. tight-knit little thread family. 🫢

see Section 2.9.9, Synchronization Object Copies and Alternative Mappings for further requirements β€” yes that's a real section, go read it, i'm not doing your homework for you.

⚠️ okay this part is IMPORTANT bestie: attempting to initialize an already initialized semaphore results in undefined behavior. undefined. behavior. do not do it. i am begging. πŸ™ˆ

πŸ”₯ RETURN VALUE πŸ”₯

upon successful completion, the sem_init() function shall initialize the semaphore in sem and return 0. otherwise, it shall return -1 and set errno to indicate the error. classic.


πŸ‘‘ ERRORS πŸ‘‘

the sem_init() function shall fail if:

  • EINVAL β€” the value argument exceeds {SEM_VALUE_MAX}. too much! dial it back!
  • ENOSPC β€” a resource required to initialize the semaphore has been exhausted, or the limit on semaphores ({SEM_NSEMS_MAX}) has been reached. you ran out, it's giving packed house 😭
  • EPERM β€” the process lacks appropriate privileges to initialize the semaphore. no permission, no semaphore, sorry babe.

the following sections are informative.


🌸 EXAMPLES 🌸

None.

(i know. i know. nothing. we move. πŸ’€)


πŸ’œ APPLICATION USAGE πŸ’œ

None.


⭐ RATIONALE ⭐

None.


🌈 FUTURE DIRECTIONS 🌈

None.


πŸŽ€ SEE ALSO πŸŽ€

  • sem_destroy()
  • sem_post()
  • sem_timedwait()
  • sem_trywait()

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 .


and that's the whole page, bestie!! this little masterpiece was served up by IEEE/The Open Group, dated 2017, and it lives at SEM_INIT(3POSIX) in the POSIX Programmer's Manual. go forth and initialize responsibly. periodt. πŸ’…βœ¨