2022年实验二读者写者问题实验报告 .pdf

上传人:C****o 文档编号:33371015 上传时间:2022-08-10 格式:PDF 页数:12 大小:511.86KB
返回 下载 相关 举报
2022年实验二读者写者问题实验报告 .pdf_第1页
第1页 / 共12页
2022年实验二读者写者问题实验报告 .pdf_第2页
第2页 / 共12页
点击查看更多>>
资源描述

《2022年实验二读者写者问题实验报告 .pdf》由会员分享,可在线阅读,更多相关《2022年实验二读者写者问题实验报告 .pdf(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、实验二读者写者问题实验报告一、实验目的Windows2000/XP 提供了互斥量 (mutex)、信号量 (semapore) 、 事件(event)等三种同步对象和相应的系统调用, 用于线程的互斥与同步。 通过对读者写者问题的调试,了解 Windows2000/XP 中的同步机制。二、实验内容及实验步骤利用 Windows2000/XP 信号量机制,实现读者写者问题。在 Windows 2000 环境下,创建一个控制台进程,此进程包含n 个线程。用这 n 个线程来表示 n 个读者或写者。每个线程按相应测试数据文件 (后面有介绍)的要求进行读写操作。用信号量机制分别实现读者优先和写者优先的读者

2、-写者问题。读者-写者问题的读写操作限制(包括读者优先和写者优先):写-写互斥,即不能有两个写者同时进行写操作。读-写互斥,即不能同时有一个线程在读,而另一个线程在写。读-读允许,即可以有一个或多个读者在读。读者优先的附加限制: 如果一个读者申请进行读操作时已有另一个读者正在进行读操作,则该读者可直接开始读操作。写者优先的附加限制: 如果一个读者申请进行读操作时已有另一写者在等待访问共享资源,则该读者必须等到没有写者处于等待状态才能开始读操作。运行结果显示要求: 要求在每个线程创建、 发出读写操作申请、 开始读写操作和结果读写操作时分别显示一行提示信息,以确定所有处理都遵守相应的读写操作限制。

3、三、实验结果及分析图 2.1 选择界面第一字段为一个正整数, 表示线程序号。 第二字段表示相应线程角色, R 表示读者是,W 表示写者。第三字段为一个正数,表示读写操作的开始时间。线程创建精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 1 页,共 12 页后,延时相应时间(单位为秒)后发出对共享资源的读写申请。第四字段为一个正数,表示读写操作的持续时间。 当线程读写申请成功后, 开始对共享资源的读写操作,该操作持续相应时间后结束,并释放共享资源。下面是一个测试数据文件的例子:1 R 3 5 2 W 4 5 3 R 5 2 4 R 6 5 5 W 5

4、.1 3 测试结果如下:图 2.2 读者优先运行结果精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 2 页,共 12 页图 2.3 写者优先运行结果分析如下:将所有的读者和所有的写者分别放进两个等待队列中,当读允许时就让读者队列释放一个或多个读者,当写允许时,释放第一个写者操作。读者优先:如果没有写者正在操作,则读者不需要等待,用一个整型变量readcount 记录当前的读者数目,用于确定是否释放写者线程,(当readcout=0 时,说明所有的读者都已经读完,释放一个写者线程) , 每个 读者开始读之前都要修改readcount,为了互斥的实现对

5、readcount 的修改,需要一个互斥对象Mutex 来实现互斥。另外,为了实现写-写互斥,需要一个临界区对象write, 当写者发出写的请求时,必须先得到临界区对象的所有权。通过这种方法,可以实现读写互斥,当readcount=1 时,(即第一个读者的到来时,),读者线程也必须申请临界区对象的所有权. 当读者拥有临界区的所有权,写者都阻塞在临界区对象write 上。当写者拥有临界区对象所有权时,第一个判断完readcount=1 后,其余的读者由于等待对readcount 的判断,阻塞在 Mutex 上!写者优先:写者优先和读者优先有相同之处,不同的地方在: 一旦有一个写者到来时,应该尽快

6、让写者进行写, 如果有一个写者在等待,则新到的读者操作不能读操作,为此添加一个整型变量 writecount, 记录写者的数目,当writecount=0 时才可以释放读者进行读操作!精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 3 页,共 12 页为了实现对全局变量writecount 的互斥访问,设置了一个互斥对象Mutex3。为了实现写者优先,设置一个临界区对象read,当有写者在写或等待时,读者必须阻塞在临界区对象read 上。读者除了要一个全局变量readcount 实现操作上的互斥外,还需要一个互斥对象对阻塞在 read这一个过程实现

7、互斥,这两个互斥对象分别为mutex1 和 mutex2。附:源代码如下:#include windows.h #include #include #include #include #include #include #define READER R / 读者#define WRITER W / 写者#define INTE_PER_SEC 1000 / 每秒时钟中断数目。#define MAX_THREAD_NUM 64 / 最大线程数目#define MAX_FILE_NUM 32 / 最大数据文件数目#define MAX_STR_LEN 32 / 字符串长度int readcount

8、=0; / 读者数目int writecount=0; / 写者数目CRITICAL_SECTION RP_Write; /临界区CRITICAL_SECTION cs_Write; CRITICAL_SECTION cs_Read; struct ThreadInfo int serial; / 线程序号char entity; /线程类别(判断读者线程还是写者线程)double delay; double persist; ; / / 读者优先 -读者线程/p:读者线程信息精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 4 页,共 12 页vo

9、id RP_ReaderThread(void* p) /互斥变量HANDLE h_Mutex; h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex_for_readcount); DWORD wait_for_mutex; /等待互斥变量所有权DWORD m_delay; / 延迟时间DWORD m_persist; / 读文件持续时间int m_serial; /线程序号/从参数中获得信息m_serial=(ThreadInfo*)(p)-serial; m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_

