一步一步炼代码之多线程篇(二)经典PV问题
来源:互联网 发布:软件项目管理期末试卷 编辑:程序博客网 时间:2024/06/03 20:24
一 生产者消费者问题
环境:linux gcc vim
#include<stdio.h>#include<unistd.h>#include<pthread.h>#include<sys/ipc.h>#include<semaphore.h>#include<errno.h>#include<fcntl.h>#define N 5int count = 0;time_t time_end;sem_t mutex,empty,full;void productor(void* args);void customer(void* args);int main(){ int ret; pthread_t id1,id2; time_end = time(NULL)+30; sem_init(&mutex,0,1); sem_init(&empty,0,N); sem_init(&full,0,0); ret = pthread_create(&id1,NULL,(void*)productor,NULL); if(!ret) printf("thread of productor is created successfully.\r\n"); ret = pthread_create(&id2,NULL,(void*)customer,NULL); if(!ret) printf("thread of customer is created successfully.\r\n"); pthread_join(id1,NULL); printf("thread productor is return\r\n"); pthread_join(id2,NULL); printf("thread customer is return\r\n"); return 0;}void productor(void* args){ while(time(NULL)<time_end) { sem_wait(&empty); sem_wait(&mutex); if(count>=N) printf("FULL\r\n"); else { count++; printf("productor::count is %d\r\n",count); } sem_post(&mutex); sem_post(&full); sleep(1); } printf("thread productor is over\r\n");}void customer(void* args){ while(time(NULL)<time_end||count>0) { sem_wait(&full); sem_wait(&mutex); if(count<=0) printf("EMPTY\r\n"); else { count--; printf("customer::count is %d\r\n",count); } sem_post(&mutex); sem_post(&empty); sleep(2); } printf("thread customer is over\r\n");}
编译选项:gcc ProductorCustomer.c -lpthread -o mainApp
运行结果:productor::count is 1thread of productor is created successfully.customer::count is 0thread of customer is created successfully.productor::count is 1customer::count is 0productor::count is 1productor::count is 2customer::count is 1productor::count is 2productor::count is 3customer::count is 2productor::count is 3productor::count is 4customer::count is 3productor::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5thread productor is overthread productor is returncustomer::count is 4customer::count is 3customer::count is 2customer::count is 1customer::count is 0thread customer is overthread customer is return
环境:linux gcc vim
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <errno.h>#include <sys/ipc.h>#include <semaphore.h>#include <fcntl.h>#define n 5time_t end_time; /*end time*/sem_t mutex,customers,barbers;int count = 0;/*等待理发的顾客数*/void barber(void* arg);void customer(void* arg);int main(int argc, char *argv[]){ pthread_t id1,id2; int ret = 0; end_time = time(NULL)+20; /*初使化信号量的个数*/ sem_init(&mutex,0,1); sem_init(&customers,0,0); ret = sem_init(&barbers,0,1); if(0!=ret) perror("sem init."); /*初使化两个线程*/ ret = pthread_create(&id1,NULL,(void *)barber,NULL); if(0!=ret) perror("create barbers."); ret = pthread_create(&id2,NULL,(void *)customer,NULL); if(0!=ret) perror("create customers."); /*让顾客进程先阻塞*/ pthread_join(id2,NULL); pthread_join(id1,NULL); exit(0);}/*理发师进程*/void barber(void *arg){ while(time(NULL)<end_time||count>0) { sem_wait(&customers);/*p(customers)*/ sem_wait(&mutex);/*p(mutex)*/ count--; printf("Barber:cut hair,count is:%d.\n",count); sem_post(&mutex);/*v(mutex);*/ sem_post(&barbers);/*v(barbers) 通知已进来的,在等理发师的顾客进程*/ sleep(3); }}/*customer*//*顾客进程*/void customer(void *arg){ while(time(NULL)<end_time) { sem_wait(&mutex);/*p(mutex)*/ if(count<n) { count++; printf("Customer:add count,count is:%d.\n",count); sem_post(&mutex);/*v(mutex)*/ sem_post(&customers);/*v(customers)*/ sem_wait(&barbers);/*p(barbers)*/ } else { /*v(mutex) 注意 如果个客户数已满了,则把互斥锁放掉,*/ sem_post(&mutex); } sleep(1); }/*end of while(time()<end_time)*/}/*customer*/
编译选项:gcc SleepBarber.c -lpthread -o mainApp
运行结果:
Customer:add count,count is:1.Barber:cut hair,count is:0.Customer:add count,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Barber:cut hair,count is:0.
0 0
- 一步一步炼代码之多线程篇(二)经典PV问题
- 一步一步炼代码之多线程篇(一)基础
- 经典PV问题系列二:经典详解
- 【Java基础之多线程】代码库(二)
- JAVA之多线程(二)
- Java之多线程(二)
- JAVA之多线程(二)
- PV操作经典问题
- 经典线程同步问题(二)
- Windows多线程(二)经典线程问题
- C++ 之 经典线程同步案例之多线程(五)
- android学习笔记之多线程(二)
- Android开发之多线程下载(二)
- Qt之多线程(二)
- Java之多线程(二)
- 【Java进阶之多线程进阶】代码库(二)
- PV操作--经典问题分析
- 问题总结之多线程
- PAT (Basic Level) Practise (中文)1004. 成绩排名 (20)
- Burp suite 暴力破解shell密码详细教程
- do...while(0)的妙用
- linux下安装mysql出现错误conflicts with file from package
- ICE学习(四)-Ice Box介绍
- 一步一步炼代码之多线程篇(二)经典PV问题
- MFC开发链接库的选择
- mvc3 easyui 异常帮助类 (封装 try catch操作和统一调用 增删改查 方法) 和 easy ui 帮助类
- 《黑马程序员》 笔记本USB接口应用分析与设计
- grub rescue的修复
- makefile--隐含规则
- OC & C++运用回调函数
- SharedPreferences详解
- Console命令详解,让调试js代码变得更简单