《2022年实验二读者写者问题实验报告.docx》由会员分享,可在线阅读,更多相关《2022年实验二读者写者问题实验报告.docx(24页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
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
4、R 5 2 4 R 6 5 5 W 5.1 3 测试结果如下:图 2.2 读者优先运行结果下面是一个测试数据名师归纳总结 - - - - - - -第 2 页,共 12 页精选学习资料 - - - - - - - - - 图 2.3 写者优先运行结果分析如下:将全部的读者和全部的写者分别放进两个等待队列中,当读答应时就让读者队列释放一个或多个读者,当写答应时,释放第一个写者操作;读者优先:假如没有写者正在操作,就读者不需要等待,用一个整型变量readcount 记录当前的读者数目,用于确定是否释放写者线程,(当readcout=0 时,说明全部的读者都已经读完,释放一个写者线程) ,每个 读者
5、开头读之前都要修改 readcount,为了互斥的实现对 readcount 的修改,需要一个互斥对象 Mutex 来实现互斥;另外,为了实现写-写互斥,需要一个临界区对象 write, 当写者发出写的恳求时,必需先得到临界区对象的全部权;通过这种方法,可以实现读写互斥,当readcount=1 时,(即第一个读者的到来时,),读者线程也必需申请临界区对象的全部权 . 当读者拥有临界区的全部权,写者都堵塞在临界区对象write 上;当写者拥有临界区对象全部权时,第一个判定完readcount=1 后,其余的读者由于等待对readcount 的判定,阻塞在 Mutex 上!写者优先:写者优先和读
6、者优先有相同之处,不同的地方在: 一旦有一个写者到来时,应当尽快让写者进行写, 假如有一个写者在等待,就新到的读者操作不能读操作,为此添加一个整型变量 writecount, 记录写者的数目,当writecount=0 时才可以释放读者进行读操作!名师归纳总结 - - - - - - -第 3 页,共 12 页精选学习资料 - - - - - - - - - 为了实现对全局变量writecount 的互斥拜访,设置了一个互斥对象Mutex3 ;为了实现写者优先,设置一个临界区对象 在临界区对象 read 上;read,当有写者在写或等待时,读者必需堵塞读者除了要一个全局变量 readcount
7、 实现操作上的互斥外,仍需要一个互斥对象对堵塞在 read 这一个过程实现互斥,这两个互斥对象分别为附:源代码如下:#include windows.h #include #include #include #include #include #include #define READER R / 读者 #define WRITER W / 写者mutex1 和 mutex2 ;#define INTE_PER_SEC 1000 / 每秒时钟中断数目;#define MAX_THREAD_NUM 64 / 最大线程数目#define MAX_FILE_NUM 32 / 最大数据文件数目#def
8、ine MAX_STR_LEN 32 / 字符串长度int readcount=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:读者线程信息名师归纳总结 - - - - - - -
9、第 4 页,共 12 页精选学习资料 - - - - - - - - - void RP_ReaderThreadvoid* p /互斥变量 HANDLE h_Mutex; h_Mutex=OpenMutexMUTEX_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=DW
10、ORDThreadInfo*p-delay*INTE_PER_SEC; m_persist=DWORDThreadInfo*p-persist*INTE_PER_SEC; Sleepm_delay; /推迟等待printfReader thread %d sents the reading require.n,m_serial; / 等待互斥信号,保证对 readcount 的拜访、修改互斥 wait_for_mutex=WaitForSingleObjecth_Mutex,-1; /读者数目增加 Readcount+; ifreadcount=1 /第一个读者,等待资源 EnterCriti
11、calSection&RP_Write; ReleaseMutexh_Mutex; /释放互斥信号/读文件 printfReader thread %d begins to read file.n,m_serial; Sleepm_persist; / 退出线程 printfReader thread %d finished reading file.n,m_serial; /等待互斥信号,保证对 readcount 的拜访、修改互斥 wait_for_mutex=WaitForSingleObjecth_Mutex,-1; /读者数目削减 readcount-; ifreadcount=0
12、/假如全部读者读完,唤醒写者名师归纳总结 - - - - - - -第 5 页,共 12 页精选学习资料 - - - - - - - - - LeaveCriticalSection&RP_Write; ReleaseMutexh_Mutex; /释放互斥信号 / / 读者优先 -写者线程 /p:写者线程信息void RP_WriterThreadvoid* p DWORD m_delay; / 推迟时间 DWORD m_persist; / 写文件连续时间 int m_serial; /线程序号 /从参数中获得信息 m_serial=ThreadInfo*p-serial; m_delay=
13、DWORDThreadInfo*p-delay*INTE_PER_SEC; m_persist=DWORDThreadInfo*p -persist*INTE_PER_SEC; Sleepm_delay; /推迟等待printfWriter thread %d sents the writing require.n,m_serial; / 等待资源 EnterCriticalSection&RP_Write; /写文件 printfWriter thread %d begins to Write to the file.n,m_serial; Sleepm_persist; / 退出线程 pr
14、intfWriter thread %d finished Writing to the file.n,m_serial; /释放资源 LeaveCriticalSection&RP_Write; / / 读者优先处理函数 /file :文件名void ReaderPrioritychar* file DWORD n_thread=0; /线程数目 DWORD thread_ID; /线程 ID DWORD wait_for_all; /等待全部线程终止 /互斥对象名师归纳总结 - - - - - - -第 6 页,共 12 页精选学习资料 - - - - - - - - - HANDLE h
15、_Mutex; h_Mutex=CreateMutexNULL,FALSE,mutex_for_readcount; /线程对象的数组 HANDLE h_ThreadMAX_THREAD_NUM; ThreadInfo thread_infoMAX_THREAD_NUM; readcount=0; / 初始化readcount InitializeCriticalSection&RP_Write; /初始化临界区ifstream inFile; inFile.openfile; /打开文件 printfReader Priority:nn; whileinFile /读入每一个读者、写者的信息
16、 inFilethread_infon_thread.serial; inFilethread_infon_thread.entity; inFilethread_infon_thread.delay; inFilethread_infon_thread+.persist; inFile.get ; forint i=0;iserial; m_delay=DWORDThreadInfo*p-delay*INTE_PER_SEC; m_persist=DWORDThreadInfo*p -persist*INTE_PER_SEC; Sleepm_delay; /推迟等待printfReader
17、thread %d sents the reading require.n,m_serial; wait_for_mutex1= WaitForSingleObjecth_Mutex1,-1; /进入读者临界区 EnterCriticalSection&cs_Read; / 堵塞互斥对象 mutex2,保证对 readcount 的拜访、修改互斥 wait_for_mutex2= WaitForSingleObjecth_Mutex2,-1; /修改读者数目 readcount+; ifreadcount=1 /假如是第一个读者,等待写者写完 EnterCriticalSection&cs_W
18、rite; ReleaseMutexh_Mutex2; /释放互斥信号mutex2 / 让其他读者进入临界区 LeaveCriticalSection&cs_Write; ReleaseMutexh_Mutex1; /读文件 printfReader thread %d begins to read file.n,m_serial; Sleepm_persist; / 退出线程 printfReader thread %d finished reading file.n,m_serial; 名师归纳总结 / 堵塞互斥对象mutex2,保证对 readcount 的拜访、修改互斥第 8 页,共
19、12 页- - - - - - -精选学习资料 - - - - - - - - - wait_for_mutex2= WaitForSingleObjecth_Mutex2,-1; readcount-; ifreadcount=0 / 最终一个读者,唤醒写者 LeaveCriticalSection&cs_Write; ReleaseMutexh_Mutex2; /释放互斥信号 / / 写者优先 -写者线程 /p:写者线程信息 void WP_WriterThreadvoid* p DWORD m_delay; / 推迟时间 DWORD m_persist; / 写文件连续时间 int m_
20、serial; /线程序号 DWORD wait_for_mutex3; /互斥对象 HANDLE h_Mutex3; h_Mutex3= OpenMutexMUTEX_ALL_ACCESS,FALSE,mutex3; /从参数中获得信息 m_serial=ThreadInfo*p-serial; m_delay=DWORDThreadInfo*p-delay*INTE_PER_SEC; m_persist=DWORDThreadInfo*p-persist*INTE_PER_SEC; Sleepm_delay; /推迟等待 printfWriter thread %d sents the w
21、riting require.n,m_serial; / 堵塞互斥对象 mutex3,保证对 writecount 的拜访、修改互斥 wait_for_mutex3= WaitForSingleObjecth_Mutex3,-1; writecount+; /修改读者数目 ifwritecount=1 /第一个写者,等待读者读完 EnterCriticalSection&cs_Read; ReleaseMutexh_Mutex3; /进入写者临界区 EnterCriticalSection&cs_Write; /写文件 printfWriter thread %d begins to Writ
22、e to the file.n,m_serial; Sleepm_persist; 名师归纳总结 - - - - - - -第 9 页,共 12 页精选学习资料 - - - - - - - - - / 退出线程 printfWriter thread %d finishing Writing to the file.n,m_serial; /离开临界区 LeaveCriticalSection&cs_Write; / 堵塞互斥对象 mutex3,保证对 writecount 的拜访、修改互斥 wait_for_mutex3= WaitForSingleObjecth_Mutex3,-1; wr
23、itecount-; /修改读者数目 ifwritecount=0 /写者写完,读者可以读 LeaveCriticalSection&cs_Read; ReleaseMutexh_Mutex3; / / 写者优先处理函数 /file :文件名void WriterPrioritychar* file DWORD n_thread=0; /线程数目 DWORD thread_ID; /线程 ID DWORD wait_for_all; /等待全部线程终止/互斥对象 HANDLE h_Mutex1; h_Mutex1=CreateMutexNULL,FALSE,mutex1; HANDLE h_M
24、utex2; h_Mutex2=CreateMutexNULL,FALSE,mutex2; HANDLE h_Mutex3; h_Mutex3=CreateMutexNULL,FALSE,mutex3; /线程对象 HANDLE h_ThreadMAX_THREAD_NUM; ThreadInfo thread_infoMAX_THREAD_NUM; readcount=0; / 初始化 readcount writecount=0; / 初始化 writecount InitializeCriticalSection&cs_Write; /初始化临界区 InitializeCriticalS
25、ection&cs_Read; 名师归纳总结 - - - - - - -第 10 页,共 12 页精选学习资料 - - - - - - - - - ifstream inFile; inFile.openfile; /打开文件 printfWriter Priority:nn; whileinFile /读入每一个读者、写者的信息 inFilethread_infon_thread.serial; inFilethread_infon_thread.entity; inFilethread_infon_thread.delay; inFilethread_infon_thread+.persi
26、st; inFile.get ; forint i=0;i intn_thread;i+ if thread_infoi.entity=READER | thread_infoi.entity=R /创建读者线程h_Threadi=CreateThreadNULL,0,LPTHREAD_START_ROUTINERP_WriterThread,&thread_infoi,0,&th read_ID; else /创建写者线程 h_Threadi=CreateThreadNULL,0,LPTHREAD_START_ROUTINEWP_WriterThread,&thread_infoi,0,&t
27、 hread_ID; /等待全部线程终止 wait_for_all=WaitForMultipleObjectsn_thread,h_Thread,TRUE,-1; printfAll reader and writer have finished operating.n; / /主函数 int mainint argc,char* argv char ch; while true /打印提示信息 printf*n; 名师归纳总结 printf 1:Reader Priorityn; 第 11 页,共 12 页printf 2:Writer Priorityn; - - - - - - -精选
28、学习资料 - - - - - - - - - printf 3:Exit Priorityn; printf*n; printfEnter your choice1,2 or 3: ; /假如输入信息不正确,连续输入 do ch=char_getch ; whilech .= 1 &ch .= 2 & ch .= 3; systemcls; /挑选 3,返回 ifch=3 return 0; /挑选 1,读者优先 else ifch=1 ReaderPrioritythread.dat; /挑选 2,写者优先 else WriterPrioritythread.dat; /终止 printfnPress Any Key To Continue: ; _getch ; systemcls; return 0; 名师归纳总结 - - - - - - -第 12 页,共 12 页