版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、目录s录1用多线程同步方法解决生产者一消费者问题31. 设计题目与要求4设计题目42 设计思想及系统平台42丄设计思想42.2系统平台及使用语言53.数据结构与模块说明54 源程序清单错误!未定义书签。5 运行结果与运行情况13&调试过程13本科生课程设计成绩评定表15课程设计任务书学生姓名:专业班级:指导教师:工作单位:计算机科学与技术学院题目:用多线程同步方法解决生产者一消费者问题(producer-c onsumer pr oblem)初始条件:1. 操作系统:lin ux2. 程序设计语言:c语言3. 有界缓冲区内设有20个存储单元,其初值为0o放入/取出的数据项按 增序设定为
2、1 -20这20个整型数。要求完成的主要任务:(包括课程设计江作量及其技术要求,以及说明书撰写等具体要求)1. 技术要求:1)为每个生产者/消费者产生一个线程,设计正确的同步算法2)每个生产者和消费者对有界缓冲区进行操作后,即时显示有界缓 冲区的当前全部内容、当前指针位置和生产者/消费者线程的自 定义标识符。3)生产者和消费者各有两个以上。4)多个生产者或多个消费者之间须共享对缓冲区进行操作的函数代 码。2. 设计说明书内容要求:1)设计题目与要求2)总的设计思想及系统平台、语言、工具等。3)数据结构与模块说明(功能与流程图)4)给岀用户名、源程序名、目标程序名和源程序及其运行结果。(要注明存
3、储各个程序及其运行结果的主机ip地址和目录。)5)运行结果与运行情况(提示:(1 )有界缓冲区可用数组实现。(2)编译命令可用:cc -ipthrea d -o目标文件名源文件名(3 )多线程编程方法参见附件。)3. 调试报告:1)调试记录2)自我评析和总结上机时间安排:18 周一 五 08: 0 -12: 00指导教师签名:年月 日系主任(或责任教师)签名:年月日用多线程同步方法解决生产者一消费者问题1.设计题目与要求1.1设计题目解决生产者一消费者(bou nded - buf fer proble m)问题1.2设计要求1. 每个生产者和消费者对有界缓冲区进行操作后,即时显示有界缓冲区的
4、全部内容、当前指针位置和生产者/消费者线程的标识符。2. 生产者和消费者各有两个以上。3. 多个生产者或多个消费者z间须共享对缓冲区进行操作的函数代码。2设计思想及系统平台2.1设计思想生产者进程与消费者进程是经典的同步互斥关系。系统创建两类进程: proceduce r ()和consu mer (),分别用来描述生产者和消费者的行为。生产者 与消费者问题是指若干进程通过循环缓冲池区交换数据。牛产者进程不断向循 环缓冲池区中写入数据(即生产数据),而消费者进程不断从循环缓冲池区中读 出数据(即消费数据)。循环缓冲池共有n个缓冲区,缓冲区可以暂存一个产品, 任何时刻只能有一个进程可以对循环缓冲
5、池进行操作。只耍缓冲区未满,生产 者就可以把产品送入缓冲区;只要缓冲区未空,消费者就可以从缓冲区中取走 物品。为了解决生产者和消费者问题,应该设置信号量和变量如下: full:满缓冲区资源信号量,初值为0;empty:空缓冲区资源信号量,初值为min:生产者指针,初值均为0;out:消费者指针,均为0;mutex:缓冲区操作的互斥信号量,初值为1利用互斥信号量mu tex实现诸进程对缓冲池的互斥使用,利用信号量empty和f ull分别表示缓冲池中空缓冲池和满缓冲区的数量。2.2系统平台及使用语言1) 操作系统:linux2) 程序设计语言:c语言3) 编译器:gcc3. 数据结构与模块说明3
6、.1程序自定义函数1 void produce(struct semnfo * );这个函数是生产者进行的生产过程,为所有的生产者所共享。结构体指针用来接收生产者线程创建时传來的生产者的个人信息。2 void co nsumer(str uct semn fo *);这个函数是消费者进行的生产过程,为所有的消费者所共享。结构体指针 用来接收消费者线程创建时传来的消费者的个人信息。3、void setproduce (void);这个函数是用来设置生产者的个数和他们的名字。4、vo i d set con sumer (void);这个函数是用来设置消费者的个数和他们的名字。5、void act
7、i ve pthread (in t);这个函数是用来创建生产者线程,in t型参数为生产者的个数。6、void a ct i vecthread(int);这个函数是用来创建生产者线程,int型参数为生产者的个数。7、i nt gettime (void);这个函数返回来一个整数,作为线程的sleep()函数的参数。8、void mysca nf (void);这个函数用来获取设置生产者和消费者的个数时的整数,确保这个数字在o 到 max_buff er z间。32系统函数调用线程linux系统下的多线程遵循posix线程接口,称为pthreado编写linux下 的多线程程序,需要使用头文
8、件pthread.h,连接时需要使用库iibpthread.a<> linux下pthread的实现是通过系统调用clone ()來实现的。clone ()是linux 所特有的系统调用,它的使用方式类似forko函数时read_create用来创建一个线程,它的原型为: extern int pthread_create _p (pthread_t *_thread, _const pthread_attr_t *_attr,void*(*_start_routine)(void*),void*_arg);第一个参数为指向线程标识符的指针,第二个参数用來设置线程属性,第三个 参数
9、是线程运行函数的起始地址,最后一个参数是运行函数的参数。笫二个参 数我们也设为空指针,这样将生成默认属性的线程。当创建线程成功时,函数 返回0,若不为0则说明创建线程失败,常见的错误返冋代码为eagain和 einvalo前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第 二个参数代表的线程属性值非法。创建线程成功后,新创建的线程则运行参数 三和参数四确定的函数,原来的线程则继续运行下一行代码。 函数pthread join用来等待一个线程的结束。函数原型为: exter n int pthreadjoin _p (pthread_t _th, void *_thread_retur
10、n); 第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可 以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的 函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源 被收回。一个线稈的结束有两种途径,一种是函数结束了,调用它的线程也就 结束了;另一种方式是通过函数pthread.exit来实现。它的函数原型为: extern void pthread_exit _p (void *_retval) _attribute_ (_noreturn_); 唯一的参数是函数的返回代码,只要pthreadjoin中的第二个参数 thread_retur
11、n不是null,这个值将被传递给thread_returno最后要说明的是, 一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余 调用pthreadjoin的线程则返回错误代码esrch。信号量 信号量本质上是一个非负的整数计数器,它被用来控制对公共资源的访问。当 公共资源增加时,调用函数sem_post ()增加信号量。只有当信号量值大于0 时,才能使用公共资源,使用后,函数sem.wait ()减少信号量。函数 sem_trywait ()和函数pthread_ mutex_trylock ()起同样的作用,它是函数 sem_wait ()的非阻塞版本。它们都在头文件/u
12、sr/include/semaphore.h 41定义。 信号量的数据类型为结构sem_t,它本质上是一个长整型的数。函数semnit()用来初始化一个信号量。它的原型为: extern int sem_init _p (sem_t *_sem, int _pshared, unsigned int _value); sem为指向信号量结构的一个指针;pshared不为0时此信号量在进程间共享, 否则只能为当前进程的所有线程共享;value给出了信号量的初始值。 函数sem_post( sem.t *sem )用来增加信号虽的值。当有线程阻塞在这个信号量 上时,调用这个函数会使其屮的一个线程不
13、在阻塞,选择机制同样是由线程的 调度策略决定的。函数sem_wait( sem_t *sem )被用来阻塞当前线程直到信号量sem的值人于0, 解除阻塞后将sem的值减一,表明公共资源经使用后减少。函数semjrywait (sem_t *sem )是函数sem_wait ()的非阻塞版木,它直接将信号量sem的值减函数sem_destroy(sem_t水sem)用来释放信号量sem03.3流程图设置生产者消费者信息创建生产者生产者生产缓存区空?缓存区可进?创建生产者 生产者生产缓存区空?缓存区可进?程序结束失败否否否失败否4. 源程序清单#inelude <semaphore.h>
14、;#include <stdio.h>#include <sys/sem.h>#inelude <pthread.h>#include <time.h>#include <stringh>#include <malloc.h>#define max_buffer 20int in=0,out=0;int pnumcnum;int she ng 二 1;int xiao=l;int i;char buffermax_buffer;struct sem_info pthread_t semid;char * name;*pse
15、mmax_bufferz*csemmax_buffer;sem_t mutex,full,empty;生产者开始void produce(struct sem_info * rpsem)printf("m%d 个生产者创建完成!n",sheng+);while(l)int si;sleep(gettime();sem_wait(&empty);sem_wait(&m utex);if(in>19)in=o;srand(unsigned)time(null)+in);bufferin=rand()%100+10;printic'l am the
16、produce ,the pid:%d ,my name is:%s ,my products is:%dn1',&rpsemsemid,&rpsem>n ame,bufferin);printff'the in point is:%dn",in);prin tf("the con tents of the buffer is:n");for(sl=0;sl<max_buffer;sl+)printfd|”,buffersl);in+;pri ntf(hn生产结束nn");sem_post(&mutex
17、);sem_post(&full);void consumer(struct semnfo * rcsem)prin tf(nm%d个消费者创建完成!.n*xi ao+);while(l)sleep(gettime();int s2=0;sem_wait(&full);sem_wait(&m utex);讦(out>19)will consume theout=0;printf(hl am the consumer ,the pid:%d ,my name is:%s ,1 products:%drr:&rcsem>semict&rcsern
18、 >n ame,bufferout);bufferout=0;printff'the out point is:%dn",out);printf(hafter consume the buffer'contents is:n“);for(s2=0;s2<max_buffer;s2+)printf(h%dr'zbuffers2);out+;printf("n消费结束nnh);sem_post(&mutex);sem_post(&empty); void setproduce()print f(“请输入生产者的个数pn um=
19、mysca nf();for(i=0;i<pnum;i+)psemi=(struct semjnfo *)malloc(sizeof(struct sem_info);psemi->semid=o;printff请输入第d个生产者的名字:nj+l);sea nfs 舄&psemi> name);getchar();void setconsumer()printf ('请输入消费者的个数:“);cn um=mysca nf();for(i=0;i<cnum;i+)csemi=(struct semnfo *)malloc(sizeof(struct sem
20、nfo); csemi->semid=o;printff请输入第d(消费者的名字:j+l);scanf("%s",&csemi-> name);getchar(); int activepthreadfint number) int kl=o;int retl;for(kl=0;kl<number;kl+)memset(&psemkl->semidasizeof(psemkl->semid);retl=pthread_create(&psemkl卜semid,null,(void *)produce,psemkl);if
21、(retl!=o)pr intf(m创建第d个生产者线程失败lkl+l);return -1;return 0;int activecthread(int number) int k2=0;int ret2;for(k2=0;k2<number;k2+)memsetf&csemk2 卜semicl0sizeof(csernk2 卜semid);ret2=pthread_create(&csemk2->semid,null,(void *)consumer/csemk2);if(ret2!=o) prin tf(“创建第d个消费者线程失败!n,k2+l);return
22、 -1;return 0;int gettime()int j;sran d(unsigned)time(null);j=rand()%4+l;return j; int myscanf()int num=-99999; while(l)scanf("%dn,&num);getchar();if(num<=-99999| |num>=20)p rintfc'请输入1到20的一个整数elsebreak;return num;int main()int keyl,key2;semnit(&mutex,0,1);sem_i nit(&full,0
23、,0);semnit(&empty,o,max_buffer);setproduce();setc on sumer();keyl=activecthread(c num);key2=activepthread(pnum);if (keyl=o&&key2=0)pthread_join(psem0卜semid,null); 等待第一个生产者线程的结束,也就是让主线程处于无限等待屮,不会过早的退出5.编译及运行结果编译命令:gcc -o pv pv .c -pthrea d执行命令:./pv运行结果:rootlocalhost root# gcc pv.c -o pv -
24、pthreadrootlocalhost root# ./pv请输入生产者的个数:3请输入第1个生产者的名字:a请输入第2个生产者的名字:b请输入第3个生产者的名字:c请输入消费者的个数:2请输入第1个消费者的名字:si请输入第2个消费者的名字:s2第1个消费者创建完成!第2个消费者创建完成!第1个生产者创建完成!第2个生产者创建完成!第3个生产者创建完成!.i am the produce ,the pid:134517488 ,my name is:a ,my products is:70the in point is:0the contents of the buffer is:70|0
25、|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517504 ,my name is:b ,my products is:88the in point is:lthe contents of the buffer is:70|88|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517520 ,my name is:c ,my products is:93the in point is:2the contents
26、of the buffer is:70|88|93|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|生产结束i am the consumer ,the pid:134517536 ,my name is:sl ,1 will consume the products:70the out point is:0after consume the buffer'contents is:0|88|93|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|消费结束i am the consumer ,the pid:134517552 ,my name is
27、:s2 ,1 will consume the products:88 the out point is:lafter consume the buffer'contents is:0|0|93|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|消费结束i am the produce ,the pid:134517488 ,my name is:a ,my products is:46the in point is:3the contents of the buffer is:0|0|93|46|0|0 0|0|0|0|0|0|0|0|0|0|0|0|0|0|生产结
28、束i am the produce ,the pid:134517504 ,my name is:b ,my products is:70the in point is:4the contents of the buffer is:0|0|93|46|70|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517520 ,my name is:c ,my products is:50the in point is:5the contents of the buffer is:0|0|93|46|70|50|0|0|0|
29、0|0|0|0|0|0|0|0|0|0|0|生产结束i am the consumer ,the pid:134517536 ,my name is:sl ,1 will consume the products:93 the out point is:2after consume the buffer'contents is:0|0|0|46|70|50|0|0|0|0|0|0|0|0|0|0|0|0|0|0|消费结束i am the consumer ,the pid:134517552name is:s2 j will consume the products:46 the ou
30、t point is:3after con sume the buffer'c on tents is:0|0|0|0|70|50|0|0|0|0|0|0|0|0|0|0|0|0|0|0|消费结束i am the produce ,the pid:134517488 ,my name is:a ,my products is:46the in point is:6the contents of the buffer is:0|0|0|0|70|50 46|0|0|0|0|0|0|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517504
31、7my name is:b ,my products is:37the in point is:7the contents of the buffer is:0|0|0|0|70|50|46|37|0|0|0|0|0|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517520 ?my name is:c ,my products is:81the in point is:8the contents of the buffer is:0|0|0|0|70|50 46|37|81|0|0|0|0|0|0|0|0|0|0|0|生产结束i am the
32、consumer ,the pid:134517536 ,my name is:sl ,1 will consume the products:70 the out point is:4after consume the buffer'contents is:0|0|0|0|0|50|46|37|81|0|0|0|0|0|0|0|0|0|0|0|消费结束i am the consumer ,the pid:134517552 ,my name is:s2 ,1 will consume the products:50 the out point is:5after consume th
33、e buffer'contents is:0|0|0|0|0|0|46|37|81|0|0|0|0|0|0|0|0|0|0|0|消费结束i am the produce ,the pid:134517488 ,my name is:a ,my products is:69the in point is:9the contents of the buffer is:0|0|0|0|0|0|46|37|81|69|0|0|0|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517504 ,my name is:b ,my products is
34、:89the in point is:10the contents of the buffer is:0|0|0|0|0|0|46|37|81|69|89|0|0|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517520 7my name is:c ,my products is:67the in point is:llthe contents of the buffer is:0|0|0|0|0|0|46|37|81|69|89|67|0|0|0|0|0|0|0|0|生产结束i am the consumer ,the pid:1345175
35、36 ,my name is:sl ,1 will consume the products:46the out point is:6after con sume the buffer'c on tents is:0|0|0|0|0|0|0 37|81|69|89|67|0|0|0|0|0|0|0|0|湄赛结束i am the consumer ,the pid:134517552name is:s2 j will consume the products:37 the out point is:7after consume the buffer'contents is:0|0
36、|0|0|0|0|0 0|81|69|89|67|0|0|0|0|0|0|0|0|消费结束i am the produce ,the pid:134517488 ,my name is:a ,my products is:37the in point is:12the contents of the buffer is:0|0|0|0|0|0|0|0|81|69|89|67|37|0|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517504 7my name is:b ,my products is:21the in point is:13the
37、contents of the buffer is:0|0|0|0|0|0|0 0|81|69|89|67|37|21|0|0|0|0|0|0|生产结束i am the produce ,the pid:134517520 ?my name is:c ,my products is:102the in point is:14the contents of the buffer is:0|0|0|0|0|0|0 0|81|69|89|67|37|21|102|0|0|0|0|0|生产结束i am the consumer ,the pid:134517536 ,my name is:sl ,1
38、will consume the products:81 the out point is:8after consume the buffer'contents is:0|0|0|0|0|0|0 0|0|69|89|67|37|21|102|0|0|0|0|0|消费结束i am the consumer ,the pid:134517552 ,my name is:s2 ,1 will consume the products:69 the out point is:9after consume the buffer'contents is:0|0|0|0|0|0|0 0|0|
39、0|89|67|37|21|102|0|0|0|0|0|消费结束i am the produce ,the pid:134517488 ,my name is:a ,my products is:50the in point is:15the contents of the buffer is:0|0|0|0|0|0|0 0|0|0|89|67|37|21|102|50|0|0|0|0|生产结束i am the produce ,the pid:134517504 ,my name is:b ,my products is:16the in point is:16the contents of
40、 the buffer is:0|0|0|0|0|0|0 0|0|0|89|67|37|21|102|50|16|0|0|0|生产结束i am the produce ,the pid:134517520 7my name is:c ,my products is:40the in point is:17the contents of the buffer is:0|0|0|0|0|0|0 0|0|0|89|67|37|21|102|50|16|40|0|0|生产结束i am the consumer ,the pid:134517536 ,my name is:sl ,1 will consume the products:89 the out point is:10after con sume the buffer'c on tents is:0|0|0|0|0|0|0 0|0|0|0|67|37|21|102|50|16|40|0|0|湄赛结束i am the consumer ,the pid:134517552 ,m
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2031年中国经编干发巾行业投资前景及策略咨询研究报告
- 2025年普通妇检床项目可行性研究报告
- 2025年急诊室设备项目可行性研究报告
- 2025至2031年中国压力电波黑胶管行业投资前景及策略咨询研究报告
- 2025年卫生间用纺织品项目可行性研究报告
- 广西2025年广西医科大学第二附属医院护理人员招聘50人笔试历年参考题库附带答案详解
- 2025至2030年中国银行账户管理系统数据监测研究报告
- 2025至2030年中国金属鲍尔环填料数据监测研究报告
- 2025至2030年移动式电脑绗缝机项目投资价值分析报告
- 2025至2030年中国胡萝卜果蔬汁数据监测研究报告
- 中学学校2024-2025学年教师发展中心工作计划
- app 购买合同范例
- 小班期末家长会-双向奔赴 共育花开【课件】
- 矿山生态修复工程不稳定斜坡治理工程设计
- 2024年江西省高考物理试卷(含答案解析)
- 颈部瘢痕挛缩畸形治疗
- 贵州省贵阳市2023-2024学年五年级上学期语文期末试卷(含答案)
- 规划课题申报范例:俄罗斯教育改革研究(附可修改技术路线图)
- 运输企业安全事故报告调查处理制度(简单版5篇)
- 仓库货物安全管理
- 端午做香囊课件
评论
0/150
提交评论