实验二--读者写者问题实验报告

上传人:汽*** 文档编号:569486569 上传时间:2024-07-29 格式:PDF 页数:14 大小:693.32KB
返回 下载 相关 举报
实验二--读者写者问题实验报告_第1页
第1页 / 共14页
实验二--读者写者问题实验报告_第2页
第2页 / 共14页
实验二--读者写者问题实验报告_第3页
第3页 / 共14页
实验二--读者写者问题实验报告_第4页
第4页 / 共14页
实验二--读者写者问题实验报告_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《实验二--读者写者问题实验报告》由会员分享,可在线阅读,更多相关《实验二--读者写者问题实验报告(14页珍藏版)》请在金锄头文库上搜索。

1、实验二实验二 -读者写者问题实验报读者写者问题实验报告告实验二实验二读者写者问题实验报告读者写者问题实验报告一、实验目的一、实验目的Windows2000/XPWindows2000/XP 提供了互斥量提供了互斥量(mutex)(mutex)、信号量、信号量(semapore)(semapore)、事件、事件(event)(event)等三种同步对象和相应的系统调用,用于线程的互斥与同步。通过对读者写者等三种同步对象和相应的系统调用,用于线程的互斥与同步。通过对读者写者问题的调试,了解问题的调试,了解 Windows2000/XPWindows2000/XP 中的同步机制。中的同步机制。二、实

2、验内容及实验步骤二、实验内容及实验步骤利用利用 Windows2000/XPWindows2000/XP 信号量机制,实现读者写者问题。信号量机制,实现读者写者问题。在在 Windows 2000Windows 2000 环境下,创建一个控制台进程,此进程包含环境下,创建一个控制台进程,此进程包含n n 个线程。用个线程。用这这 n n 个线程来表示个线程来表示 n n 个读者或写者。每个线程按相应测试数据文件(后面有介个读者或写者。每个线程按相应测试数据文件(后面有介绍)的要求进行读写操作。用信号量机制分别实现读者优先和写者优先的读者绍)的要求进行读写操作。用信号量机制分别实现读者优先和写者

3、优先的读者 - -写者问题。写者问题。读者读者- -写者问题的读写操作限制(包括读者优先和写者优先)写者问题的读写操作限制(包括读者优先和写者优先) :写写- -写互斥,即不能有两个写者同时进行写操作。写互斥,即不能有两个写者同时进行写操作。读读- -写互斥,即不能同时有一个线程在读,而另一个线程在写。写互斥,即不能同时有一个线程在读,而另一个线程在写。读读- -读允许,即可以有一个或多个读者在读。读允许,即可以有一个或多个读者在读。读者优先的附加限制:如果一个读者申请进行读操作时已有另一个读者正读者优先的附加限制:如果一个读者申请进行读操作时已有另一个读者正在进行读操作,则该读者可直接开始读

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

5、。三、实验结果及分析三、实验结果及分析图图 2.12.1选择界面选择界面第一字段为一个正整数,表示线程序号。第二字段表示相应线程角色,第一字段为一个正整数,表示线程序号。第二字段表示相应线程角色,R R 表示表示读者是,读者是,W W 表示写者。第三字段为一个正数,表示读写操作的开始时间。线程表示写者。第三字段为一个正数,表示读写操作的开始时间。线程创建后,延时相应时间(单位为秒)后发出对共享资源的读写申请。第四字段创建后,延时相应时间(单位为秒)后发出对共享资源的读写申请。第四字段为一个正数,表示读写操作的持续时间。当线程读写申请成功后,开始对共享为一个正数,表示读写操作的持续时间。当线程读

6、写申请成功后,开始对共享资源的读写操作,该操作持续相应时间后结束,并释放共享资源。资源的读写操作,该操作持续相应时间后结束,并释放共享资源。 下面是一个下面是一个测试数据文件的例子:测试数据文件的例子:1 R 3 51 R 3 52 W 4 52 W 4 53 R 5 23 R 5 24 R 6 54 R 6 55 W 5.1 35 W 5.1 3测试结果如下:测试结果如下:图图 2.22.2读者读者优先运行结果优先运行结果图图 2.32.3写者优先写者优先运行结果运行结果分析如下:分析如下:将所有的读者和所有的写者分别放进两个等将所有的读者和所有的写者分别放进两个等待队列中,待队列中, 当读

