In Linux, the Semaphore API has two sets of system V IPC semaphores in multi-process programming, and the other is the POSIX semaphore we are going to discuss. These two sets of interfaces are similar but are not guaranteed to be interchangeable. The POSIX semaphore function has been sem_ beginning, and does not start with pthread_ like most thread functions, and the following 5 are commonly used:
#include <semaphore.h>int sem_init (sem_t* sem, int pshared, unsigned int value); int Sem_destroy (sem_t *sem); int sem _wait (sem_t *sem); int sem_trywait (sem_t *sem); int sem_post (sem_t *sem);
The first parameter of these functions, the SEM, points to the semaphore being manipulated, the above functions return 0 on success, the failure returns 1 and the errno is set.
The L sem_init function is used to initialize an unnamed semaphore (the POSIX Semaphore API supports named semaphores, but is not discussed in this section). Pshared the type of semaphore, if its value is 0, indicates that the semaphore is the local semaphore of the current process, otherwise the semaphore can be shared between multiple processes. Value creates the initial value of the semaphore, and initializing a semaphore that has already been initialized will result in unpredictable consequences.
L Sem_destroy is used to destroy the semaphore to free up the kernel resources it occupies. If you destroy a semaphore that is waiting, it will result in unpredictable consequences
L sem_wait The semaphore value minus 1 as an atomic operation, and if the semaphore value is 0, the sem_wait will be blocked until the semaphore value is not a 0 value
L sem_trywait is similar to the sem_wait function, but it always returns immediately, regardless of whether the semaphore has a non-0 value, which is equivalent to a non-blocking version of sem_wait. When the semaphore value is not 0 o'clock, the sem_trywait performs a minus 1 operation on the semaphore; When the semaphore is 0 o'clock, it will return 1 and set errno to Eagain
L sem_post The value of the semaphore by an atomic operation by 1, and when the semaphore value is greater than 0 o'clock, the other thread that is calling Sem_wait waits for the semaphore will be awakened
example of using semaphores in Threads:
#include <stdio.h> #include <stdlib.h> #include <semaphore.h> #include <pthread.h> #define Err_ SYS (msg) do {perror (msg), exit ( -1), and (0) #define ERR_EXIT (msg) do {fprintf (stderr, msg), Exit ( -1), and (0) void * R1 (void *arg) {sem_t* SEMs = (sem_t *) arg;static int cnt = 10;while (cnt--) {sem_wait (SEMS);p rintf ("I am in R1. I get the Sems.\n ");}} void *r2 (void *arg) {sem_t* SEMs = (sem_t *) arg;static int cnt = 10;while (cnt--) {printf ("I am in R2. I send the sems\n "); Sem_post (SEMS); sleep (1);}} int main (void) {sem_t sems;pthread_t T1, t2;printf ("SEMs Size:%d\n", sizeof (SEMS));/* Sem_init () The second parameter, 0, indicates that the semaphore is the local semaphore of the current process, otherwise the signal * can be shared between multiple processes */if (Sem_init (&sems, 0, 0) < 0) Err_sys ("Sem_init error");p thread _create (&T1, NULL, R1, &sems);p thread_create (&t2, NULL, R2, &sems);p thread_join (t1, null);p Thread_ Join (T2, NULL); Sem_destroy (&sems); return 0;}
Reference:
1, "Linux high-performance server Programming," chapter 14th multithreaded Programming/signal Volume
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux multithreaded Programming-semaphores