Linux 下多线程的消费者-生产者模型

消费者-生产者模型

    所谓这个模型指在ipc时,由扮演的生产者进程产生数据,由扮演消费者的进程去拿走数据。
    这个模型是由3种关系俩种角色一个场景所描述而成。
    三种关系指:
    消费者-消费者  --->  互斥
    生产者-生产者  --->  互斥
    消费者-生产者  --->  同步与互斥
    俩种角色指:
    消费者与生产者
     一个场景:
     临界区:多个进程看到的同一份临界资源。
单消费-单产生模型代码

    基于链表实现(应用了互斥锁和条件变量)
#ifndef CON_PROC_H
#define CON_PROC_H 
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
typedef int datatype;
typedef struct list{
   int _value;
   struct list * _pNext;
} Node,*PNode,**position;
PNode head;
void Insert(datatype value,position p)
{
    PNode NewNode=(Node*)malloc(sizeof(Node));
    if(NewNode==NULL)
   {   
      perror("malloc");
      return ;
   }   
   if(*p==NULL)
   {   
     NewNode->_value=value;
     NewNode->_pNext=NULL; 
     *p=NewNode;
     return ;
   }   
   NewNode->_value=value;
   NewNode->_pNext=*p;
   *p=NewNode;
}
int  empty()
{
   return head==NULL;
}
void pop_front(int*p)
{
   if(empty())
    {   
     printf("List is empty\n");
      return ;
    }
   PNode del=head;
   *p=head->_value;
   head=head->_pNext;
   free(del);
}
void Delete_List()
{
   PNode del;
   while(head)
   {
     del=head;
     head=head->_pNext;
     free(del);
   }
}
#endif
             
#include "con_prod.h"
static pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
PNode head=NULL;
void* product(void*i)
{
  int goods=0;   
  while(1)
  {
    sleep(1); 
    pthread_mutex_lock(&lock);
    goods=rand()%9999;
    Insert(goods,&head);
    printf("product success :%d",goods);
    pthread_mutex_unlock(&lock);
    pthread_cond_signal(&cond);
  }
  return (void*)0;
}
void * consume(void*j)
{
  int p=0;
  while(1)
  {
    pthread_mutex_lock(&lock);
    if(empty()) pthread_cond_wait(&cond,&lock);
    pop_front(&p);
    printf("buy success :%d\n",p);
    pthread_mutex_unlock(&lock);
  }
  return (void*)1;
}
int main()
{
   pthread_t tid,tid_2;
   pthread_create(&tid,NULL,product,NULL);
   pthread_create(&tid_2,NULL,consume,NULL);
   pthread_join(tid,NULL);
   return 0;
}
 
    
    基于环形队列实现(应用了信号量)
#ifndef CON_PROC_H
#define CON_PROC_H 
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#define SIZE 10
int Bank[SIZE];
sem_t product_sem;
sem_t consume_sem;
void Init_sem()
{
   size_t nums= sizeof(Bank)/sizeof(Bank[0]);
   size_t idx=0;
   for( idx=0;idx<nums;idx++)
   {
     Bank[idx]=0;
   }
   sem_init(&product_sem,0,10);
   sem_init(&consume_sem,0,0);
}
void Destroy_all()
{
    sem_destroy(&product_sem);
    sem_destroy(&consume_sem);
    printf("delete sem success\n");
}
#endif
                                                                       
#include "con_prod.h"
PNode head=NULL;
int Bank[SIZE];
sem_t product_sem;
sem_t consume_sem;
void* product(void*i)
{
  int step=0;
  int goods=0;   
  while(1)
  {
    sem_wait(&product_sem); 
    Bank[step]=rand()%9999; 
    printf("product success,step:%d GOODs :%d\n",step,Bank[step]);
    step=(++step)%SIZE; 
    sem_post(&consume_sem);
    sleep(1);
  }
  return (void*)0;
}
void * consume(void*j)
{
  int p=0;
  int step=0;
  while(1)
  {
    sem_wait(&consume_sem);
    printf("buy success step:%d,GOODS:%d\n",step,Bank[step]);
    step=(++step)%SIZE;
    sem_post(&product_sem);
    sleep(2);
  }
  return (void*)1;
}
int main()
{
   Init_sem();
   pthread_t tid,tid_2;
    pthread_create(&tid,NULL,product,NULL);
   pthread_create(&tid_2,NULL,consume,NULL);
   pthread_join(tid,NULL);
   pthread_join(tid,NULL);
   Destroy_all();
   return 0;
}
          
   