10、SEC); m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC); Sleep(m_delay); /延迟等待printf(Reader thread %d sents the reading require.n,m_serial); / 等待互斥信号,保证对readcount 的访问、修改互斥wait_for_mutex=WaitForSingleObject(h_Mutex,-1); /读者数目增加Readcount+; if(readcount=1) /第一个读者,等待资源EnterCriticalSection(&RP_Writ

11、e); ReleaseMutex(h_Mutex); /释放互斥信号/读文件printf(Reader thread %d begins to read file.n,m_serial); Sleep(m_persist); / 退出线程printf(Reader thread %d finished reading file.n,m_serial); /等待互斥信号,保证对readcount的访问、修改互斥wait_for_mutex=WaitForSingleObject(h_Mutex,-1); /读者数目减少readcount-; if(readcount=0) /如果所有读者读完,唤

12、醒写者精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 5 页,共 12 页LeaveCriticalSection(&RP_Write); ReleaseMutex(h_Mutex); /释放互斥信号 / / 读者优先 -写者线程/p:写者线程信息void RP_WriterThread(void* p) DWORD m_delay; / 延迟时间DWORD m_persist; / 写文件持续时间int m_serial; /线程序号/从参数中获得信息m_serial=(ThreadInfo*)(p)-serial; m_delay=(DWORD

13、)(ThreadInfo*)(p)-delay*INTE_PER_SEC); m_persist=(DWORD)(ThreadInfo*)(p) -persist*INTE_PER_SEC); Sleep(m_delay); /延迟等待printf(Writer thread %d sents the writing require.n,m_serial); / 等待资源EnterCriticalSection(&RP_Write); /写文件printf(Writer thread %d begins to Write to the file.n,m_serial); Sleep(m_per

14、sist); / 退出线程printf(Writer thread %d finished Writing to the file.n,m_serial); /释放资源LeaveCriticalSection(&RP_Write); / / 读者优先处理函数/file :文件名void ReaderPriority(char* file) DWORD n_thread=0; /线程数目DWORD thread_ID; /线程 ID DWORD wait_for_all; /等待所有线程结束/互斥对象精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 6

15、 页,共 12 页HANDLE h_Mutex; h_Mutex=CreateMutex(NULL,FALSE,mutex_for_readcount); /线程对象的数组HANDLE h_ThreadMAX_THREAD_NUM; ThreadInfo thread_infoMAX_THREAD_NUM; readcount=0; / 初始化readcount InitializeCriticalSection(&RP_Write); /初始化临界区ifstream inFile; inFile.open(file); /打开文件printf(Reader Priority:nn); whi

16、le(inFile) /读入每一个读者、写者的信息inFilethread_infon_thread.serial; inFilethread_infon_thread.entity; inFilethread_infon_thread.delay; inFilethread_infon_thread+.persist; inFile.get( ); for(int i=0;iserial; m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC); m_persist=(DWORD)(ThreadInfo*)(p) -persist*INTE_P

