C Language Call library function to realize producer consumer problem

Source: Internet
Author: User
Tags semaphore

1#include <stdio.h>2#include <stdlib.h>3#include <semaphore.h>4#include <pthread.h>5#include <unistd.h>6 7 #defineNumof_producer 5//The Max Num of producer8 #defineNumof_consumer 10//The Max num of consumer9 #defineMaxnum 10//The Max num of productTenSem_t Empty_sem;//The goal of whether the num of product is null OneSem_t Full_sem;//The goal of whether the num of product is equal to Maxnum A  -pthread_mutex_t Mutex;//The goal of whether someone use the buff -  the intproducer_id =0; - intconsumer_id =0; - intNownumofproduce =0; - void*producer (void*ARG)//The thread of producer + { -     intid = producer_id++; +      while(1) A     { atSleep0.1); -Sem_wait (&full_sem);//When it comes to zero, it means that the NUM's product is equal to Maxnum -Pthread_mutex_lock (&mutex);//Lock the Buff -nownumofproduce++; -printf"producerthread%d product one,the num is:%d \ n", id%numof_producer,nownumofproduce); -Pthread_mutex_unlock (&Mutex); inSem_post (&empty_sem);//When it comes to ten, it means there is ten products can is used by consumer -     } to  +     return((void*)0); - } the  * void*consumer (void*Arg) $ {Panax Notoginseng     intid = consumer_id++; -      while(1) the     { +Sleep0.2); ASem_wait (&Empty_sem); thePthread_mutex_lock (&Mutex); +nownumofproduce--; -printf"Consumerthread%d use product one,the num is:%d \ n", id%numof_consumer,nownumofproduce); $Pthread_mutex_unlock (&Mutex); $Sem_post (&Full_sem); -     } -     return((void*)0); the } - Wuyi intMain () the { - pthread_t Con[numof_consumer]; Wu pthread_t Pro[numof_producer]; -  About     intTemp1 = Sem_init (&empty_sem,0,0); $     intTemp2 = Sem_init (&full_sem,0, maxnum); -     if(temp1&&temp2!=0) -     { -printf"sem init failed \ n"); AExit1); +     } the  -     intTemp3 = Pthread_mutex_init (&mutex,null); $  the     if(temp3!=0) the     { theprintf"Mutex init failed \ n"); theExit1); -     } in  the      for(intI=0; i<numof_producer;i++) the     { About         intTemp4 = Pthread_create (&pro[i],null,producer, (void*) &i); the         if(temp4!=0) the         { theprintf"thread Create failed!\n"); +Exit1); -         } the     }Bayi  the      for(intI=0; i<numof_consumer;i++) the     { -         intTEMP5 = Pthread_create (&con[i],null,consumer, (void*) &i); -         if(temp5!=0) the         { theprintf"thread Create failed!\n"); the         } theExit1); -     } the     //Destroy the thread the      for(intI=0; i<numof_consumer;i++) the     {94 Pthread_join (con[i],null); the     } the  the      for(intI=0; i<numof_producer;i++)98     { About Pthread_join (pro[i],null); -     }101 102     return 0;103}

Description: Unisted.h is used to call Sleep,pthread.h is the Linux system thread programming library, Semaphore.h is a function library using signal lights, as for those initialization method, wait method, post, etc. can be checked. If there are problems like sem_t not defined when compiling the program, you need to set up link links, find the Linux system installed in the Lib library libpthread.so import into it.

C Language Call library function to realize producer consumer problem

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.