多消费-多产生模型代码

   
#ifndef CON_PROC_H
#define CON_PROC_H 
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#define SIZE 10
int Bank[SIZE];
sem_t product_sem;
sem_t consume_sem;
void Init_sem()
{
   size_t nums= sizeof(Bank)/sizeof(Bank[0]);
   size_t idx=0;
   for( idx=0;idx<nums;idx++)
   {
     Bank[idx]=0;
   }
   sem_init(&product_sem,0,10);
   sem_init(&consume_sem,0,0);
}
void Destroy_all()
{
    sem_destroy(&product_sem);
    sem_destroy(&consume_sem);
    printf("delete sem success\n");
}
#endif
#include "con_prod.h"
int Bank[SIZE];
sem_t product_sem;
sem_t consume_sem;
pthread_mutex_t pro_lock=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t con_lock=PTHREAD_MUTEX_INITIALIZER;
void* product(void*i)
{
  int step=0;
  int goods=0;   
  while(1)
  {
    pthread_mutex_lock(&pro_lock);
    sem_wait(&product_sem); 
    Bank[step]=rand()%9999; 
    printf("product success,tid:%u tep:%d  GOODs :%d\n",pthread_self(),step,Bank[step]);
    step=(++step)%SIZE; 
    sem_post(&consume_sem);
    pthread_mutex_unlock(&pro_lock);
    sleep(2);
  }
  return (void*)0;
}
void* product_2(void*i)
{
  int step=0;
  int goods=0;   
  while(1)
  {
    pthread_mutex_lock(&pro_lock);
    sem_wait(&product_sem); 
    Bank[step]=rand()%9999; 
    printf("product 2 success,tid:%u tep:%d  GOODs :%d\n",pthread_self(),step,Bank[step]);
    step=(++step)%SIZE; 
    sem_post(&consume_sem);
    pthread_mutex_unlock(&pro_lock);
    sleep(2);
  }
return (void*)0;
}
void * consume(void*j)
{
  int p=0;
  int step=0;
  while(1)
  {
    pthread_mutex_lock(&con_lock);
    sem_wait(&consume_sem);
    printf("conmuse 1 buy success ,tid:%u step:%d,GOODS:%d\n",pthread_self(),step,Bank[step]);
    step=(++step)%SIZE;
    sem_post(&product_sem);
    pthread_mutex_unlock(&con_lock);
    sleep(1);
  }
  return (void*)1;
}
void * consume_2(void*j)
{
  int p=0;
  int step=0;
  while(1)
  {
    pthread_mutex_lock(&con_lock);
    sem_wait(&consume_sem);
    printf("consume 2 buy success ,tid:%u step:%d,GOODS:%d\n",pthread_self(),step,Bank[step]);
    step=(++step)%SIZE;
    sem_post(&product_sem);
    pthread_mutex_unlock(&con_lock);
    sleep(1);
  }
  return (void*)1;
}
int main()
{
   Init_sem();
   pthread_t con,con_2,prod,prod_2;
   pthread_create(&prod,NULL,product,NULL);
   pthread_create(&con,NULL,consume,NULL);
   pthread_create(&prod_2,NULL,product_2,NULL);
   pthread_create(&con_2,NULL,consume_2,NULL);
   pthread_join(con,NULL);
   pthread_join(con_2,NULL);
   pthread_join(prod,NULL);
   pthread_join(prod_2,NULL);
   Destroy_all();
   return 0;
}

  



没有更多推荐了,返回首页