多线程(生产者和消费者)

有一个有限缓冲区(这里用有名管道实现FIFO式缓冲区)和两个线程:生产者和消费者。它们不停地把产品放入缓冲区和从缓冲区中拿走产品。一个生产者在缓冲区满的时候必须等待,一个消费者在缓冲区空的时候也必须等待。

信号量的考虑:这里使用3个信号量avail和full分别用于解决生产者和消费者线程之间的同步问题,mutex是用于这两个线程之间的互斥问题。其中avail表示有界缓冲区中的空单元数初始值为N,full表示有界缓冲区非空单元数,初始值为0;mutex是互斥信号量,初始值为1。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<pthread.h>
#include<errno.h>
#include<semaphore.h>
#include<sys/ipc.h>

#define MYFIFO "myfifo"  //缓冲区有名管道
#define BUFFER_SIZE 3    //缓冲区单元数
#define UNIT_SIZE 5      //每个单元的大小
#define RUN_TIME 30     //运行时间
#define DELAY_TIME_LEVELS 5.0     //周期的最大值

int fd;
time_t end_time;
sem_t mutex,full,avail;    //3个信号量

void *producer(void *arg)   //生产者线程
{
 int real_write;
 int delay_time = 0;
 
 while(time(NULL)<end_time)
 {
  delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)/2.0)+1;
  sleep(delay_time);
  sem_wait(&avail);    //p操作信号量avail和mutex
  sem_wait(&mutex);
  printf("/nProducer:delay = %d/n",delay_time);
  if((real_write = write(fd,"hello",UNIT_SIZE)) == -1)   //生产者写入数据
  {
   if(errno == EAGAIN)
   {
    printf("The FIFO has not been read yet.Please try later/n");
   }
  }
  else
  {
   printf("Write %d to the FIFO/n",real_write);
  }
  sem_post(&full);   //v操作信号量full和mutex
  sem_post(&mutex);
 }
 pthread_exit(NULL);
}

void *customer(void *arg)   //消费者线程
{
 unsigned char read_buffer[UNIT_SIZE];
 int real_read;
 int delay_time;

 while(time(NULL)<end_time)
 {
  delay_time = (int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX))+1;
  sleep(delay_time);
  sem_wait(&full);    //p操作信号量full和mutex
  sem_wait(&mutex);
  memset(read_buffer,0,UNIT_SIZE);
  printf("/nCustomer:delay = %d/n",delay_time);
  if((real_read = read(fd,read_buffer,UNIT_SIZE)) == -1)
  {
   if(errno == EAGAIN)
   {
    printf("No data yet/n");
   }
  }
  printf("Read %s from FIFO/n",read_buffer);
  sem_post(&avail);    //v操作信号量avail和mutex
  sem_post(&mutex);
 }
 pthread_exit(NULL);
}

int main()
{
 pthread_t thrd_prd_id,thrd_cst_id;
 pthread_t mon_th_id;
 int ret;
 srand(time(NULL));
 end_time = time(NULL) + RUN_TIME;
 if((mkfifo(MYFIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))   //创建有名管道
 {
  printf("Cannot create fifo/n");
  return errno;
 }
 
 fd = open(MYFIFO,O_RDWR);   //打开管道
 if(fd == -1)
 {
  printf("Open fifo error/n");
  return fd;
 }
 
 ret = sem_init(&mutex,0,1);    //初始化互斥信号量为1
 ret += sem_init(&avail,0,BUFFER_SIZE);   //初始化avail信号量为n
 ret += sem_init(&full,0,0);   //初始化full信号量为0

 if(ret != 0)
 {
  printf("Create producer thread error/n");
  return ret;
 }
 
 ret = pthread_create(&thrd_prd_id,NULL,producer,NULL);   //创建两个线程
 if(ret!=0)
 {
  printf("Create producer thread error/n");
  return ret;
 }
 ret = pthread_create(&thrd_cst_id,NULL,customer,NULL);   
 if(ret!=0)
 {
  printf("Create customer thread error/n");
  return ret;
 }
 pthread_join(thrd_prd_id,NULL);
 pthread_join(thrd_cst_id,NULL);
 close(fd);
 unlink(MYFIFO);
 return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值