多线程编程系列(五):“生产者消费者”实验——PV原语实现

本文介绍了一个使用管道实现的生产者消费者模型案例。该模型通过两个线程模拟了生产者和消费者的交互过程,并利用信号量机制确保了数据同步和线程间的正确交互。

一、问题描述

有一个有限缓冲区和两个线程:生产者,消费者。他们分别往缓冲区写入产品和拿出产品。当缓冲区满时,生产者不能写必须等待;当缓冲区空时,消费者线程不能读,要等待。

二、实例

  1. /*product.c*/  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <unistd.h>  
  5. #include <errno.h>  
  6. #include <sys/ipc.h>  
  7. #include <semaphore.h>  
  8. #include <fcntl.h>  
  9. #include <pthread.h>  
  10. #define FIFO "myfifo"  
  11. #define N 5  
  12. int lock_var;  
  13. time_t end_time;  
  14. char buf_r[100];  
  15. sem_t mutex,full,avail;  
  16. int fd;  
  17. void productor(void *arg);  
  18. void consumer(void *arg);  
  19.   
  20. int main(int argc,char *argv[])  
  21. {  
  22.         pthread_t id1,id2;  
  23.         pthread_t mon_th_id;  
  24.         int ret;  
  25.         end_time = time(NULL)+30;  
  26.   
  27.         /*create namepipe*/  
  28.         if((mkfifo(FIFO,O_CREAT|O_EXCL)<0) && (errno != EEXIST))  
  29.                 printf("cannot create fifoserver\n");  
  30.         printf("preparing for reading bytes...");  
  31.         memset(buf_r,0,sizeof(buf_r));  
  32.   
  33.         /*open pipe*/  
  34.         fd = open(FIFO,O_RDWR | O_NONBLOCK,0);  
  35.         if(fd == -1){  
  36.                 perror("open");  
  37.                 exit(1);  
  38.         }  
  39.         /*init sem valuable*/  
  40.         ret = sem_init(&mutex,0,1);  
  41.         ret = sem_init(&avail,0,N);  
  42.         ret = sem_init(&full,0,0);  
  43.         if(ret != 0){  
  44.                 perror("sem_init");  
  45.         }  
  46.   
  47.         /*create 2 pthread*/  
  48.         ret = pthread_create(&id1,NULL,(void *)productor,NULL);  
  49.         if(ret != 0)  
  50.                 perror("pthread_cread1");  
  51.         ret = pthread_create(&id2,NULL,(void *)consumer,NULL);  
  52.         if(ret != 0)  
  53.                 perror("pthread_cread2");  
  54.         pthread_join(id1,NULL);  
  55.         pthread_join(id2,NULL);  
  56.   
  57.         exit(0);  
  58. }  
  59.   
  60. /*product thread*/  
  61. void productor(void *arg)  
  62. {  
  63.         int i,nwrite;  
  64.         while(time(NULL) < end_time){  
  65.         /*p operate to the avail,mutex*/  
  66.         sem_wait(&avail);  
  67.         sem_wait(&mutex);  
  68.         /*productor write data to the pipe*/  
  69.         if((nwrite = write(fd,"hello",5)) == -1){  
  70.                 if(errno == EAGAIN)  
  71.                         printf("the FIFO has not been read yet,please try later\n");  
  72.         }  
  73.         else  
  74.                 printf("write hello to the FIFO\n");  
  75.         /*v opertate to full,mutex*/  
  76.         sem_post(&full);  
  77.         sem_post(&mutex);  
  78.         sleep(1);  
  79.         }  
  80. }  
  81.   
  82. /*consumer thread*/  
  83. void consumer(void *arg)  
  84. {  
  85.         int nolock = 0;  
  86.         int ret,nread;  
  87.         while(time(NULL) < end_time){  
  88.                 sem_wait(&full);  
  89.                 sem_wait(&mutex);  
  90.                 memset(buf_r,0,sizeof(buf_r));  
  91.                 if(nread = read(fd,buf_r,100) == -1){  
  92.                         if(errno = EAGAIN)  
  93.                                 printf("no data yet\n");  
  94.                 }  
  95.                 printf("read %s from FIFO\n",buf_r);  
  96.                 sem_post(&avail);  
  97.                 sem_post(&mutex);  
  98.                 sleep(1);  
  99.         }  
  100. }  
运行结果:
  1. [root@localhost net]# ./product  
  2. preparing for reading bytes...write hello to the FIFO  
  3. read hello from FIFO  
  4. write hello to the FIFO  
  5. read hello from FIFO  
  6. write hello to the FIFO  
  7. read hello from FIFO  
  8. write hello to the FIFO  
  9. read hello from FIFO  
  10. write hello to the FIFO  
  11. read hello from FIFO  
  12. write hello to the FIFO  
  13. read hello from FIFO 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值