Producer and consumer issues

Source: Internet
Author: User

Producer-consumer issues are a well-known process synchronization issue. It is described as: there is a group of producer processes in the production of products, and these products are provided to consumers process consumption. In order for the producer process to execute concurrently with the consumer process, a buffer pool with n buffers is set up between the two, and the producer process puts the products it produces into a buffer; the consumer process can take the product away from one buffer to consume. Although all producer and consumer processes run asynchronously, they must be kept in sync , not allowing the consumer process to take products to an empty buffer, nor allow the producer process to serve a product in a buffer that is already full and has not been taken away.


<1> single-producer and single-consumer

#include  <stdio.h> #include  <pthread.h> #include  <semaphore.h>sem_t space_ sem,data_sem;//Semaphore int buf[10];//Buffer Void* sem_producter (void* arg)   //producer {int  Start = 0;while (1) {sem_wait (&AMP;SPACE_SEM);  //p operation    application space, if space is 0, hang int data =  rand ()%1234;printf ("producter data : %d\n", data);buf[start] = data;start++; Start %= 10;sem_post (&data_sem);//v operation    Application data, if data is 0, suspend sleep (1);}} Void* sem_consumer (void* arg)    //Consumer {int start = 0;while (1) {sem_wait ( &data_sem) Int data = buf[start];p rintf ("consumer data : %d\n", data); start++ ; Start %= 10;sem_post (&space_sem);//sleep (1);}} Int main () {sem_init (&space_sem,0,10);//Initialize Semaphore Sem_init (&data_sem,0,0);p thread_t id1,id2,id3, Id4;pthread_create (&id1,null,sem_producter,null);  //Create thread pthread_create (&id2,nUll,sem_consumer,null);p Thread_join (id1,null);//wait for process pthread_join (id2,null); Sem_destroy (&space_sem);// Release Resources Sem_destroy (&AMP;DATA_SEM); return 0;}

If the producer sleep (1), produce a consumption one. When the buffer is empty, the consumer hangs until the producer produces the product to awaken the consumer to consume.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/3D/wKiom1ed39DiC2W3AAA-_Hf-dlY878.png "title=" 1.PNG " alt= "Wkiom1ed39dic2w3aaa-_hf-dly878.png"/>

If the consumer sleep (1), the producer will first fill the buffer production, and then hang, until consumers consume one, to wake up the producers produce products.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/3D/wKiom1ed4WKyPZwIAAAvK8m0GO4320.png "title=" 2.PNG " alt= "Wkiom1ed4wkypzwiaaavk8m0go4320.png"/>


<2> Multi-producer multi-consumer (for example, two producers and two consumers)

#include  <stdio.h> #include  <pthread.h> #include  <semaphore.h>sem_t space_ sem,data_sem; //Semaphore int buf[10];//Buffer static start = 0;static begin = 0; Void* sem_producter (Void* arg) {while (1) {sem_wait (&AMP;SPACE_SEM);  //request Space  p operation Int data  = rand ()%1234;printf ("producter data : %d\n", data); Buf[start] = data;start ++;start %= 10;sem_post (&data_sem);  //release data  v operation Sleep (1);}} Void* sem_consumer (Void* arg) {while (1) {sem_wait (&AMP;DATA_SEM);   //request data   p operation INT  data = buf[begin];p rintf ("consumer data : %d\n", data); begin++;begin %=  10;sem_post (&space_sem);   //free Space  v operation//sleep (1);}} Int main () {sem_init (&space_sem,0,10);  //initialization, free space is 10sem_init (&data_sem,0,0);p thread_t  Id1,id2,id3,id4;pthread_create (&id1,null,sem_producter,null);p thread_create (&amP;id2,null,sem_consumer,null);p thread_create (&id3,null,sem_producter,null);p thread_create (&id4,NULL, Sem_consumer,null);p Thread_join (id1,null),  pthread_join (id2,null);p thread_join (id3,null);p Thread_join (id4 , NULL); Sem_destroy (&space_sem); Sem_destroy (&data_sem); return 0;}

Test results:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/3D/wKiom1ed4mLyHUjEAAA3enVeqX4449.png "title=" 3.PNG " alt= "Wkiom1ed4mlyhujeaaa3enveqx4449.png"/>

This article from "Together to see the Stars" blog, reproduced please contact the author!

Producer and consumer issues

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.