实验二读者写者问题实验报告.docx

上传人:b****7 文档编号:10503834 上传时间:2023-02-17 格式:DOCX 页数:16 大小:17.92KB
下载 相关 举报
实验二读者写者问题实验报告.docx_第1页
第1页 / 共16页
实验二读者写者问题实验报告.docx_第2页
第2页 / 共16页
实验二读者写者问题实验报告.docx_第3页
第3页 / 共16页
实验二读者写者问题实验报告.docx_第4页
第4页 / 共16页
实验二读者写者问题实验报告.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

实验二读者写者问题实验报告.docx

《实验二读者写者问题实验报告.docx》由会员分享,可在线阅读,更多相关《实验二读者写者问题实验报告.docx(16页珍藏版)》请在冰豆网上搜索。

实验二读者写者问题实验报告.docx

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

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

1、实验目的

Windows2000/XP提供了互斥量(mutex)、信号量(semapore)、事件(event)等三种同步对象和相应的系统调用,用于线程的互斥与同步。

通过对读者写者问题的调试,了解Windows2000/XP中的同步机制。

二、实验内容及实验步骤

利用Windows2000/XP信号量机制,实现读者写者问题。

在Windows2000环境下,创建一个控制台进程,此进程包含n个线程。

用这n个线程来表示n个读者或写者。

每个线程按相应测试数据文件(后面有介绍)的要求进行读写操作。

用信号量机制分别实现读者优先和写者优先的读者-写者问题。

读者-写者问题的读写操作限制(包括读者优先和写者优先):

写-写互斥,即不能有两个写者同时进行写操作。

读-写互斥,即不能同时有一个线程在读,而另一个线程在写。

读-读允许,即可以有一个或多个读者在读。

读者优先的附加限制:

如果一个读者申请进行读操作时已有另一个读者正在进行读操作,则该读者可直接开始读操作。

写者优先的附加限制:

如果一个读者申请进行读操作时已有另一写者在等待访问共享资源,则该读者必须等到没有写者处于等待状态才能开始读操作。

运行结果显示要求:

要求在每个线程创建、发出读写操作申请、开始读写操作和结果读写操作时分别显示一行提示信息,以确定所有处理都遵守相应的读写操作限制。

三、实验结果及分析

图2.1选择界面

第一字段为一个正整数,表示线程序号。

第二字段表示相应线程角色,R表示读者是,W表示写者。

第三字段为一个正数,表示读写操作的开始时间。

线程创建后,延时相应时间(单位为秒)后发出对共享资源的读写申请。

第四字段为一个正数,表示读写操作的持续时间。

当线程读写申请成功后,开始对共享资源的读写操作,该操作持续相应时间后结束,并释放共享资源。

下面是一个测试数据文件的例子:

1R35

2W45

3R52

4R65

5W5.13

测试结果如下:

图2.2读者优先运行结果

图2.3写者优先运行结果

分析如下:

将所有的读者和所有的写者分别放进两个等待队列中,当读允许时就让读者队列释放一个或多个读者,当写允许时,释放第一个写者操作。

读者优先:

如果没有写者正在操作,则读者不需要等待,用一个整型变量readcount记录当前的读者数目,用于确定是否释放写者线程,(当readcout=0时,说明所有的读者都已经读完,释放一个写者线程),每个读者开始读之前都要修改readcount,为了互斥的实现对readcount的修改,需要一个互斥对象Mutex来实现互斥。

另外,为了实现写-写互斥,需要一个临界区对象write,当写者发出写的请求时,必须先得到临界区对象的所有权。

通过这种方法,可以实现读写互斥,当readcount=1时,(即第一个读者的到来时,),读者线程也必须申请临界区对象的所有权.

当读者拥有临界区的所有权,写者都阻塞在临界区对象write上。

当写者拥有临界区对象所有权时,第一个判断完readcount==1后,其余的读者由于等待对readcount的判断,阻塞在Mutex上!

写者优先:

写者优先和读者优先有相同之处,不同的地方在:

一旦有一个写者到来时,应该尽快让写者进行写,如果有一个写者在等待,则新到的读者操作不能读操作,为此添加一个整型变量writecount,记录写者的数目,当writecount=0时才可以释放读者进行读操作!

为了实现对全局变量writecount的互斥访问,设置了一个互斥对象Mutex3。

为了实现写者优先,设置一个临界区对象read,当有写者在写或等待时,读者必须阻塞在临界区对象read上。

读者除了要一个全局变量readcount实现操作上的互斥外,还需要一个互斥对象对阻塞在read这一个过程实现互斥,这两个互斥对象分别为mutex1和mutex2。

附:

源代码如下:

#include"windows.h"

#include

#include

#include

#include

#include

#include

#defineREADER'R'//读者

#defineWRITER'W'//写者

#defineINTE_PER_SEC1000//每秒时钟中断数目。

#defineMAX_THREAD_NUM64//最大线程数目

#defineMAX_FILE_NUM32//最大数据文件数目

