《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc

上传人:来看看 文档编号:5016895 上传时间:2020-01-28 格式:DOC 页数:19 大小:125.50KB
返回 下载 相关 举报
《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc_第1页
第1页 / 共19页
《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc_第2页
第2页 / 共19页
《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc_第3页
第3页 / 共19页
《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc_第4页
第4页 / 共19页
《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc》由会员分享,可在线阅读,更多相关《《操作系统》课程设计说明书-用多线程同步方法解决生产者-消费者问题.doc(19页珍藏版)》请在三一文库上搜索。

1、操作系统课程设计说明书 目 录目 录2用多线程同步方法解决生产者消费者问题41. 设计题目与要求41.1设计题目41.2设计要求42.设计思想及系统平台42.1设计思想42.2系统平台及使用语言53.数据结构与模块说明54.源程序清单75.运行结果与运行情况136.调试过程137.总结14本科生课程设计成绩评定表15课程设计任务书学生姓名: 专业班级: 指导教师: 工作单位: 计算机科学与技术学院 题目: 用多线程同步方法解决生产者消费者问题 (Producer-Consumer Problem) 初始条件:1 操作系统:Linux2 程序设计语言:C语言3 有界缓冲区内设有20个存储单元,其

2、初值为0。放入取出的数据项按增序设定为120这20个整型数。要求完成的主要任务: (包括课程设计工作量及其技术要求,以及说明书撰写等具体要求) 1技术要求:1)为每个生产者消费者产生一个线程,设计正确的同步算法2)每个生产者和消费者对有界缓冲区进行操作后,即时显示有界缓冲区的当前全部内容、当前指针位置和生产者消费者线程的自定义标识符。3)生产者和消费者各有两个以上。4)多个生产者或多个消费者之间须共享对缓冲区进行操作的函数代码。2 设计说明书内容要求:1)设计题目与要求2)总的设计思想及系统平台、语言、工具等。3)数据结构与模块说明(功能与流程图)4)给出用户名、源程序名、目标程序名和源程序及

3、其运行结果。(要注明存储各个程序及其运行结果的主机IP地址和目录。)5)运行结果与运行情况(提示: (1)有界缓冲区可用数组实现。(2)编译命令可用:cc -lpthread -o 目标文件名源文件名(3)多线程编程方法参见附件。)3. 调试报告:1)调试记录2)自我评析和总结上机时间安排:18周一 五 08:0 12:00 指导教师签名: 年 月 日系主任(或责任教师)签名: 年 月 日用多线程同步方法解决生产者消费者问题1. 设计题目与要求1.1设计题目 解决生产者消费者(Bounded - Buffer Problem)问题1.2设计要求1) 每个生产者和消费者对有界缓冲区进行操作后,即

4、时显示有界缓冲区的全部内容、当前指针位置和生产者消费者线程的标识符。2) 生产者和消费者各有两个以上。3) 多个生产者或多个消费者之间须共享对缓冲区进行操作的函数代码。2.设计思想及系统平台2.1设计思想生产者进程与消费者进程是经典的同步互斥关系。系统创建两类进程:proceducer ()和consumer(),分别用来描述生产者和消费者的行为。生产者与消费者问题是指若干进程通过循环缓冲池区交换数据。生产者进程不断向循环缓冲池区中写入数据(即生产数据),而消费者进程不断从循环缓冲池区中读出数据(即消费数据)。循环缓冲池共有N个缓冲区,缓冲区可以暂存一个产品,任何时刻只能有一个进程可以对循环缓

5、冲池进行操作。只要缓冲区未满,生产者就可以把产品送入缓冲区;只要缓冲区未空,消费者就可以从缓冲区中取走物品。为了解决生产者和消费者问题,应该设置信号量和变量如下: full: 满缓冲区资源信号量 ,初值为0;empty:空缓冲区资源信号量 ,初值为n;in: 生产者指针,初值均为0;out: 消费者指针,均为0;mutex:缓冲区操作的互斥信号量,初值为1利用互斥信号量mutex实现诸进程对缓冲池的互斥使用,利用信号量empty和full分别表示缓冲池中空缓冲池和满缓冲区的数量。2.2系统平台及使用语言1)操作系统:Linux2)程序设计语言:C语言3)编译器:GCC3.数据结构与模块说明3.

6、1程序自定义函数1、void produce(struct sem_info * );这个函数是生产者进行的生产过程,为所有的生产者所共享。结构体指针用来接收生产者线程创建时传来的生产者的个人信息。2、void consumer(struct sem_info * );这个函数是消费者进行的生产过程,为所有的消费者所共享。结构体指针用来接收消费者线程创建时传来的消费者的个人信息。3、void setproduce(void); 这个函数是用来设置生产者的个数和他们的名字。4、void setconsumer(void);这个函数是用来设置消费者的个数和他们的名字。5、void activept