7、允许时就让读者队列释放一个或当读允许时就让读者队列释放一个或多个读者,当写允许时,释放第一个写者操作。多个读者,当写允许时,释放第一个写者操作。读者优先:读者优先:如果没有写者正在操作,如果没有写者正在操作, 则读者不需要等待,则读者不需要等待,用一个整型变量用一个整型变量 readcountreadcount 记录当前的读者数记录当前的读者数目,目, 用于确定是否释放写者线程,用于确定是否释放写者线程,(当当 readcout=0readcout=0时,时,说明所有的读者都已经读完,说明所有的读者都已经读完,释放一个写者释放一个写者线线 程程 ) , 每每 个个读读 者者 开开 始始 读读

8、之之 前前 都都 要要 修修 改改readcount,readcount,为了互斥的实现对为了互斥的实现对readcountreadcount 的修的修改,需要一个互斥对象改,需要一个互斥对象 MutexMutex 来实现互斥。来实现互斥。另外,为了实现写另外,为了实现写- -写互斥,需要一个临界写互斥,需要一个临界区对象区对象 write,write,当写者发出写的请求时,当写者发出写的请求时, 必须先得必须先得到临界区对象的所有权。到临界区对象的所有权。通过这种方法,通过这种方法,可以实可以实现读写互斥,当现读写互斥,当 readcount=1readcount=1 时,时, (即第一个读

9、(即第一个读者的到来时,者的到来时, ) , 读者线程也必须申请临界区对象读者线程也必须申请临界区对象的所有权的所有权. .当读者拥有临界区的所有权,当读者拥有临界区的所有权, 写者都阻塞在写者都阻塞在临界区对象临界区对象 writewrite 上。当写者拥有临界区对象所上。当写者拥有临界区对象所有权时,第一个判断完有权时,第一个判断完 readcount=1readcount=1 后,其余后,其余的读者由于等待对的读者由于等待对 readcountreadcount 的判断,阻塞在的判断,阻塞在MutexMutex 上!上!写者优先:写者优先:写者优先和读者优先有相同之处,写者优先和读者优先

10、有相同之处, 不同的地不同的地方在:方在:一旦有一个写者到来时,一旦有一个写者到来时,应该尽快让写者应该尽快让写者进行写,进行写,如果有一个写者在等待,如果有一个写者在等待,则新到的读者则新到的读者操操作作不不能能读读操操作作,为为此此添添加加一一个个整整型型变变量量writecount,writecount,记录写者的数目,记录写者的数目, 当当 writecount=0writecount=0 时时才可以释放读者进行读操作!才可以释放读者进行读操作!为了实现对全局变量为了实现对全局变量 writecountwritecount 的互斥访的互斥访问,设置了一个互斥对象问,设置了一个互斥对象

11、Mutex3Mutex3。为了实现写者优先,设置一个临界区对象为了实现写者优先,设置一个临界区对象read,read,当有写者在写或等待时,当有写者在写或等待时, 读者必须阻塞在临读者必须阻塞在临界区对象界区对象 readread 上。上。读者除了要一个全局变量读者除了要一个全局变量 readcountreadcount实现操实现操作上的互斥外,还需要一个互斥对象对阻塞在作上的互斥外,还需要一个互斥对象对阻塞在readread 这一个过程实现互斥,这一个过程实现互斥, 这两个互斥对象分别这两个互斥对象分别为为 mutex1mutex1 和和 mutex2mutex2。附:附:源代码如下:源代码

12、如下:#include windows.h#include windows.h#include #include #include #include #include #include #include #include #include #include #include #include #define READER R#define READER R/ / 读者读者#define WRITER W#define WRITER W/ / 写者写者#define INTE_PER_SEC 1000#define INTE_PER_SEC 1000/ / 每秒时钟中断数目。每秒时钟中断数目。#

13、define MAX_THREAD_NUM 64#define MAX_THREAD_NUM 64/ / 最大线程数目最大线程数目#define MAX_FILE_NUM 32#define MAX_FILE_NUM 32/ / 最大数据文件数目最大数据文件数目#define MAX_STR_LEN 32#define MAX_STR_LEN 32/ / 字符串长度字符串长度int readcount=0;int readcount=0;/ / 读者数目读者数目int writecount=0;int writecount=0;/ / 写者数目写者数目CRITICAL_SECTION RP_W

14、rite;CRITICAL_SECTION RP_Write;/ /临界区临界区CRITICAL_SECTION cs_Write;CRITICAL_SECTION cs_Write;CRITICAL_SECTION cs_Read;CRITICAL_SECTION cs_Read;structstructThreadInfoThreadInfo intintserial;serial;/ / 线程序号线程序号charcharentity;entity;/ /线程类别(判断读者线程还是写者线程)线程类别(判断读者线程还是写者线程)double delay;double delay;double

15、 persist;double persist; ;/ / 读者优先读者优先- -读者线程读者线程/p/p:读者线程信息:读者线程信息void RP_ReaderThread(void* p)void RP_ReaderThread(void* p) / /互斥变量互斥变量HANDLE h_Mutex;HANDLE h_Mutex;h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex_for_readcount);h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex_for_readcount);DWORD wai

