SEM_INIT(3) BSD Library Functions Manual SEM_INIT(3)NAME
sem_init -- initialize an unnamed semaphore
Standard C Library (libc, -lc)
sem_init(sem_t *sem, int pshared, unsigned int value);
The sem_init() function initializes the unnamed semaphore pointed to by sem to have the value value.
A non-zero value for pshared specifies a shared semaphore that can be used by multiple processes, the semaphore should be located in shared
memory region (see mmap(2), shm_open(2), and shmget(2)), any process having read and write access to address sem can perform semaphore opera-
tions on sem.
Following a successful call to sem_init(), sem can be used as an argument in subsequent calls to sem_wait(3), sem_trywait(3), sem_post(3),
and sem_destroy(3). The sem argument is no longer valid after a successful call to sem_destroy(3).
The sem_init() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indi-
cate the error.
The sem_init() function will fail if:
[EINVAL] The value argument exceeds SEM_VALUE_MAX.
[ENOSPC] Memory allocation error.
SEE ALSO sem_destroy(3), sem_getvalue(3), sem_post(3), sem_trywait(3), sem_wait(3)STANDARDS
The sem_init() function conforms to ISO/IEC 9945-1:1996 (``POSIX.1'').
BSD January 9, 2010 BSD
Check Out this Related Man Page
SEM_INIT(3) Linux Programmer's Manual SEM_INIT(3)NAME
sem_init - initialize an unnamed semaphore
int sem_init(sem_t *sem, int pshared, unsigned int value);
Link with -pthread.
sem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initial value for the sem-
The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes.
If pshared has the value 0, then the semaphore is shared between the threads of a process, and should be located at some address that is
visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap).
If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3),
mmap(2), and shmget(2)). (Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.) Any
process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), etc.
Initializing a semaphore that has already been initialized results in undefined behavior.
sem_init() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error.
EINVAL value exceeds SEM_VALUE_MAX.
ENOSYS pshared is nonzero, but the system does not support process-shared semaphores (see sem_overview(7)).
Bizarrely, POSIX.1-2001 does not specify the value that should be returned by a successful call to sem_init(). POSIX.1-2008 rectifies
this, specifying the zero return on success.
SEE ALSO sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7)COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2012-05-13 SEM_INIT(3)
I'm running a COBOL process (with subroutines in standard C accessing an oracle database using OCI calls, using shared memories and sem etc....) on an AIX 5.1 machine.
Under unusually heavy conditions (millions of loops in the process) I've got the following error:
Execution error : file... (0 Replies)
:confused: This is already my n-th attempt to write this program. Some help would be appreciated. I have created some children processes, a couple of them writes to a shared memory (array of ints, where the first element is the "counter" of all elements in the array). The other child deletes an... (1 Reply)
I am using semaphores in my program, but when I compile the program, it gives the following error
$ gcc sem.c -o sem -lpthread
Undefined first referenced
symbol in file
sem_init ... (1 Reply)
The problem is to write logs in a shared file from several processes.
The cooperate call of fprintf() leads to messing the content even in one call
fprintf(f,"a1 \n a2");
fprintf(f,"a3 \n a4");
Ofcourse this is possible to implement the file as a... (9 Replies)
I have some files like file, file.chk, file.sem and file.temp in huge. I would like to delete some files based on following criteria.
1. Unconditionally delete .sem and .temp files
2. If we found the actual file, don't remove .chk file, otherwise remove .chk file as well
for... (5 Replies)