7、hread(int);这个函数是用来创建生产者线程,int型参数为生产者的个数。6、void activecthread(int);这个函数是用来创建生产者线程,int型参数为生产者的个数。7、int gettime(void);这个函数返回来一个整数,作为线程的sleep()函数的参数。8、void myscanf(void);这个函数用来获取设置生产者和消费者的个数时的整数,确保这个数字在0到MAX_BUFFER之间。3.2系统函数调用 线程Linux系统下的多线程遵循POSIX线程接口,称为pthread。编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库l

8、ibpthread.a。Linux下pthread的实现是通过系统调用clone()来实现的。clone()是Linux所特有的系统调用,它的使用方式类似fork。函数pthread_create用来创建一个线程,它的原型为:extern int pthread_create _P (pthread_t *_thread, _const pthread_attr_t *_attr,void *(*_start_routine) (void *), void *_arg);第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,第三个参数是线程运行函数的起始地址,最后一个参数是运行函数的参

9、数。第二个参数我们也设为空指针,这样将生成默认属性的线程。当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。创建线程成功后,新创建的线程则运行参数三和参数四确定的函数,原来的线程则继续运行下一行代码。函数pthread_join用来等待一个线程的结束。函数原型为:extern int pthread_join _P (pthread_t _th, void *_thread_return);第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指

10、针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:extern void pthread_exit _P (void *_retval) _attribute_ (_noreturn_);唯一的参数是函数的返回代码,只要pthread_join中的第二个参数thread_return不是NULL,这个值将被传递给 thread_return。最后要说明的是,一

11、个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。信号量信号量本质上是一个非负的整数计数器,它被用来控制对公共资源的访问。当公共资源增加时,调用函数sem_post()增加信号量。只有当信号量值大于时,才能使用公共资源,使用后,函数sem_wait()减少信号量。函数sem_trywait()和函数pthread_ mutex_trylock()起同样的作用,它是函数sem_wait()的非阻塞版本。它们都在头文件 /usr/include/semaphore.h中定义。信号量的数据类型为结构sem_t,它本质上是一个

12、长整型的数。函数sem_init()用来初始化一个信号量。它的原型为:extern int sem_init _P (sem_t *_sem, int _pshared, unsigned int _value);sem为指向信号量结构的一个指针;pshared不为时此信号量在进程间共享,否则只能为当前进程的所有线程共享;value给出了信号量的初始值。函数sem_post( sem_t *sem )用来增加信号量的值。当有线程阻塞在这个信号量上时,调用这个函数会使其中的一个线程不在阻塞,选择机制同样是由线程的调度策略决定的。函数sem_wait( sem_t *sem )被用来阻塞当前线程直

13、到信号量sem的值大于0,解除阻塞后将sem的值减一,表明公共资源经使用后减少。函数sem_trywait ( sem_t *sem )是函数sem_wait()的非阻塞版本,它直接将信号量sem的值减一。函数sem_destroy(sem_t *sem)用来释放信号量sem。3.3流程图设置生产者消费者信息创建生产者生产者生产缓存区空?缓存区可进?创建生产者生产者生产缓存区空?缓存区可进?程序结束失败否否否失败否4.源程序清单 #include #include #include #include #include #include #include #define MAX_BUFFER 2

14、0int in=0,out=0;int pnum,cnum;int sheng=1;int xiao=1;int i;char bufferMAX_BUFFER;struct sem_info pthread_t semid; char * name; *psemMAX_BUFFER,*csemMAX_BUFFER;sem_t mutex,full,empty;/生产者开始void produce(struct sem_info * rpsem) printf(第%d个生产者创建完成!.n,sheng+);while(1) int s1; sleep(gettime(); sem_wait(&

15、empty); sem_wait(&mutex); if(in19) in=0; srand(unsigned)time(NULL)+in); bufferin=rand()%100+10; printf(I am the produce ,the pid:%d ,my name is:%s ,my products is:%dn,&rpsem-semid,&rpsem-name,bufferin); printf(the in point is:%dn,in); printf(the contents of the buffer is:n); for(s1=0;s119) out=0; pr

