Linux system Programming: Thread Synchronization-semaphore (semaphore)

Source: Internet
Author: User
Tags rand semaphore

Thread synchronization-semaphore (semaphore)

Rethinking the problems of producers and consumers

In real life, there is only a commodity. Consumers will be able to consume, that's fine.

But the production of producers is not unlimited. For example, the warehouse is limited, the raw materials are limited, production indicators are limited by consumption indicators and so on. In order to further solve the problem of producers and consumers, the introduction of signal volume into the mechanism.


Signal Volume

The Semaphore (semaphore) is an upgraded version of the mutually exclusive amount: The state of the mutually exclusive amount is 0 or 1. And the semaphore can be n.

That is, when using mutually exclusive amounts. A maximum of one thread is allowed to enter the critical area, and the semaphore agrees with multiple, and the detailed value is the current internal value of the semaphore.


related Functions

sem_t       //Semaphore type Sem_init (sem_t *sem, int pshared, unsigned int value); sem_wait (sem_t *sem) sem_trywaitsem_ Timedwaitsem_post (sem_t *sem) Sem_destroy
It is important to understand: sem_wait and sem_post two functions.

Sem_wait (SEM); When the SEM is zero, the thread is blocked. Otherwise, the SEM minus one, the thread does not clog.

Sem_post (SEM), SEM plus one.

In addition, the Sem_init method is used to initialize the semaphore type and the second parameter. The default is 0, marked for use between threads. The third parameter specifies the initial value.


single producer and single consumer

 #include <stdio.h> #include <unistd.h > #include <pthread.h> #include <semaphore.h> #define NUM 5sem_t blank_num, Product_num;int I, J, K;int Goods[num];void *producer (void *argv) {while (1) {sem_wait (&blank_num); Goods[i] = rand ()% + 1;printf ("Produce%d\ N ", Goods[i]); Sem_post (&product_num); i = (i + 1)% Num;sleep (rand ()% 2);}} void *comsumer (void *argv) {while (1) {sem_wait (&product_num);p rintf ("Comsume%d\n", Goods[j]); Goods[j] = 0;sem_ Post (&blank_num); j = (j + 1)% Num;sleep (rand ()% 2);}} int main (void) {i = j = k = 0;//Initialize semaphore sem_init (&blank_num, 0, num); Sem_init (&product_num, 0, 0);p thread_t Pro, com; Pthread_create (&com, NULL, producer, NULL);p thread_create (&pro, NULL, Comsumer, NULL);p thread_join (COM, null) ;p thread_join (Pro, NULL); Sem_destroy (&blank_num); Sem_destroy (&product_num); return 0;} 


Multi-producer and multi-consumer

#include <stdio.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #define NUM 5pthread_mutex_t M1, m2;sem_t blank_num, Product_num;int goods[num];int i, J, k;void *producer (void *argv) {while (1) {Sem_ Wait (&blank_num);p thread_mutex_lock (&AMP;M1); Goods[i] = rand ()% + 1;printf ("Produce%d\n", goods[i]); i = (i + 1) % Num;pthread_mutex_unlock (&AMP;M1); Sem_post (&product_num); Sleep (rand ()% 2);}} void *comsumer (void *argv) {while (1) {sem_wait (&product_num);p thread_mutex_lock (&m2);p rintf ("Comsume%d\n",   GOODS[J]); Goods[j] = 0; 0 J = (j + 1)% Num;pthread_mutex_unlock (&m2); Sem_post (&blank_num); Sleep (rand ()% 2);}} int main (void) {i = j = k = 0;//initialization semaphore and mutual repulsion amount Sem_init (&blank_num, 0, num); Sem_init (&product_num, 0, 0);p Thread_mute X_init (&AMP;M1, NULL);p thread_mutex_init (&m2, null);p thread_t pro[2], com[3];for (k = 0; k < 3; k++) pthread_create (&com[k], NULL, producer, null); for (k = 0; k < 2; k++) Pthread_create (&AMP;PRO[K], NULL, comsumer, NULL); for (k = 0; k < 3; k++) Pthread_join (com[k], NULL); for (k = 0; k < 2; k++) Pthread_jo In (Pro[k], NULL);p Thread_mutex_destroy (&AMP;M1);p Thread_mutex_destroy (&m2); Sem_destroy (&blank_num); sem_ Destroy (&product_num); return 0;}


CCPP Blog Folder


Linux system Programming: Thread Synchronization-semaphore (semaphore)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.