#defineMAX_STR_LEN32//字符串长度

intreadcount=0;//读者数目

intwritecount=0;//写者数目

CRITICAL_SECTIONRP_Write;//临界区

CRITICAL_SECTIONcs_Write;

CRITICAL_SECTIONcs_Read;

structThreadInfo

{

intserial;//线程序号

charentity;//线程类别(判断读者线程还是写者线程)

doubledelay;

doublepersist;

};

///////////////////////////////////////////////////////////////////////////

//读者优先--读者线程

//p:

读者线程信息

voidRP_ReaderThread(void*p)

{

//互斥变量

HANDLEh_Mutex;

h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");

DWORDwait_for_mutex;//等待互斥变量所有权

DWORDm_delay;//延迟时间

DWORDm_persist;//读文件持续时间

intm_serial;//线程序号

//从参数中获得信息

m_serial=((ThreadInfo*)(p))->serial;

m_delay=(DWORD)(((ThreadInfo*)(p))->delay*INTE_PER_SEC);

m_persist=(DWORD)(((ThreadInfo*)(p))->persist*INTE_PER_SEC);

Sleep(m_delay);//延迟等待

printf("Readerthread%dsentsthereadingrequire.\n",m_serial);

//等待互斥信号,保证对readcount的访问、修改互斥

wait_for_mutex=WaitForSingleObject(h_Mutex,-1);

//读者数目增加

Readcount++;

if(readcount==1)

{

//第一个读者,等待资源

EnterCriticalSection(&RP_Write);

}

ReleaseMutex(h_Mutex);//释放互斥信号

//读文件

printf("Readerthread%dbeginstoreadfile.\n",m_serial);

Sleep(m_persist);

//退出线程

printf("Readerthread%dfinishedreadingfile.\n",m_serial);

//等待互斥信号,保证对readcount的访问、修改互斥

wait_for_mutex=WaitForSingleObject(h_Mutex,-1);

//读者数目减少

readcount--;

if(readcount==0)

{

//如果所有读者读完,唤醒写者

LeaveCriticalSection(&RP_Write);

}

ReleaseMutex(h_Mutex);//释放互斥信号

}

///////////////////////////////////////////////////////////////////////////

//读者优先--写者线程

//p:

写者线程信息

voidRP_WriterThread(void*p)

{

DWORDm_delay;//延迟时间

DWORDm_persist;//写文件持续时间

intm_serial;//线程序号

//从参数中获得信息

m_serial=((ThreadInfo*)(p))->serial;

m_delay=(DWORD)(((ThreadInfo*)(p))->delay*INTE_PER_SEC);

m_persist=(DWORD)(((ThreadInfo*)(p))->persist*INTE_PER_SEC);

Sleep(m_delay);//延迟等待

printf("Writerthread%dsentsthewritingrequire.\n",m_serial);

//等待资源

EnterCriticalSection(&RP_Write);

//写文件

printf("Writerthread%dbeginstoWritetothefile.\n",m_serial);

Sleep(m_persist);

//退出线程

printf("Writerthread%dfinishedWritingtothefile.\n",m_serial);

//释放资源

LeaveCriticalSection(&RP_Write);

}

///////////////////////////////////////////////////////////////////////////

//读者优先处理函数

//file:

文件名

voidReaderPriority(char*file)

{

DWORDn_thread=0;//线程数目

DWORDthread_ID;//线程ID

DWORDwait_for_all;//等待所有线程结束

//互斥对象

HANDLEh_Mutex;

h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");

//线程对象的数组

HANDLEh_Thread[MAX_THREAD_NUM];

ThreadInfothread_info[MAX_THREAD_NUM];

readcount=0;//初始化readcount

InitializeCriticalSection(&RP_Write);//初始化临界区

ifstreaminFile;

inFile.open(file);//打开文件

printf("ReaderPriority:

\n\n");

while(inFile)

{

//读入每一个读者、写者的信息

inFile>>thread_info[n_thread].serial;

inFile>>thread_info[n_thread].entity;

inFile>>thread_info[n_thread].delay;

inFile>>thread_info[n_thread++].persist;

inFile.get();

}

for(inti=0;i<(int)(n_thread);i++)

{

if(thread_info[i].entity==READER||thread_info[i].entity=='R')

{

h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReaderThread),&thread_info[i],0,&thread_ID);//创建读者线程

}

else{

//创建写者线程

h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thread_info[i],0,&thread_ID);

}

}

//等待所有线程结束

wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);

printf("Allreaderandwriterhavefinishedoperating.\n");

}

///////////////////////////////////////////////////////////////////////////

//写者优先--读者线程

//p:

读者线程信息

voidWP_ReaderThread(void*p)

