1. Sem_init name Sem_init-Initialize an unnamed semaphore
Overview #include <semaphore.h>
int Sem_init (sem_t *sem, int pshared, unsigned int value);
link library-pthread
Description Sem_inti () initializes the unnamed semaphore at the address specified by the SEM. The parameter value specifies the initial value of the semaphore.
The parameter pshared indicates whether the signal is shared online threads (under the same process) or between processes.
If the pshared value is 0, then the semaphore is shared online threads (under the same process), which should be on the address space visible to all threads (for example, global variables or dynamically requested on the heap).
If pshared is not 0, then the semaphore is shared between processes and should be placed in the shared memory area (see Shm_open (3), Nmap (2), and Shmget (2)). (because a child process created by fork (2) inherits the memory map of the parent process, it can also access the semaphore.) Any process that can access the shared memory area can manipulate the semaphore through functions such as sem_post (3), sem_wait (3), and so on.
Initializing a semaphore that has already been initialized will result in undefined behavior.
The return value Sem_init () returns 0 successfully, the failure returns 1, and the setting errno indicates the error.
Error EINVAL parameter VALUE exceeds Sem_value_max.
The Enosys parameter pshared is not 0, but the system does not support sharing semaphores between processes (see Sem_overview (7)).
Follow posix.1-2001
Note Oddly, posix.1-2001 does not specify the return value when Sem_init () is successfully called. posix.1-2008 corrected this point, indicating a successful return of 0.
See Sem_destroy (3), Sem_post (3), sem_wait (3), Sem_overview (7)
2. sem_wait name Sem_wait,sem_timedwait,sem_trywait-Lock signal Volume
Overview #include <semaphore.h>
int sem_wait (sem_t *sem);
int sem_trywait (sem_t *sem);
int sem_timedwait (sem_t *sem, const struct TIMESPEC *abs_timeout);
link library-pthread
GLIBC Requirements for functional test macros (see Feature_test_macros (7)):
Sem_timedwait (): _posix_c_source >= 200112L | | _xopen_source >= 600
Describes the amount of sem_wait () decrement (lock) that is directed by the SEM. If the value of the semaphore is greater than 0, then decrements, and the function returns immediately. If the current value of the semaphore is 0, the call is blocked until it can perform a decrement operation (that is, the semaphore value is greater than 0), or the signal handler interrupts the call.
Sem_trywait () is similar to sem_wait () except that Sem_trywati returns an error when the decrement operation cannot be performed immediately (errno is set to Eagain) instead of blocking.
Sem_timedwait () is similar to sem_wait () except that sem_timedwait specifies the maximum blocking time for this call by Abs_timeout when the decrement operation cannot be executed immediately. The Abs_timeout parameter points to an arrogant era, 1970-01-01 00:00:00 +0000 (UTC), the absolute time-out structure in seconds and nanoseconds. The structure is defined as follows:
STRUTCT Timespec {
time_t tv_sec; /* Seconds */
Long tv_nsec; /* nanoseconds [0.. 999999999] */
};
If the timeout expires within the function call time, the semaphore is not immediately locked, sem_timedwait () execution fails and a time-out error is returned (errno is set to Etimedout).
If the operation executes immediately, sem_timedwait () will never return a time-out error regardless of the Abs_timeout value. In addition, in this case, the validity of the abs_timedout is not detected.
The return value of all these functions returns 0 on success, and 1 when it fails, and the value of the semaphore remains constant and the errno is set to indicate an error.
Error eintr function call interrupted by interrupt handler; see Signal (7).
EINVAL parameter sem is not a valid semaphore.
The following additional errors appear in the sem_trywait () Call:
The Eagain cannot perform this operation under non-blocking (for example, the semaphore current value is 0).
The following additional errors appear in the sem_timedwait () Call:
The EINVAL parameter Abs_timeout.tv_nsecs value is less than 0, or greater than or equal to 1 billion.
Etimedout The call timed out before the semaphore was locked.
Follow posix.1-2001
Note that signal handlers always interrupt blocking calls to one of these functions, regardless of using the Sigaction (2) Sa_restart flag.
See Clock_gettime (2), Sem_getvalue (3), Sem_post (3), Sem_overview (7), Time (7)
3. Sem_post name Sem_post-release semaphore
Overview #include <semaphore.h>
Int sem_post (sem_t *sem);
link library-pthread
Describes Sem_post () increments (releases) the amount of signal the Sem points to. If the value of the semaphore becomes greater than 0, then other processes or threads that are blocked by the call through Sem_wait (3) will be awakened and continue to lock the semaphore.
The return value Sem_post () returns 0 successfully, and on failure, returns-1, the value of the semaphore remains constant, and the setting errno indicates an error.
Error EINVAL SEM is not a valid semaphore.
The eoverflow exceeds the maximum allowable value of the semaphore.
Follow posix.1-2001
Note that Sem_post () is an asynchronous signal safe: it can be safely called in a signal handler.
See Sem_getvalue (3), sem_wait (3), Sem_overview (7)
4. Sem_destroy name Sem_destroy-Destroy unnamed semaphore
Overview #include <semaphore.h>
int Sem_destroy (sem_t *sem);
link library-pthread
Description Sem_destroy () destroys the unnamed semaphore pointed to by the SEM.
Only semaphores initialized by Sem_init (3) can be destroyed by Sem_destroy ().
Destroying the semaphore that is currently blocked by another process or thread (sem_wait (3)) causes undefined behavior.
The return value Sem_destroy () returns 0 successfully, returns 1 when failed, and sets errno to indicate an error.
Error EINVAL SEM is not a valid semaphore.
Follow posix.1-2001
Note the unnamed semaphore should call Sem_destroy () before the memory it resides in is freed. If you do not do this, some implementations may cause a resource leak.
See Sem_init (3), Sem_post (3), sem_wait (3), Sem_overview (7)
Linux Programmer ' s Manual--unnamed semaphore