16、t_for_mutex;DWORD wait_for_mutex;/ /等待互斥变量所有权等待互斥变量所有权DWORD m_delay;DWORD m_delay;/ / 延迟时间延迟时间DWORD m_persist;DWORD m_persist;/ / 读文件持续时间读文件持续时间intintm_serial;m_serial;/ /线程序号线程序号/ /从参数中获得信息从参数中获得信息m_serial=(ThreadInfo*)(p)-serial;m_serial=(ThreadInfo*)(p)-serial;m_delay=(DWORD)(ThreadInfo*)(p)-dela

17、y*INTE_PER_SEC);m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC);Sleep(m_delay);Sleep(m_delay);/ /延迟等待延迟等待printf(Reader thread %d sents the reading require.n,m_serial);printf(Reader thr

18、ead %d sents the reading require.n,m_serial);/ / 等待互斥信号,保证对等待互斥信号,保证对 readcountreadcount 的访问、修改互斥的访问、修改互斥wait_for_mutex=WaitForSingleObject(h_Mutex,-1);wait_for_mutex=WaitForSingleObject(h_Mutex,-1);/ /读者数目增加读者数目增加Readcount+;Readcount+;if(readcount=1)if(readcount=1) / /第一个读者,等待资源第一个读者,等待资源EnterCriti

19、calSection(&RP_Write);EnterCriticalSection(&RP_Write); ReleaseMutex(h_Mutex);ReleaseMutex(h_Mutex);/ /释放互斥信号释放互斥信号/ /读文件读文件printf(Reader thread %d begins to read file.n,m_serial);printf(Reader thread %d begins to read file.n,m_serial);Sleep(m_persist);Sleep(m_persist);/ / 退出线程退出线程printf(Reader threa

20、d %d finished reading file.n,m_serial);printf(Reader thread %d finished reading file.n,m_serial);/ /等待互斥信号,保证对等待互斥信号,保证对 readcountreadcount 的访问、修改互斥的访问、修改互斥wait_for_mutex=WaitForSingleObject(h_Mutex,-1);wait_for_mutex=WaitForSingleObject(h_Mutex,-1);/ /读者数目减少读者数目减少readcount-;readcount-;if(readcount=

21、0)if(readcount=0) / /如果所有读者读完,唤醒写者如果所有读者读完,唤醒写者LeaveCriticalSection(&RP_Write);LeaveCriticalSection(&RP_Write); ReleaseMutex(h_Mutex);ReleaseMutex(h_Mutex);/ /释放互斥信号释放互斥信号 / / 读者优先读者优先- -写者线程写者线程/p/p:写者线程信息:写者线程信息void RP_WriterThread(void* p)void RP_WriterThread(void* p) DWORD m_delay;DWORD m_delay;

