Realization of producer consumer problem in Linux environment

Source: Internet
Author: User

#include<stdio.h>#include<semaphore.h>#include<stdlib.h>#include<pthread.h>#include<unistd.h>#defineN1 3//definition of 3 producers#defineN2 4//definition of 4 consumers#defineM 10//define 10 size buffersint inch=0;int  out=0;intBuff[m] = {0};//buffer size is tensem_t Empty_sem;//number of empty buffersSem_t Full_sem;//number of full bufferspthread_mutex_t Mutex;//mutually exclusive access buffersintproduct_id =0;intconsumer_id =0;intdata; FILE*FP;void*product () {intID = + +product_id;  while(1) {Sleep (1); Sem_wait (&Empty_sem); Pthread_mutex_lock (&mutex); //if (feof (fp)! = 0) fseek (FP, 0, Seek_set);        if(FP, fscanf"%d", &data) = =EOF) {fseek (FP),0, Seek_set); FSCANF (FP,"%d", &data); }        inch=inch%M; buff[inch] =data; printf ("Producter%d produce%d in position%d\n", ID, buff[inch],inch); ++inch; Pthread_mutex_unlock (&mutex); Sem_post (&Full_sem); }}void*consume () {intID = + +consumer_id;  while(1) {Sleep (1); Sem_wait (&Full_sem); Pthread_mutex_lock (&mutex);  out= out%M; printf ("Consumer%d take product%d in position%d\n", ID, buff[ out], out); buff[ out] =0; ++ out; Pthread_mutex_unlock (&mutex); Sem_post (&Empty_sem); }}intMain () {pthread_t id1[n1];//Defining producer Threadspthread_t ID2[N2];//Defining consumer Threads    inti; intRET1[N1]; intRET2[N2]; intIni1 = Sem_init (&empty_sem,0, M);//Initialize the empty buffer to M (TEN)    intIni2 = Sem_init (&full_sem,0,0);//Initialize full buffer 0    if(Ini1 && Ini2! =0) {printf ("Sem Init failed\n"); Exit (1); }    intIni3 = Pthread_mutex_init (&mutex, NULL);//Initialize the mutex signal volume    if(Ini3! =0) {printf ("Mutex init failed\n"); Exit (1); } FP= fopen ("/.data.txt","R");//Open File Data.txt    if(fp = = NULL) exit (1);  for(i =0; i < N1; i++) {Ret1[i]= Pthread_create (&id1[i], NULL, product, (void*) (&i));//Create a producer thread        if(Ret1[i]! =0) {printf ("product%d creat failed\n", i); Exit (1); }    }     for(i =0; i< N2; i++) {Ret2[i]= Pthread_create (&id2[i], NULL, consume, NULL);//Create a consumer thread        if(Ret2[i]! =0) {printf ("consumer%d creat failed\n", i); Exit (1); }    }     for(i =0; i < N1; i++{pthread_join (id1[i], NULL);}  for(i =0; i < N2; i++{pthread_join (id2[i], NULL);} Exit (0);}

Realization of producer consumer problem in Linux environment

Related Article

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.