生产者-消费者模型的多线程实验指导

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

一、预备知识有C语言基础掌握在Linux下常用编辑器的使用掌握Linux下的程序编译过程学习pthread库函数的使用掌握共享锁和信号量的使用方法掌握make和makefile工具编译程序二、实验设备及工具硬件:PC机Pentium500以上,硬盘40G以上,内存大于128M。软件:PC机操作系统REDHATLINUX9.0三、实验流程生产者写入缓冲区和消费者从缓冲区读数的具体流程,生产者首先要获得互斥锁,并且判断写指针+1后是否等于读指针,如果相等则进入等待状态,等候条件变量notfull;如果不等则向缓冲区中写一个整数,并且设置条件变量为notempty,最后释放互斥锁。消费者线程与生产者线程类似。流程图如下:生产者-消费者实验源代码结构流程四、实验源代码:#includestdio.h#includestdlib.h#includetime.h#includepthread.h#defineBUFFER_SIZE16/*设置一个整数的圆形缓冲区*/structprodcons{intbuffer[BUFFER_SIZE];/*缓冲区数组*/pthread_mutex_tlock;/*互斥锁*/intreadpos,writepos;/*读写的位置*/*/pthread_cond_tnotempty;/*缓冲区非空信号*/pthread_cond_tnotfull;/*缓冲区非满信号*/};/*--------------------------------------------------------*//*初始化缓冲区*/voidinit(structprodcons*b){pthread_mutex_init(&b-lock,NULL);pthread_cond_init(&b-notempty,NULL);pthread_cond_init(&b-notfull,NULL);b-readpos=0;b-writepos=0;}/*--------------------------------------------------------*//*向缓冲区中写入一个整数*/voidput(structprodcons*b,intdata){pthread_mutex_lock(&b-lock);/*获取互斥锁*//*等待缓冲区非满*/while((b-writepos+1)%BUFFER_SIZE==b-readpos){printf(waitfornotfull\n);pthread_cond_wait(&b-notfull,&b-lock);/*等待状态变量b-notfull,不满则跳出阻塞*/}/*写数据并且指针前移*/b-buffer[b-writepos]=data;b-writepos++;if(b-writepos=BUFFER_SIZE)b-writepos=0;/*设置缓冲区非空信号*/pthread_cond_signal(&b-notempty);/*设置状态变量*/pthread_mutex_unlock(&b-lock);/*//释放互斥锁*/}/*--------------------------------------------------------*//*从缓冲区中读出一个整数*/intget(structprodcons*b){intdata;pthread_mutex_lock(&b-lock);/*获取互斥锁*//*等待缓冲区非空*/while(b-writepos==b-readpos){printf(waitfornotempty\n);pthread_cond_wait(&b-notempty,&b-lock);/*等待状态变量b-notempty,不空则跳出阻塞。否则无数据可读*/}/*读数据并且指针前移*/data=b-buffer[b-readpos];b-readpos++;if(b-readpos=BUFFER_SIZE)b-readpos=0;/*设置缓冲区非满信号*/pthread_cond_signal(&b-notfull);/*设置状态变量*/pthread_mutex_unlock(&b-lock);/*释放互斥锁*/returndata;}/*--------------------------------------------------------*/#defineOVER(-1)structprodconsbuffer;/*--------------------------------------------------------*/void*producer(void*data){intn;for(n=0;n1000;n++){printf(put--%d\n,n);put(&buffer,n);}put(&buffer,OVER);printf(producerstopped!\n);returnNULL;}/*--------------------------------------------------------*/void*consumer(void*data){intd;while(1){d=get(&buffer);if(d==OVER)break;printf(%d--get\n,d);}printf(consumerstopped!\n);returnNULL;}/*--------------------------------------------------------*/intmain(void){pthread_tth_a,th_b;void*retval;init(&buffer);pthread_create(&th_a,NULL,producer,0);pthread_create(&th_b,NULL,consumer,0);/*等待生产者和消费者结束*/pthread_join(th_a,&retval);pthread_join(th_b,&retval);return0;}五、线程API说明:在程序的代码中大量的使用了线程函数,如pthread_cond_signal、pthread_mutex_init、pthread_mutex_lock等。下面简单介绍,详细的说明请查阅资料。线程创建函数:intpthread_create(pthread_t*thread_id,__constpthread_attr_t*__attr,void*(*__start_routine)(void*),void*__restrict__arg)获得父进程ID:pthread_tpthread_self(void)测试两个线程号是否相同:intpthread_equal(pthread_t__thread1,pthread_t__thread2)线程退出:voidpthread_exit(void*__retval)等待指定的线程结束:intpthread_join(pthread_t__th,void**__thread_return)互斥量初始化:pthread_mutex_init(pthread_mutex_t*,__constpthread_mutexattr_t*)销毁互斥量:intpthread_mutex_destroy(pthread_mutex_t*__mutex)锁定互斥量(阻塞)::intpthread_mutex_lock(pthread_mutex_t*__mutex)解锁互斥量:intpthread_mutex_unlock(pthread_mutex_t*__mutex)唤醒线程等待条件变量:intpthread_cond_signal(pthread_cond_t*__cond)等待条件变量(阻塞)::intpthread_cond_wait(pthread_cond_t*__restrict__cond,pthread_mutex_t*__restrict__mutex)六、实验步骤编辑源码,得到pthread.c使用gcc编译(或者使用make工具)运行七、运行结果:waitfornotemptyput--994put--995put--996put--997put--998put--999producerstopped!993--get994--get995--get996--get997--get998--get999--getconsumerstopped!

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功