{

//互斥变量

HANDLEh_Mutex1;

h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex1");

HANDLEh_Mutex2;

h_Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex2");

DWORDwait_for_mutex1;//等待互斥变量所有权

DWORDwait_for_mutex2;

DWORDm_delay;//延迟时间

DWORDm_persist;//读文件持续时间

intm_serial;//线程序号

//从参数中获得信息

m_serial=((ThreadInfo*)(p))->serial;

m_delay=(DWORD)(((ThreadInfo*)(p))->delay*INTE_PER_SEC);

m_persist=(DWORD)(((ThreadInfo*)(p))->persist*INTE_PER_SEC);

Sleep(m_delay);//延迟等待

printf("Readerthread%dsentsthereadingrequire.\n",m_serial);

wait_for_mutex1=WaitForSingleObject(h_Mutex1,-1);

//进入读者临界区

EnterCriticalSection(&cs_Read);

//阻塞互斥对象mutex2,保证对readcount的访问、修改互斥

wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);

//修改读者数目

readcount++;

if(readcount==1)

{

//如果是第一个读者,等待写者写完

EnterCriticalSection(&cs_Write);

}

ReleaseMutex(h_Mutex2);//释放互斥信号mutex2

//让其他读者进入临界区

LeaveCriticalSection(&cs_Write);

ReleaseMutex(h_Mutex1);

//读文件

printf("Readerthread%dbeginstoreadfile.\n",m_serial);

Sleep(m_persist);

//退出线程

printf("Readerthread%dfinishedreadingfile.\n",m_serial);

//阻塞互斥对象mutex2,保证对readcount的访问、修改互斥

wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);

readcount--;

if(readcount==0)

{

//最后一个读者,唤醒写者

LeaveCriticalSection(&cs_Write);

}

ReleaseMutex(h_Mutex2);//释放互斥信号

}

///////////////////////////////////////////////////////////////////////////

//写者优先--写者线程

//p:

写者线程信息

voidWP_WriterThread(void*p)

{

DWORDm_delay;//延迟时间

DWORDm_persist;//写文件持续时间

intm_serial;//线程序号

DWORDwait_for_mutex3;

//互斥对象

HANDLEh_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*)(p))->persist*INTE_PER_SEC);

Sleep(m_delay);//延迟等待

printf("Writerthread%dsentsthewritingrequire.\n",m_serial);

//阻塞互斥对象mutex3,保证对writecount的访问、修改互斥

wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);

writecount++;//修改读者数目

if(writecount==1)

{

//第一个写者,等待读者读完

EnterCriticalSection(&cs_Read);

}

ReleaseMutex(h_Mutex3);

//进入写者临界区

EnterCriticalSection(&cs_Write);

//写文件

printf("Writerthread%dbeginstoWritetothefile.\n",m_serial);

Sleep(m_persist);

//退出线程

printf("Writerthread%dfinishingWritingtothefile.\n",m_serial);

//离开临界区

LeaveCriticalSection(&cs_Write);

//阻塞互斥对象mutex3,保证对writecount的访问、修改互斥

wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);

writecount--;//修改读者数目

if(writecount==0)

{

//写者写完,读者可以读

LeaveCriticalSection(&cs_Read);

}

ReleaseMutex(h_Mutex3);

}

///////////////////////////////////////////////////////////////////////////

//写者优先处理函数

//file:

文件名

voidWriterPriority(char*file)

{

DWORDn_thread=0;//线程数目

DWORDthread_ID;//线程ID

DWORDwait_for_all;//等待所有线程结束

//互斥对象

HANDLEh_Mutex1;

h_Mutex1=CreateMutex(NULL,FALSE,"mutex1");

HANDLEh_Mutex2;

h_Mutex2=CreateMutex(NULL,FALSE,"mutex2");

HANDLEh_Mutex3;

h_Mutex3=CreateMutex(NULL,FALSE,"mutex3");

//线程对象

HANDLEh_Thread[MAX_THREAD_NUM];

ThreadInfothread_info[MAX_THREAD_NUM];

readcount=0;//初始化readcount

writecount=0;//初始化writecount

InitializeCriticalSection(&cs_Write);//初始化临界区

InitializeCriticalSection(&cs_Read);

ifstreaminFile;

inFile.open(file);//打开文件

printf("WriterPriority:

\n\n");

while(inFile)

{

//读入每一个读者、写者的信息

inFile>>thread_info[n_thread].serial;

inFile>>thread_info[n_thread].entity;

inFile>>thread_info[n_thread].delay;

inFile>>thread_info[n_thread++].persist;

inFile.get();

}

for(inti=0;i<(int)(n_thread);i++)

{

if(thread_info[i].entity==READER||thread_info[i].entity=='R')

{

//创建读者线程

h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thread_info[i],0,&thread_ID);

}

else{

//创建写者线程

h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_info[i],0,&thread_ID);

}

}

//等待所有线程结束

wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);

printf("Allreaderandwriterhavefinishedoperating.\n");

}

///////////////////////////////////////////////////////////////////////////////

//主函数

intmain(intargc,char*argv[])

{

charch;

while(true)

{

//打印提示信息

printf("************************************************\n");

printf("1:

ReaderPriority\n");

printf("2:

WriterPriority\n");

printf("3:

Exi

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

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1