多线程编程系列(五):“生产者消费者”实验——PV原语实现
来源:互联网 发布:b2b行业网站源码 编辑:程序博客网 时间:2024/05/20 09:43
一、问题描述
有一个有限缓冲区和两个线程:生产者,消费者。他们分别往缓冲区写入产品和拿出产品。当缓冲区满时,生产者不能写必须等待;当缓冲区空时,消费者线程不能读,要等待。
二、实例
/*product.c*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <sys/ipc.h>#include <semaphore.h>#include <fcntl.h>#include <pthread.h>#define FIFO "myfifo"#define N 5int lock_var;time_t end_time;char buf_r[100];sem_t mutex,full,avail;int fd;void productor(void *arg);void consumer(void *arg);int main(int argc,char *argv[]){ pthread_t id1,id2; pthread_t mon_th_id; int ret; end_time = time(NULL)+30; /*create namepipe*/ if((mkfifo(FIFO,O_CREAT|O_EXCL)<0) && (errno != EEXIST)) printf("cannot create fifoserver\n"); printf("preparing for reading bytes..."); memset(buf_r,0,sizeof(buf_r)); /*open pipe*/ fd = open(FIFO,O_RDWR | O_NONBLOCK,0); if(fd == -1){ perror("open"); exit(1); } /*init sem valuable*/ ret = sem_init(&mutex,0,1); ret = sem_init(&avail,0,N); ret = sem_init(&full,0,0); if(ret != 0){ perror("sem_init"); } /*create 2 pthread*/ ret = pthread_create(&id1,NULL,(void *)productor,NULL); if(ret != 0) perror("pthread_cread1"); ret = pthread_create(&id2,NULL,(void *)consumer,NULL); if(ret != 0) perror("pthread_cread2"); pthread_join(id1,NULL); pthread_join(id2,NULL); exit(0);}/*product thread*/void productor(void *arg){ int i,nwrite; while(time(NULL) < end_time){ /*p operate to the avail,mutex*/ sem_wait(&avail); sem_wait(&mutex); /*productor write data to the pipe*/ if((nwrite = write(fd,"hello",5)) == -1){ if(errno == EAGAIN) printf("the FIFO has not been read yet,please try later\n"); } else printf("write hello to the FIFO\n"); /*v opertate to full,mutex*/ sem_post(&full); sem_post(&mutex); sleep(1); }}/*consumer thread*/void consumer(void *arg){ int nolock = 0; int ret,nread; while(time(NULL) < end_time){ sem_wait(&full); sem_wait(&mutex); memset(buf_r,0,sizeof(buf_r)); if(nread = read(fd,buf_r,100) == -1){ if(errno = EAGAIN) printf("no data yet\n"); } printf("read %s from FIFO\n",buf_r); sem_post(&avail); sem_post(&mutex); sleep(1); }}运行结果:
[root@localhost net]# ./productpreparing for reading bytes...write hello to the FIFOread hello from FIFOwrite hello to the FIFOread hello from FIFOwrite hello to the FIFOread hello from FIFOwrite hello to the FIFOread hello from FIFOwrite hello to the FIFOread hello from FIFOwrite hello to the FIFOread hello from FIFO
- 多线程编程系列(五):“生产者消费者”实验——PV原语实现
- 多线程编程系列(五):“生产者消费者”实验——PV原语实现
- 进程同步-生产者和消费者(PV操作实现——java多线程模拟)
- Java多线程系列(三)—生产者消费者模型
- javaSE基础编程——多线程(生产者和消费者)
- PV操作——生产者消费者问题
- JAVA多线程实现生产者——消费者
- Java——生产者消费者多线程实现
- Linux 多线程编程(实现生产者消费者模型)
- 多线程编程之生产者消费者(java实现)
- 用PV操作 实现生产者-消费者问题(C++语言)
- 多线程——生产者消费者
- 生产者-消费者多线程实现
- 多线程实现生产者消费者
- 多线程实现生产者消费者
- 多线程实现生产者消费者
- linux 多线程编程之——消费者与生产者
- Java多线程编程核心技术——生产者消费者模型
- POJ1989 The Cow Lineup——贪心+扫描技巧
- TestCocoon安装使用小记
- STUN检测NAT类型原理(转)
- 2011年软考系统架构设计师学习笔记第五章
- sengoku
- 多线程编程系列(五):“生产者消费者”实验——PV原语实现
- java 调用hbase 0.90.3
- 扩展方法及常见错误
- 前端开发中需要用到的变换矩阵
- ubuntu安装maya2011的方法
- 在Dell Dimension 4700上安装MAC
- 练手、练手
- pDlg->ShowWindow(SW_SHOWNORMAL);
- Android 调用相册 拍照 实现系统控件缩放 切割图片