16、intf(I am the consumer ,the PID:%d ,my name is:%s ,I will consume the products:%dn,&rcsem-semid,&rcsem-name,bufferout); bufferout=0; printf(the out point is:%dn,out); printf(After consume the buffercontents is:n); for(s2=0;s2MAX_BUFFER;s2+) printf(%d|,buffers2); out+; printf(n-消费结束-nn); sem_post(&mu

17、tex); sem_post(&empty); void setproduce() printf(请输入生产者的个数:); pnum=myscanf(); for(i=0;isemid=0; printf(请输入第%d个生产者的名字:,i+1); scanf(%s,&psemi-name); getchar(); void setconsumer() printf(请输入消费者的个数:); cnum=myscanf(); for(i=0;isemid=0; printf(请输入第%d个消费者的名字:,i+1); scanf(%s,&csemi-name); getchar(); int act

18、ivepthread(int number) int k1=0; int ret1; for(k1=0;k1semid,0,sizeof(psemk1-semid); ret1=pthread_create(&psemk1-semid,NULL,(void *)produce,psemk1); if(ret1!=0) printf(创建第%d个生产者线程失败!,k1+1); return -1; return 0;int activecthread(int number) int k2=0; int ret2; for(k2=0;k2semid,0,sizeof(csemk2-semid);

19、ret2=pthread_create(&csemk2-semid,NULL,(void *)consumer,csemk2); if(ret2!=0) printf(创建第%d个消费者线程失败!,k2+1); return -1; return 0;int gettime() int j; srand(unsigned)time(NULL); j=rand()%4+1; return j; int myscanf() int num=-99999; while(1) scanf(%d,&num); getchar(); if(num=20) printf(请输入1到20的一个整数: ); e

20、lse break; return num;int main() int key1,key2; sem_init(&mutex,0,1); sem_init(&full,0,0); sem_init(&empty,0,MAX_BUFFER); setproduce(); setconsumer(); key1=activecthread(cnum); key2=activepthread(pnum); if(key1=0&key2=0) pthread_join(psem0-semid,NULL);/等待第一个生产者线程的结束,也就是让主线程处于无限等待中,不会过早的退出 5. 编译及运行结果

21、编译命令:gcc -o pvpv.c -pthread执行命令:./pv运行结果:rootlocalhost root# gcc pv.c -o pv -pthreadrootlocalhost root# ./pv请输入生产者的个数:3请输入第1个生产者的名字:a请输入第2个生产者的名字:b请输入第3个生产者的名字:c请输入消费者的个数:2请输入第1个消费者的名字:s1请输入第2个消费者的名字:s2第1个消费者创建完成!.第2个消费者创建完成!.第1个生产者创建完成!.第2个生产者创建完成!.第3个生产者创建完成!.I am the produce ,the pid:134517488 ,m

22、y name is:a ,my products is:70the in point is:0the contents of the buffer is:70|0|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:1the 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 pr

23、oduce ,the pid:134517520 ,my name is:c ,my products is:93the in point is:2the contents 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:s1 ,I will consume the products:70the out point is:0After consume the buffercontents is:0|88|93|0

24、|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:s2 ,I will consume the products:88the out point is:1After consume the buffercontents 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

25、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|-生产结束- 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 i

26、s:c ,my products is:50the in point is:5the contents of the buffer is:0|0|93|46|70|50|0|0|0|0|0|0|0|0|0|0|0|0|0|0|-生产结束- I am the consumer ,the PID:134517536 ,my name is:s1 ,I will consume the products:93the out point is:2After consume the buffercontents is:0|0|0|46|70|50|0|0|0|0|0|0|0|0|0|0|0|0|0|0|

27、-消费结束- I am the consumer ,the PID:134517552 ,my name is:s2 ,I will consume the products:46the out point is:3After consume the buffercontents 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 bu

28、ffer 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 ,my 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 p

29、oint 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 consumer ,the PID:134517536 ,my name is:s1 ,I will consume the products:70the out point is:4After consume the buffercontents 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 ,th

30、e PID:134517552 ,my name is:s2 ,I will consume the products:50the out point is:5After consume the buffercontents 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|8

31、1|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: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 ,my name is:c ,my products is:67the in point is:11the contents o

32、f 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:134517536 ,my name is:s1 ,I will consume the products:46the out point is:6After consume the buffercontents 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:134517552 ,my n

33、ame is:s2 ,I will consume the products:37the out point is:7After consume the buffercontents is:0|0|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

34、|0|0|0|-生产结束- I am the produce ,the pid:134517504 ,my name is:b ,my products is:21the in point is:13the 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:

35、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:s1 ,I will consume the products:81the out point is:8After consume the buffercontents 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 ,I

36、 will consume the products:69the out point is:9After consume the buffercontents is:0|0|0|0|0|0|0|0|0|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|-

37、生产结束- I am the produce ,the pid:134517504 ,my name is:b ,my products is:16the in point is:16the contents of 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 ,my name is:c ,my products is:40the in point is:17the contents of the buffer is:0|0|0|0

38、|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:s1 ,I will consume the products:89the out point is:10After consume the buffercontents 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 ,my name is:s2 ,I wil

39、l consume the products:67the out point is:11After consume the buffercontents is:0|0|0|0|0|0|0|0|0|0|0|0|37|21|102|50|16|40|0|0|-消费结束- 6调试过程6.1调试工具简介VI(visual editor)进入vi,直接执行 vi文件名,即可:此刻屏幕上会出现 vi 的编辑窗口,同时 vi 会将文件复制一份至记忆体中的缓冲区 (buffer)。vi会保留在磁盘中的文件不变,而先对缓冲区的档案作编辑,编辑完成后,我们可以决定是否要取代原来旧有的文件。编译命令:gcc -o 目标文件名源文件名执行命令:./ 目标文件名6.2、调试问题分析因为生产者-消费者方案很常用,所以在构建应用程序时它可能会出现几次,这导致了代码重复。在设计过程期间多次使用了生产者-消费者方案的问题。我的解决方案就是创建 Consumer 类,其目的是:在应用程序中,消除这种代码重复 为每个生产者-消费者实例编写一个新作业队列和消费者进程来解决这个问题。按照要求,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 研究报告 > 商业贸易


经营许可证编号:宁ICP备18001539号-1