1SEM_INIT(P)                POSIX Programmer's Manual               SEM_INIT(P)
2
3
4

NAME

6       sem_init - initialize an unnamed semaphore (REALTIME)
7

SYNOPSIS

9       #include <semaphore.h>
10
11       int sem_init(sem_t *sem, int pshared, unsigned value);
12
13

DESCRIPTION

15       The sem_init() function shall initialize the unnamed semaphore referred
16       to by sem. The value of the initialized semaphore shall be value.  Fol‐
17       lowing  a  successful  call to sem_init(), the semaphore may be used in
18       subsequent  calls  to  sem_wait(),   sem_trywait(),   sem_post(),   and
19       sem_destroy().   This semaphore shall remain usable until the semaphore
20       is destroyed.
21
22       If the pshared argument has a non-zero value,  then  the  semaphore  is
23       shared between processes; in this case, any process that can access the
24       semaphore sem can use sem  for  performing  sem_wait(),  sem_trywait(),
25       sem_post(), and sem_destroy() operations.
26
27       Only sem itself may be used for performing synchronization.  The result
28       of referring to copies of sem in calls  to  sem_wait(),  sem_trywait(),
29       sem_post(), and sem_destroy() is undefined.
30
31       If  the  pshared argument is zero, then the semaphore is shared between
32       threads of the process; any thread in this process can use sem for per‐
33       forming sem_wait(), sem_trywait(), sem_post(), and sem_destroy() opera‐
34       tions. The use of the semaphore by threads other than those created  in
35       the same process is undefined.
36
37       Attempting  to  initialize  an already initialized semaphore results in
38       undefined behavior.
39

RETURN VALUE

41       Upon successful completion, the sem_init()  function  shall  initialize
42       the  semaphore  in  sem. Otherwise, it shall return -1 and set errno to
43       indicate the error.
44

ERRORS

46       The sem_init() function shall fail if:
47
48       EINVAL The value argument exceeds {SEM_VALUE_MAX}.
49
50       ENOSPC A  resource  required  to  initialize  the  semaphore  has  been
51              exhausted,  or  the  limit  on semaphores ( {SEM_NSEMS_MAX}) has
52              been reached.
53
54       EPERM  The process lacks the appropriate privileges to  initialize  the
55              semaphore.
56
57
58       The following sections are informative.
59

EXAMPLES

61       None.
62

APPLICATION USAGE

64       The  sem_init()  function is part of the Semaphores option and need not
65       be available on all implementations.
66

RATIONALE

68       Although this volume of IEEE Std 1003.1-2001 fails to  specify  a  suc‐
69       cessful return value, it is likely that a later version may require the
70       implementation to return a value of zero if the call to  sem_init()  is
71       successful.
72

FUTURE DIRECTIONS

74       None.
75

SEE ALSO

77       sem_destroy()   ,   sem_post()  ,  sem_timedwait()  ,  sem_trywait()  ,
78       sem_wait() , the Base Definitions volume of IEEE Std 1003.1-2001, <sem‐
79       aphore.h>
80
82       Portions  of  this text are reprinted and reproduced in electronic form
83       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
84       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
85       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
86       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
87       event of any discrepancy between this version and the original IEEE and
88       The  Open Group Standard, the original IEEE and The Open Group Standard
89       is the referee document. The original Standard can be  obtained  online
90       at http://www.opengroup.org/unix/online.html .
91
92
93
94IEEE/The Open Group                  2003                          SEM_INIT(P)
Impressum