22、/ / 延迟时间延迟时间DWORD m_persist;DWORD m_persist;/ / 写文件持续时间写文件持续时间intintm_serial;m_serial;/ /线程序号线程序号/ /从参数中获得信息从参数中获得信息m_serial=(ThreadInfo*)(p)-serial;m_serial=(ThreadInfo*)(p)-serial;m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_persist=(DWO

23、RD)(ThreadInfo*)(p) -persist*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p) -persist*INTE_PER_SEC);Sleep(m_delay);Sleep(m_delay);/ /延迟等待延迟等待printf(Writer thread %d sents the writing require.n,m_serial);printf(Writer thread %d sents the writing require.n,m_serial);/ / 等待资源等待资源EnterCriticalSection(&R

24、P_Write);EnterCriticalSection(&RP_Write);/ /写文件写文件printf(Writer thread %d begins to Write to the file.n,m_serial);printf(Writer thread %d begins to Write to the file.n,m_serial);Sleep(m_persist);Sleep(m_persist);/ / 退出线程退出线程printf(Writer thread %d finished Writing to the file.n,m_serial);printf(Writ

25、er thread %d finished Writing to the file.n,m_serial);/ /释放资源释放资源LeaveCriticalSection(&RP_Write);LeaveCriticalSection(&RP_Write); / / 读者优先处理函数读者优先处理函数/file/file:文件名:文件名void ReaderPriority(char* file)void ReaderPriority(char* file) DWORD n_thread=0;DWORD n_thread=0;/ /线程数目线程数目DWORD thread_ID;DWORD th

26、read_ID;/ /线程线程 IDIDDWORD wait_for_all;DWORD wait_for_all;/ /等待所有线程结束等待所有线程结束/ /互斥对象互斥对象HANDLE h_Mutex;HANDLE h_Mutex;h_Mutex=CreateMutex(NULL,FALSE,mutex_for_readcount);h_Mutex=CreateMutex(NULL,FALSE,mutex_for_readcount);/ /线程对象的数组线程对象的数组HANDLE h_ThreadMAX_THREAD_NUM;HANDLE h_ThreadMAX_THREAD_NUM;T

27、hreadInfo thread_infoMAX_THREAD_NUM;ThreadInfo thread_infoMAX_THREAD_NUM;readcount=0;readcount=0;/ / 初始化初始化 readcountreadcountInitializeCriticalSection(&RP_Write);InitializeCriticalSection(&RP_Write);/ /初始化临界区初始化临界区ifstream inFile;ifstream inFile;inFile.open(file);inFile.open(file);/ /打开文件打开文件printf

28、(Reader Priority:nn);printf(Reader Priority:nn);while(inFile)while(inFile) / /读入每一个读者、写者的信息读入每一个读者、写者的信息inFilethread_infon_thread.serial;inFilethread_infon_thread.serial;inFilethread_infon_thread.entity;inFilethread_infon_thread.entity;inFilethread_infon_thread.delay;inFilethread_infon_thread.delay;

29、inFilethread_infon_thread+.persist;inFilethread_infon_thread+.persist;inFile.get( );inFile.get( ); for(int i=0;i (int)(n_thread);i+)for(int i=0;iserial;m_serial=(ThreadInfo*)(p)-serial;m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_persist=(

30、DWORD)(ThreadInfo*)(p) -persist*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p) -persist*INTE_PER_SEC);Sleep(m_delay);Sleep(m_delay);/ /延迟等待延迟等待printf(Reader thread %d sents the reading require.n,m_serial);printf(Reader thread %d sents the reading require.n,m_serial);wait_for_mutex1= WaitForSingleOb

31、ject(h_Mutex1,-1);wait_for_mutex1= WaitForSingleObject(h_Mutex1,-1);/ /进入读者临界区进入读者临界区EnterCriticalSection(&cs_Read);EnterCriticalSection(&cs_Read);/ / 阻塞互斥对象阻塞互斥对象 mutex2mutex2,保证对,保证对 readcountreadcount 的访问、修改互斥的访问、修改互斥wait_for_mutex2= WaitForSingleObject(h_Mutex2,-1);wait_for_mutex2= WaitForSingle

32、Object(h_Mutex2,-1);/ /修改读者数目修改读者数目readcount+;readcount+;if(readcount=1)if(readcount=1) / /如果是第一个读者,等待写者写完如果是第一个读者,等待写者写完EnterCriticalSection(&cs_Write);EnterCriticalSection(&cs_Write); ReleaseMutex(h_Mutex2);ReleaseMutex(h_Mutex2);/ /释放互斥信号释放互斥信号 mutex2mutex2/ / 让其他读者进入临界区让其他读者进入临界区LeaveCriticalSec

