




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
学 号: 题 目用多线程同步方法解决生产者消费者问题(producer-consumer problem)学 院 物理学与电子信息工程学院专 业电子信息工程班 级08电信本一班姓 名指导教师2010年12月日 操作系统课程设计说明书 目 录目 录1课程设计任务书1正 文21.设计目的与要求21.1设计目的21.2设计要求22.设计思想及系统平台22.1设计思想22.2系统平台及使用语言33.详细算法描述34.源程序清单65.运行结果与运行情况96.调试过程117.总结12课程设计任务书题目: 用多线程同步方法解决生产者消费者问题 (producer-consumer problem) 初始条件:1 操作系统:linux2 程序设计语言:c语言3 有界缓冲区内设有20个存储单元,其初值为0。放入取出的数据项按增序设定为120这20个整型数。要求完成的主要任务: (包括课程设计工作量及其技术要求,以及说明书撰写等具体要求) 1技术要求:1)为每个生产者消费者产生一个线程,设计正确的同步算法2)每个生产者和消费者对有界缓冲区进行操作后,即时显示有界缓冲区的当前全部内容、当前指针位置和生产者消费者线程的自定义标识符。3)生产者和消费者各有两个以上。4)多个生产者或多个消费者之间须共享对缓冲区进行操作的函数代码。2 设计说明书内容要求:1)设计题目与要求2)总的设计思想及系统平台、语言、工具等。3)数据结构与模块说明(功能与流程图)4)给出用户名、源程序名、目标程序名和源程序及其运行结果。(要注明存储各个程序及其运行结果的主机ip地址和目录。)5)运行结果与运行情况(提示: (1)有界缓冲区可用数组实现。(2)编译命令可用:cc -lpthread -o 目标文件名源文件名(3)多线程编程方法参见附件。) 3. 调试报告:1)调试记录2)自我评析和总结 正 文1.设计目的与要求1.1设计目的通过研究linux的线程机制和信号量实现生产者消费者问题(producer-consumer problem)的并发控制。1.2设计要求1)为每个生产者消费者产生一个线程,设计正确的同步算法2)每个生产者/消费者对该存储区进行操作后,即时显示该存储区的全部内容、当 前指针位置和生产者/消费者线程的自定义标识符。3)生产者和消费者各有两个以上。4)多个生产者/消费者之间须共享对存储区进行操作的函数代码。2.设计思想及系统平台2.1设计思想在本问题中,共需要一个mutex和两个semaphore.其中,mutex是用来锁定临界区的,以解决对共享数据buffer的互斥访问问题(无论是对生成者还是对消费者);我们共需要两个semaphore,这是因为在本问题中共有两个稀缺资源.第一种是非空这种资源,是在消费者之间进行竞争的.第二种是非满这种资源,是在生产者之间进行竞争的.所以,一般来说,需要锁定临界区,就需要mutex;有几种稀缺资源就需要几个semaphore.对稀缺资源的分析不能想当然.稀缺资源不一定是指被共享的资源,很多时候是指线程会被阻塞的条件(除了要进临界区被阻塞外).在生产者消费者问题中,消费者会在缓冲区为空时被阻塞,所以非空是一种稀缺资源;需要设置一个信号量consumer_semaphore,初值设为0;生产者会在缓冲区为满时被阻塞,所以非满也是一种稀缺资源.需要设置一个信号量producer_semaphore,初值设为buffer的大小max_buffer2.2系统平台及使用语言本课程设计在linux操作系统下,使用c语言完成。用到的工具主要有gcc编译器和vi编辑器。3.详细算法描述共享数据:semaphore buffer_mutex=1;semaphore producer_semaphore=max_buffer;semaphore consumer_semaphore=0;int buffermax_buffer;producer线程的处理函数:while(1)wait(producer_semaphore);wait(buffer_mutex);bufferpn=product;pn=(pn+1)%max_buffer;signal(consumer_semaphore);signal(buffer_mutex);sleep();producer线程的处理函数流程图如下:consumer线程的处理函数:while(1)wait(consumer_semaphore);wait(buffer_mutex);consume=buffercn;cn=(cn+1)%max_buffer;signal(producer_semaphore);signal(buffer_mutex);sleep();consumer线程的处理函数流程图如下:4.源程序清单用户名:rj070126(ip:192.168.2.254)源程序名:/home/rj070126/pc.c目标程序名:/home/rj070126/pc运行结果:/home/rj070126/output.txt源程序清单如下:#include#include#include#include#include#include#include#include#include#include/生产者的个数#define num_threads_p 5 /消费者的个数#define num_threads_c 5/缓冲区的大小 #define max_buffer 20 /运行的时间#define run_time 20/缓冲区的数组int buffermax_buffer; int produce_pointer=0,consume_pointer=0;sem_t producer_semaphore,consumer_semaphore,buffer_mutex;pthread_t threads_pnum_threads_p; /*producer*/pthread_t threads_cnum_threads_c; /*consumer*/file* fd; void *producer_thread(void *tid);void *consumer_thread(void *tid);void showbuf();void handler()int i;for(i=0;inum_threads_p;i+)/送终止信号给thread线程,如果成功则返回0,否则为非0值。发送成功并不意味着thread/会终止。pthread_cancel(threads_pi);for(i=0;inum_threads_c;i+)pthread_cancel(threads_ci);int main()int i;signal(sigalrm,handler);fd=fopen(output.txt,w); /*open a file to save the result*/sem_init(&producer_semaphore,0,max_buffer); /*set the value of semaphores*/sem_init(&consumer_semaphore,0,0);sem_init(&buffer_mutex,0,1);for(i=0;imax_buffer;i+)bufferi=0; /*initiate the buffer*/*create the threads*/for(i=0;inum_threads_p;i+)pthread_create(&threads_pi,null,(void*)producer_thread,(void*)(i+1);for(i=0;inum_threads_c;i+)pthread_create(&threads_ci,null,(void*)consumer_thread,(void *)(i+1);alarm(run_time); /*set time to run*/*wait the threads to exit*/for(i=0;inum_threads_p;i+)pthread_join(threads_pi,null);for(i=0;inum_threads_c;i+)pthread_join(threads_ci,null);/*destroy the semaphores*/sem_destroy(&producer_semaphore); sem_destroy(&consumer_semaphore);sem_destroy(&buffer_mutex);fclose(fd);return 0;void *producer_thread(void *tid)/*the thread can be canceled by other thread*/pthread_setcancelstate(pthread_cancel_enable,null);while(1)sem_wait(&producer_semaphore);srand(int)time(null)*(int)tid);sleep(rand()%2+1); /*one or two seconds to produce*/while(produce_pointer+1)%20=consume_pointer);sem_wait(&buffer_mutex);bufferproduce_pointer=rand()%20+1;produce_pointer=(produce_pointer+1)%20;if(produce_pointer=0) /*if buffer was filled to the 19th*/printf(producer:%d pointer_p:%2d produced:%2dn,(int)tid,19,buffer19);fprintf(fd,producer:%d pointer_p:%2d produced:%2dn,(int)tid,19,buffer19);elseprintf(producer:%d pointer_p:%2d produced:%2dn,(int)tid,produce_pointer-1,bufferproduce_pointer-1);fprintf(fd,producer:%d pointer_p:%2d produced:%2dn,(int)tid,produce_pointer-1,bufferproduce_pointer-1);showbuf();sem_post(&buffer_mutex);sem_post(&consumer_semaphore); /*inform the consumer the buffer is not empty*/srand(int)time(null)*(int)tid);sleep(rand()%5+1); /*wait a few seconds ,then continue producing*/return (void*)0);void *consumer_thread(void *tid)/*the thread can be canceled by other thread*/pthread_setcancelstate(pthread_cancel_enable,null);while(1)/信号等待sem_wait(&consumer_semaphore);/获取种子srand(int)time(null)*(int)tid);/休眠一到两秒sleep(rand()%2+1); /*one or two seconds to consume*/sem_wait(&buffer_mutex);printf(consumer:%d pointer_c:%2d consumed:%2dn,(int)tid,consume_pointer,bufferconsume_pointer);fprintf(fd,consumer:%d pointer_c:%2d consumed:%2dn,(int)tid,consume_pointer,bufferconsume_pointer);bufferconsume_pointer=0;consume_pointer=(consume_pointer+1)%20;showbuf();sem_post(&buffer_mutex);sem_post(&producer_semaphore); srand(int)time(null)*(int)tid);sleep(rand()%5+1); /*wait a few seconds ,then continue consuming*/return (void*)0);/*show the content of buffer*/void showbuf()int i;printf(buffer:);fprintf(fd,buffer:);for(i=0;imax_buffer;i+)printf(%2d ,bufferi);fprintf(fd,%2d ,bufferi);printf(nn);fprintf(fd,nn);5.运行结果与运行情况程序运行结果如下:producer:1 pointer_p: 0 produced:20buffer:20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:3 pointer_p: 1 produced:13buffer:20 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:2 pointer_p: 2 produced: 6buffer:20 13 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:4 pointer_p: 3 produced:14buffer:20 13 6 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:5 pointer_p: 4 produced:20buffer:20 13 6 14 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 consumer:2 pointer_c: 0 consumed:20buffer: 0 13 6 14 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:1 pointer_p: 5 produced:20buffer: 0 13 6 14 20 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 consumer:1 pointer_c: 1 consumed:13buffer: 0 0 6 14 20 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 consumer:3 pointer_c: 2 consumed: 6buffer: 0 0 0 14 20 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 consumer:4 pointer_c: 3 consumed:14buffer: 0 0 0 0 20 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 consumer:5 pointer_c: 4 consumed:20buffer: 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:3 pointer_p: 6 produced: 1buffer: 0 0 0 0 0 20 1 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:2 pointer_p: 7 produced:14buffer: 0 0 0 0 0 20 1 14 0 0 0 0 0 0 0 0 0 0 0 0 consumer:3 pointer_c: 5 consumed:20buffer: 0 0 0 0 0 0 1 14 0 0 0 0 0 0 0 0 0 0 0 0 producer:4 pointer_p: 8 produced: 6buffer: 0 0 0 0 0 0 1 14 6 0 0 0 0 0 0 0 0 0 0 0 consumer:5 pointer_c: 6 consumed: 1buffer: 0 0 0 0 0 0 0 14 6 0 0 0 0 0 0 0 0 0 0 0 producer:5 pointer_p: 9 produced: 8buffer: 0 0 0 0 0 0 0 14 6 8 0 0 0 0 0 0 0 0 0 0 consumer:2 pointer_c: 7 consumed:14buffer: 0 0 0 0 0 0 0 0 6 8 0 0 0 0 0 0 0 0 0 0 consumer:5 pointer_c: 8 consumed: 6buffer: 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 producer:1 pointer_p:10 produced:18buffer: 0 0 0 0 0 0 0 0 0 8 18 0 0 0 0 0 0 0 0 0 consumer:1 pointer_c: 9 consumed: 8buffer: 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 0 0 0 0 0 producer:2 pointer_p:11 produced:10buffer: 0 0 0 0 0 0 0 0 0 0 18 10 0 0 0 0 0 0 0 0 producer:4 pointer_p:12 produced:10buffer: 0 0 0 0 0 0 0 0 0 0 18 10 10 0 0 0 0 0 0 0 consumer:4 pointer_c:10 consumed:18buffer: 0 0 0 0 0 0 0 0 0 0 0 10 10 0 0 0 0 0 0 0 producer:3 pointer_p:13 produced: 3buffer: 0 0 0 0 0 0 0 0 0 0 0 10 10 3 0 0 0 0 0 0 consumer:3 pointer_c:11 consumed:10buffer: 0 0 0 0 0 0 0 0 0 0 0 0 10 3 0 0 0 0 0 0 consumer:2 pointer_c:12 consumed:10buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 producer:1 pointer_p:14 produced: 6buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 3 6 0 0 0 0 0 consumer:1 pointer_c:13 consumed: 3buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 producer:2 pointer_p:15 produced:18buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 18 0 0 0 0 consumer:5 pointer_c:14 consumed: 6buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 producer:1 pointer_p:16 produced: 6buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 6 0 0 0 producer:3 pointer_p:17 produced:19buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 6 19 0 0 consumer:1 pointer_c:15 consumed:18buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 19 0 0 producer:5 pointer_p:18 produced: 7buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 19 7 0 consumer:3 pointer_c:16 consumed: 6buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 7 0 producer:4 pointer_p:19 produced:14buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 7 14 consumer:5 pointer_c:17 consumed:19buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 14 consumer:4 pointer_c:18 consumed: 7buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 producer:1 pointer_p: 0 produced: 4buffer: 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 consumer:2 pointer_c:19 consumed:14buffer: 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 consumer:1 pointer_c: 0 consumed: 4buffer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:2 pointer_p: 1 produced:15buffer: 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:3 pointer_p: 2 produced:13buffer: 0 15 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 producer:2 pointer_p: 3 produced: 3buffer: 0 15 13 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 说明: “producer:2”是指自定义标号为2的producer,“pointer_p:3”是指该producer的指针,“produced:3”是指该producer在buffer3里写入3,“buffer: 0 15 13 3 0 0 0 0 0 0 0 0 0 0 0 0
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025长沙市商品房(预)销售合同(无水印、绝对珍藏版)
- 2025买卖合同的条款决定着违约时的责任分配
- 再制造工艺标准化-洞察及研究
- 家庭林场林业合作种植合同书
- 2025年已签订但未生效合同的仓管员职责处理规定
- 紫荆果国画题目大全及答案
- 2025【合同范本】咖啡店加盟合同
- 专四考试时间题目及答案
- 主流车型测试题目及答案
- 守护你的微笑600字7篇
- 2025年贵州省中考英语试题(附答案和音频)
- 驾驶员高级工考试题及答案
- 2025届四川眉山中考历史真题试卷【含答案】
- 2024北京北师大实验中学高二10月月考数学试题及答案
- 子宫腺肌症术后护理查房
- 在制品生产车间管理制度
- 学校口腔健康知识讲座
- 子公司运营规范管理制度
- 医疗美容手术管理制度
- 道路绿化修剪合同范本
- T/CSPSTC 75-2021微动探测技术规程
评论
0/150
提交评论