The semaphores in Linux can be used both for synchronization between threads and for inter-process synchronization.
The semaphore is actually a non-negative integer counter that is used to control the public resources. When the public resource increases, the value of the signal increases, and when the public resource is consumed, the value of the semaphore decreases; the public resource with the table of the semaphore can be accessed only when the semaphore value is greater than 0.
PS: semaphores have POSIX interface and system API interface in Linux, the latter is too difficult to remember, so use the former directly.
The main functions of the semaphore are the semaphore initialization function Sem_init (3), the destruction function of the semaphore Sem_destroy (1), the increase of the semaphore function Sem_post (1), and the reduction function of the semaphore sem_wait (1).
The data type of the semaphore is the structure sem_t, which is essentially a number of long integers.
1. Initialization function of the semaphore Sem_init (3)
int Sem_init (sem_t *sem, int pshared, unsigned int value);
Parameters
The SEM pointer, pointing to the anonymous semaphore;
The value parameter specifies the initial value of the semaphore;
The pshared parameter indicates that the semaphore is shared by the process threads: 0 is thread sharing, not 0 is process sharing;
return value
Sem_init () returns 0 on success, returns 1 when error, and sets errno to the appropriate value.
2. Increase in the semaphore function Sem_post (1)
The role of the Sem_post function is to add a "1" to the value of the semaphore, which is an "atomic operation"-that is, two threads that add "1" to the same semaphore do not conflict.
int Sem_post (sem_t *sem);
Parameters
The SEM pointer, pointing to the anonymous semaphore;
return value
Sem_post () returns 0 on success, the value of the semaphore does not change when the error occurs, 1 is returned, and the errno is set to indicate the error.
3. Reduction of the semaphore function sem_wait (1)
The Sem_wait function is also an atomic operation whose function is to subtract a "1" from the value of the semaphore, but it will always wait until the semaphore is a non-0 value before starting the subtraction.
That is, if you call Sem_wait () on a semaphore with a value of 2, the thread will continue to execute, and the semaphore value will be reduced to 1.
If you call Sem_wait () on a semaphore with a value of 0, the function waits until another thread increments the value so that it is no longer 0.
int sem_wait (sem_t * sem);
Parameters
The SEM pointer, pointing to the anonymous semaphore;
return value
Sem_wait () returns 0 on success, returns 1 when error, and sets errno to the appropriate value.
4. Semaphore Destruction Function Sem_destroy (1)
int Sem_destroy (sem_t *sem);
Parameters
The SEM pointer, pointing to the anonymous semaphore;
return value
Sem_wait () returns 0 on success, returns 1 when error, and sets errno to the appropriate value.
Well, here's an example, producer consumers
/* Producer Consumer My example has 2 consumers, 2 producers, 10 buffers to store products */#include <stdio.h> #include <unistd.h> #include <pthread.h > #include <semaphore.h> #define buf_count 10#define consumer_count 2#define producer_count 2int pos;int CPOs; int Buf[buf_count];p thread_mutex_t buf_mutex,cbuf_mutex;sem_t buf_empty,buf_full;void* producer (void *arg) {int start = * (int*) Arg;int end = start + 10;while (Start < end) {sem_wait (&buf_empty);p thread_mutex_lock (&buf_mutex); Buf[pos] = start;printf ("producer%u put buf[%d]:%d\n", pthread_self (), Pos,buf[pos]);p OS = (pos+1)%buf_count;pthread_ Mutex_unlock (&buf_mutex); Sem_post (&buf_full); Usleep (1); ++start;} void* consumer (void *arg) {while (1) {sem_wait (&buf_full);p thread_mutex_lock (&cbuf_mutex);p rintf ("\ t Consumer%u take buf[%d]:%d\n ", pthread_self (), Cpos,buf[cpos]);p thread_mutex_unlock (&cbuf_mutex); CPOs = (cpos+1) %buf_count;usleep (); Sem_post (&buf_empty);}} int main () {sem_init (&buf_full,0,0); Sem_init (&buf_empty,0,10);p thread_mutex_Init (&buf_mutex,null);p thread_mutex_init (&cbuf_mutex,null);p thread_t Consumers[consumer_count];p thread_ T producers[producer_count];int producerstart[producer_count];int pc = 0;while (PC < Producer_count) {producerStart[ PC] = PC * 10;++PC;} int i = 0;while (i < Producer_count) {printf ("Main Create producer%d\n", i);p thread_create (&producers[i],null, Producer, (void*) &producerstart[i]); ++i;} i = 0;while (i < Consumer_count) {printf ("Main Create consumer%d\n", i);p thread_create (&consumers[i],null, Consumer,null); ++i;} i = 0;while (i < Producer_count) {pthread_join (producers[i],null); ++i;} i = 0;while (i < Consumer_count) {pthread_join (consumers[i],null); ++i;} Pthread_mutex_destroy (&buf_mutex);p Thread_mutex_destroy (&cbuf_mutex); Sem_destroy (&buf_full); sem_ Destroy (&buf_empty); return 0;}
Thread (process) synchronization--Semaphore