33、tion(&cs_Write);LeaveCriticalSection(&cs_Write);ReleaseMutex(h_Mutex1);ReleaseMutex(h_Mutex1);/ /读文件读文件printf(Reader thread %d begins to read file.n,m_serial);printf(Reader thread %d begins to read file.n,m_serial);Sleep(m_persist);Sleep(m_persist);/ / 退出线程退出线程printf(Reader thread %d finished readin

34、g file.n,m_serial);printf(Reader thread %d finished reading file.n,m_serial);/ / 阻塞互斥对象阻塞互斥对象 mutex2mutex2,保证对,保证对 readcountreadcount 的访问、修改互斥的访问、修改互斥wait_for_mutex2= WaitForSingleObject(h_Mutex2,-1);wait_for_mutex2= WaitForSingleObject(h_Mutex2,-1);readcount-;readcount-;if(readcount=0)if(readcount=

35、0) / / 最后一个读者,唤醒写者最后一个读者,唤醒写者LeaveCriticalSection(&cs_Write);LeaveCriticalSection(&cs_Write); ReleaseMutex(h_Mutex2);ReleaseMutex(h_Mutex2);/ /释放互斥信号释放互斥信号 / / 写者优先写者优先- -写者线程写者线程/p/p:写者线程信息:写者线程信息void WP_WriterThread(void* p)void WP_WriterThread(void* p) DWORD m_delay;DWORD m_delay;/ / 延迟时间延迟时间DWOR

36、D m_persist;DWORD m_persist;/ / 写文件持续时间写文件持续时间intintm_serial;m_serial;/ /线程序号线程序号DWORD wait_for_mutex3;DWORD wait_for_mutex3;/ /互斥对象互斥对象HANDLE h_Mutex3;HANDLE h_Mutex3;h_Mutex3= OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex3);h_Mutex3= OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex3);/ /从参数中获得信息从参数中获得信息m_serial=(T

37、hreadInfo*)(p)-serial;m_serial=(ThreadInfo*)(p)-serial;m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC);Sleep(m_delay);Sleep(m_delay)

38、;/ /延迟等待延迟等待printf(Writer thread %d sents the writing require.n,m_serial);printf(Writer thread %d sents the writing require.n,m_serial);/ / 阻塞互斥对象阻塞互斥对象 mutex3mutex3,保证对,保证对 writecountwritecount 的访问、修改互斥的访问、修改互斥wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1);wait_for_mutex3= WaitForSingleObject(h_

39、Mutex3,-1);writecount+;writecount+;/ /修改读者数目修改读者数目if(writecount=1)if(writecount=1) / /第一个写者,等待读者读完第一个写者,等待读者读完EnterCriticalSection(&cs_Read);EnterCriticalSection(&cs_Read); ReleaseMutex(h_Mutex3);ReleaseMutex(h_Mutex3);/ /进入写者临界区进入写者临界区EnterCriticalSection(&cs_Write);EnterCriticalSection(&cs_Write);

40、/ /写文件写文件printf(Writer thread %d begins to Write to the file.n,m_serial);printf(Writer thread %d begins to Write to the file.n,m_serial);Sleep(m_persist);Sleep(m_persist);/ / 退出线程退出线程printf(Writer thread %d finishing Writing to the file.n,m_serial);printf(Writer thread %d finishing Writing to the fi

41、le.n,m_serial);/ /离开临界区离开临界区LeaveCriticalSection(&cs_Write);LeaveCriticalSection(&cs_Write);/ / 阻塞互斥对象阻塞互斥对象 mutex3mutex3,保证对,保证对 writecountwritecount 的访问、修改互斥的访问、修改互斥wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1);wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1);writecount-;writecount-;/ /修改读者数目

42、修改读者数目if(writecount=0)if(writecount=0) / /写者写完,读者可以读写者写完,读者可以读LeaveCriticalSection(&cs_Read);LeaveCriticalSection(&cs_Read); ReleaseMutex(h_Mutex3);ReleaseMutex(h_Mutex3); / / 写者优先处理函数写者优先处理函数/file/file:文件名:文件名void WriterPriority(char* file)void WriterPriority(char* file) DWORD n_thread=0;DWORD n_th