17、ER_SEC); Sleep(m_delay); /延迟等待printf(Reader thread %d sents the reading require.n,m_serial); wait_for_mutex1= WaitForSingleObject(h_Mutex1,-1); /进入读者临界区EnterCriticalSection(&cs_Read); / 阻塞互斥对象mutex2,保证对 readcount 的访问、修改互斥wait_for_mutex2= WaitForSingleObject(h_Mutex2,-1); /修改读者数目readcount+; if(readco

18、unt=1) /如果是第一个读者,等待写者写完EnterCriticalSection(&cs_Write); ReleaseMutex(h_Mutex2); /释放互斥信号mutex2 / 让其他读者进入临界区LeaveCriticalSection(&cs_Write); ReleaseMutex(h_Mutex1); /读文件printf(Reader thread %d begins to read file.n,m_serial); Sleep(m_persist); / 退出线程printf(Reader thread %d finished reading file.n,m_se

19、rial); / 阻塞互斥对象mutex2,保证对 readcount 的访问、修改互斥精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 8 页,共 12 页wait_for_mutex2= WaitForSingleObject(h_Mutex2,-1); readcount-; if(readcount=0) / 最后一个读者,唤醒写者LeaveCriticalSection(&cs_Write); ReleaseMutex(h_Mutex2); /释放互斥信号 / / 写者优先 -写者线程/p:写者线程信息void WP_WriterThrea

20、d(void* p) DWORD m_delay; / 延迟时间DWORD m_persist; / 写文件持续时间int m_serial; /线程序号DWORD wait_for_mutex3; /互斥对象HANDLE h_Mutex3; h_Mutex3= OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex3); /从参数中获得信息m_serial=(ThreadInfo*)(p)-serial; m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC); m_persist=(DWORD)(ThreadInfo*)

21、(p)-persist*INTE_PER_SEC); Sleep(m_delay); /延迟等待printf(Writer thread %d sents the writing require.n,m_serial); / 阻塞互斥对象mutex3,保证对 writecount 的访问、修改互斥wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1); writecount+; /修改读者数目if(writecount=1) /第一个写者,等待读者读完EnterCriticalSection(&cs_Read); ReleaseMutex(h_Mut

22、ex3); /进入写者临界区EnterCriticalSection(&cs_Write); /写文件printf(Writer thread %d begins to Write to the file.n,m_serial); Sleep(m_persist); 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 9 页,共 12 页/ 退出线程printf(Writer thread %d finishing Writing to the file.n,m_serial); /离开临界区LeaveCriticalSection(&cs_Write

23、); / 阻塞互斥对象mutex3,保证对 writecount 的访问、修改互斥wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1); writecount-; /修改读者数目if(writecount=0) /写者写完,读者可以读LeaveCriticalSection(&cs_Read); ReleaseMutex(h_Mutex3); / / 写者优先处理函数/file :文件名void WriterPriority(char* file) DWORD n_thread=0; /线程数目DWORD thread_ID; /线程 ID DWO

24、RD wait_for_all; /等待所有线程结束/互斥对象HANDLE h_Mutex1; h_Mutex1=CreateMutex(NULL,FALSE,mutex1); HANDLE h_Mutex2; h_Mutex2=CreateMutex(NULL,FALSE,mutex2); HANDLE h_Mutex3; h_Mutex3=CreateMutex(NULL,FALSE,mutex3); /线程对象HANDLE h_ThreadMAX_THREAD_NUM; ThreadInfo thread_infoMAX_THREAD_NUM; readcount=0; / 初始化rea

25、dcount writecount=0; / 初始化 writecount InitializeCriticalSection(&cs_Write); /初始化临界区InitializeCriticalSection(&cs_Read); 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 10 页,共 12 页ifstream inFile; inFile.open(file); /打开文件printf(Writer Priority:nn); while(inFile) /读入每一个读者、写者的信息inFilethread_infon_thread

26、.serial; inFilethread_infon_thread.entity; inFilethread_infon_thread.delay; inFilethread_infon_thread+.persist; inFile.get( ); for(int i=0;i (int)(n_thread);i+) if (thread_infoi.entity=READER | thread_infoi.entity=R) /创建读者线程h_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thr

27、ead_infoi,0,&thread_ID); else /创建写者线程h_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_infoi,0,&thread_ID); /等待所有线程结束wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1); printf(All reader and writer have finished operating.n); / /主函数int main(int argc,char* ar

28、gv) char ch; while (true) /打印提示信息printf(*n); printf( 1:Reader Priorityn); printf( 2:Writer Priorityn); 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 11 页,共 12 页printf( 3:Exit Priorityn); printf(*n); printf(Enter your choice(1,2 or 3): ); /如果输入信息不正确,继续输入do ch=(char)_getch( ); while(ch != 1 &ch != 2 & ch != 3); system(cls); /选择 3,返回if(ch=3) return 0; /选择 1,读者优先else if(ch=1) ReaderPriority(thread.dat); /选择 2,写者优先else WriterPriority(thread.dat); /结束printf(nPress Any Key To Continue: ); _getch( ); system(cls); return 0; 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 12 页,共 12 页

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com