| 1 |
'\" t |
| 2 |
.\" Copyright 2006, Michael Kerrisk <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH sem_init 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
sem_init \- initialize an unnamed semaphore |
| 10 |
.SH LIBRARY |
| 11 |
POSIX threads library |
| 12 |
.RI ( libpthread ,\~ \-lpthread ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <semaphore.h> |
| 16 |
.P |
| 17 |
.BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value ); |
| 18 |
.fi |
| 19 |
.SH DESCRIPTION |
| 20 |
.BR sem_init () |
| 21 |
initializes the unnamed semaphore at the address pointed to by |
| 22 |
.IR sem . |
| 23 |
The |
| 24 |
.I value |
| 25 |
argument specifies the initial value for the semaphore. |
| 26 |
.P |
| 27 |
The |
| 28 |
.I pshared |
| 29 |
argument indicates whether this semaphore is to be shared |
| 30 |
between the threads of a process, or between processes. |
| 31 |
.P |
| 32 |
If |
| 33 |
.I pshared |
| 34 |
has the value 0, |
| 35 |
then the semaphore is shared between the threads of a process, |
| 36 |
and should be located at some address that is visible to all threads |
| 37 |
(e.g., a global variable, or a variable allocated dynamically on |
| 38 |
the heap). |
| 39 |
.P |
| 40 |
If |
| 41 |
.I pshared |
| 42 |
is nonzero, then the semaphore is shared between processes, |
| 43 |
and should be located in a region of shared memory (see |
| 44 |
.BR shm_open (3), |
| 45 |
.BR mmap (2), |
| 46 |
and |
| 47 |
.BR shmget (2)). |
| 48 |
(Since a child created by |
| 49 |
.BR fork (2) |
| 50 |
inherits its parent's memory mappings, it can also access the semaphore.) |
| 51 |
Any process that can access the shared memory region |
| 52 |
can operate on the semaphore using |
| 53 |
.BR sem_post (3), |
| 54 |
.BR sem_wait (3), |
| 55 |
and so on. |
| 56 |
.P |
| 57 |
Initializing a semaphore that has already been initialized |
| 58 |
results in undefined behavior. |
| 59 |
.SH RETURN VALUE |
| 60 |
.BR sem_init () |
| 61 |
returns 0 on success; |
| 62 |
on error, \-1 is returned, and |
| 63 |
.I errno |
| 64 |
is set to indicate the error. |
| 65 |
.SH ERRORS |
| 66 |
.TP |
| 67 |
.B EINVAL |
| 68 |
.I value |
| 69 |
exceeds |
| 70 |
.BR SEM_VALUE_MAX . |
| 71 |
.TP |
| 72 |
.B ENOSYS |
| 73 |
.I pshared |
| 74 |
is nonzero, |
| 75 |
but the system does not support process-shared semaphores (see |
| 76 |
.BR sem_overview (7)). |
| 77 |
.SH ATTRIBUTES |
| 78 |
For an explanation of the terms used in this section, see |
| 79 |
.BR attributes (7). |
| 80 |
.TS |
| 81 |
allbox; |
| 82 |
lbx lb lb |
| 83 |
l l l. |
| 84 |
Interface Attribute Value |
| 85 |
T{ |
| 86 |
.na |
| 87 |
.nh |
| 88 |
.BR sem_init () |
| 89 |
T} Thread safety MT-Safe |
| 90 |
.TE |
| 91 |
.SH STANDARDS |
| 92 |
POSIX.1-2008. |
| 93 |
.SH HISTORY |
| 94 |
POSIX.1-2001. |
| 95 |
.P |
| 96 |
Bizarrely, POSIX.1-2001 does not specify the value that should |
| 97 |
be returned by a successful call to |
| 98 |
.BR sem_init (). |
| 99 |
POSIX.1-2008 rectifies this, specifying the zero return on success. |
| 100 |
.SH EXAMPLES |
| 101 |
See |
| 102 |
.BR shm_open (3) |
| 103 |
and |
| 104 |
.BR sem_wait (3). |
| 105 |
.SH SEE ALSO |
| 106 |
.BR sem_destroy (3), |
| 107 |
.BR sem_post (3), |
| 108 |
.BR sem_wait (3), |
| 109 |
.BR sem_overview (7) |