43、read=0;/ /线程数目线程数目DWORD thread_ID;DWORD thread_ID;/ /线程线程 IDIDDWORD wait_for_all;DWORD wait_for_all;/ /等待所有线程结束等待所有线程结束/ /互斥对象互斥对象HANDLE h_Mutex1;HANDLE h_Mutex1;h_Mutex1=CreateMutex(NULL,FALSE,mutex1);h_Mutex1=CreateMutex(NULL,FALSE,mutex1);HANDLE h_Mutex2;HANDLE h_Mutex2;h_Mutex2=CreateMutex(NULL,

44、FALSE,mutex2);h_Mutex2=CreateMutex(NULL,FALSE,mutex2);HANDLE h_Mutex3;HANDLE h_Mutex3;h_Mutex3=CreateMutex(NULL,FALSE,mutex3);h_Mutex3=CreateMutex(NULL,FALSE,mutex3);/ /线程对象线程对象HANDLE h_ThreadMAX_THREAD_NUM;HANDLE h_ThreadMAX_THREAD_NUM;ThreadInfo thread_infoMAX_THREAD_NUM;ThreadInfo thread_infoMAX_

45、THREAD_NUM;readcount=0;readcount=0;/ / 初始化初始化 readcountreadcountwritecount=0;writecount=0;/ / 初始化初始化 writecountwritecountInitializeCriticalSection(&cs_Write);InitializeCriticalSection(&cs_Write);/ /初始化临界区初始化临界区InitializeCriticalSection(&cs_Read);InitializeCriticalSection(&cs_Read);ifstream inFile;if

46、stream inFile;inFile.open(file);inFile.open(file);/ /打开文件打开文件printf(Writer Priority:nn);printf(Writer Priority:nn);while(inFile)while(inFile) / /读入每一个读者、写者的信息读入每一个读者、写者的信息inFilethread_infon_thread.serial;inFilethread_infon_thread.serial;inFilethread_infon_thread.entity;inFilethread_infon_thread.enti

47、ty;inFilethread_infon_thread.delay;inFilethread_infon_thread.delay;inFilethread_infon_thread+.persist;inFilethread_infon_thread+.persist;inFile.get( );inFile.get( ); for(int i=0;i (int)(n_thread);i+)for(int i=0;i (int)(n_thread);i+) if (thread_infoi.entity=READER | thread_infoi.entity=R)if (thread_i

48、nfoi.entity=READER | thread_infoi.entity=R) / /创建读者线程创建读者线程h_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thread_infoh_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thread_infoi,0,&thread_ID);i,0,&thread_ID); elseelse/ /创建写者线程创建写者线程h_Threadi=CreateT

49、hread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_infoh_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_infoi,0,&thread_ID);i,0,&thread_ID); / /等待所有线程结束等待所有线程结束wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);wait_for_all=WaitForMultipleObject

50、s(n_thread,h_Thread,TRUE,-1);printf(All reader and writer have finished operating.n);printf(All reader and writer have finished operating.n); / /主函数主函数int main(int argc,char* argv)int main(int argc,char* argv) char ch;char ch;while (true)while (true) / /打印提示信息打印提示信息printf(*n);printf(*n);printf(print

51、f(1:Reader Priorityn);1:Reader Priorityn);printf(printf(2:Writer Priorityn);2:Writer Priorityn);printf(printf(3:Exit Priorityn);3:Exit Priorityn);printf(*n);printf(*n);printf(Enter your choice(1,2 or 3): );printf(Enter your choice(1,2 or 3): );/ /如果输入信息不正确,继续输入如果输入信息不正确,继续输入dodoch=(char)_getch( );ch

52、=(char)_getch( );while(ch != 1 &ch != 2 & ch != 3);while(ch != 1 &ch != 2 & ch != 3);system(cls);system(cls);/ /选择选择 3 3,返回,返回if(ch=3)if(ch=3)return 0;return 0;/ /选择选择 1 1,读者优先,读者优先else if(ch=1)else if(ch=1)ReaderPriority(thread.dat);ReaderPriority(thread.dat);/ /选择选择 2 2,写者优先,写者优先elseelseWriterPriority(thread.dat);WriterPriority(thread.dat);/ /结束结束printf(nPress Any Key To Continue: );printf(nPress Any Key To Continue: );

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号