读者写者实验报告_第1页
读者写者实验报告_第2页
读者写者实验报告_第3页
读者写者实验报告_第4页
读者写者实验报告_第5页
已阅读5页,还剩20页未读, 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、操作系统原理实验报告实验名称:操作系统XXX名:号:XXXXXXXXXXXXX级:XXX指导老师:、实验内容在Windows2000 环境下,创建一个控制台进程,此进程包含n个线程。用这 n个线程来表示n个读者或写者。每个线程按相应测试数据文件(后面有介绍)的要求进行读写操作。用信号量机制分别实现读者优先和写者优先的读者-写者问题。读者-写者问题的读写操作限制(包括读者优先和写者优先):1)写-写互斥,即不能有两个写者同时进行写操作。2)读-写互斥,即不能同时有一个线程在读,而另一个线程在写。, 3)读-读允许,即可以有一个或多个读者在读。读者优先的附加限制:如果一个读者申请进行读操作时已有另

2、一个读者正在进行 读操作,则该读者可直接开始读操作。写者优先的附加限制:如果一个读者申请进行读操作时已有另一写者在等待访问 共享资源,则该读者必须等到没有写者处于等待状态后才能开始读操作。运行结果显示要求:要求在每个线程创建、发出读写操作申请、开始读写操作和 结束读写操作时分别显示一行提示信息,以确定所有处理都遵守相应的读写操作 限制。、实验目的在Windows2000 环境下,创建一个控制台进程,此进程包含n个线程。用这 n个线程来表示n个读者或写者。每个线程按相应测试数据文件(后面有介绍)的要求进行读写操作。用信号量机制分别实现读者优先和写者优先的读者-写者问题。三、实验原理1).读者优先

3、读者优先指的是除非有写者在写文件,否则读者不需要等待。所以可以用一个整 型变量read_count记录当前的读者数目,用于确定是否需要释放正在等待的写 者线程(当read_count=O 时,表明所有的读者读完,需要释放写者等待队列中的一个写者)。每一个读者开始读文件时,必须修改read_count变量。因此需要一个互斥对象 mutex来实现对全局变量read_count修改时的互斥。另外,为了实现写-写互斥,需要增加一个临界区对象 write。当写者发出写请求时,必须申请临界区对象的所有权。通过这种方法,也可以实现读-写互斥,当 read_count=l时(即第一个读者到来时),读者线程也必

4、须申请临界区对象的所有权。当读者拥有临界区的所有权时,写者阻塞在临界区对象 write上。当写者拥有临界区的所有权时,第一个读者判断完"read_count=1" 后阻塞在write 上, 其余的读者由于等待对read_count的判断,阻塞在 mutex上。2).写者优先写者优先与读者优先类似;不同之处在于一旦一个写者到来,它应该尽快对文件 进行写操作,如果有一个写者在等待,则新到来的读者不允许进行读操作。 为此 应当添加一个整型变量write_count,用于记录正在等待的写者的数目,当write_cou nt=O 时,才可以释放等待的读者线程队列。为了对全局变量wri

5、te_count实现互斥,必须增加一个互斥对象mutex3 。为了实现写者优先,应当添加一个临界区对象read,当有写者在写文件或等待时,读者必须阻塞在read上。读者线程除了要对全局变量read_cou nt实现操作上的互斥外,还必须有一个互斥对象对阻塞,read这一过程实现互斥。这两个互斥对象分别命名为mutex1和 mutex2。四、实验过程 在Windows2000 环境下,创建一个控制台进程。用VC+实现。读者优先指的是除非有写者在写文件,否则读者不需要等待。所以可以用一个整 型变量read_count记录当前的读者数目,用于确定是否需要释放正在等待的写 者线程(当read_coun

6、t=O 时,表明所有的读者读完,需要释放写者等待队列中的一个写者)。每一个读者开始读文件时,必须修改read_count变量。因此需要一个互斥对象mutex来实现对全局变量read count修改时的互斥。另外,为了实现写-写互斥,需要增加一个临界区对象 write。当写者发出写请求时,必须申请临界区对象的所有权。通过这种方法,也可以实现读-写互斥,当 read_count=l时(即第一个读者到来时),读者线程也必须申请临界区对象的所有权。当读者拥有临界区的所有权时,写者阻塞在临界区对象 write上。当写者拥有临界区的所有权时,第一个读者判断完"read_count=1"

7、 后阻塞在write 上, 其余的读者由于等待对read_count的判断,阻塞在 mutex上。写者优先与读者优先类似;不同之处在于一旦一个写者到来,它应该尽快对文件 进行写操作,如果有一个写者在等待,则新到来的读者不允许进行读操作。 为此应当添加一个整型变量write_count,用于记录正在等待的写者的数目,当write_cou nt=O 时,才可以释放等待的读者线程队列。为了对全局变量write_count实现互斥,必须增加一个互斥对象mutex3 。为了实现写者优先,应当添加一个临界区对象read,当有写者在写文件或等待时,读者必须阻塞在read上。读者线程除了要对全局变量read_

8、cou nt实现操作上的互斥外,还必须有一个互斥对象对阻塞,read这一过程实现互斥。这两个互斥对象分别命名为mutexi和 mutex2。结构:头文件定义全局变量RP_ReaderThread ()读者优先-读者线程RP_WriterThread()读者优先-写者线程Readerpriority ()读者优先处理函数WP_ReaderThrea ()写者优先-读者线程WP_WriterThread ()写者优先-写者线程WriterPriority()写者优先处理函数主函数:包含对dat文件的读取以及函数的调用。代码:附录 五、实验结果界面:1. Aeadet PrLoiiti,2. Wri

9、ter Pr£oi*itiJ3.1xit to UindowsEnter yeup clioictl”2,测试数据:5 W 5.1 3结果读者优先Jrz-tc t lircad 2 Jriter ±ki*e ad Reade r thre adJrite thread 5 Header thread (Jpite tliiwad 2 RefinerReader Headei*Readep Headei'Jritei«thread thread thread thread thread threadJrite thread 5 )11 reader and3

10、cnt;3 the writ; in日 rcquiirc . be 甘 ins to write to t lie filemerits tlie reo.dir'g req,la ii'c» seits tlie writ In £f require - wants tbr readlreqmIr-e* f11Isbed uritln to the begins to read file* begins to read file, begins t* read file* finished reading fil« finished readin

11、g file, begins to upit« to the FinisFied uniting to thefile.file, file.wi*iter haue finished ot>erat inv.Press Anv Keu to Uoutinue:_写者优先Thread lnfo()Hriter Rcculcr Writer Fteculcr Writer Wi-lter Ji-lter BtreiUer Reader Reeder Reeder KeederIzhi'CAd thread thrcAd thread thread thread threa

12、d thread thread thread thread thread thread223542553$1$1a ents tlie begins toStlie5 Bnt« tlie5 ent'S' tlieuritrE4uir-e wr Ltc ta t he file. rcAil ingwr i.t in'9 ruyii'c. rcAd ingrequ ire -rcQU ire * Flu Is Fed Tltlng to the file - begins to write to ti*: file, finished vr-ltLna

13、Co the file. begins to read file, beyIns to read file, bugrlns to read file, finished reading file, finis lied read tnsr f il&.nil reader and writer have fInished operating>Vess Pny Key to Goutinue;六、实验心得体会通过对读者写者问题的编程,对线程有了更深的了解,希望在后面的学习中懂得更多。七、参考文献老师提供的资料,以及互联网查阅。八、附录#in elude "wi ndow

14、s.h" #in elude vconi o.h> #i nclude <stdlib.h> #in elude <fstream.h> #i nclude <io.h>#in elude <stri ng.h>#i nclude <stdio.h>#i nclude "wi nbase.h"#defi ne READER'R'/读者/写者#defi ne WRITER#define INTE PER SEC1000/每秒时钟中断的数目#defi ne MAX_THREAD_NUM6

15、4/最大线程数/读者计数/写者计数int n ReaderC nt = 0;int n WriterC nt = 0;HANDLE hWrite:CreateSema phore(NULL, 1, 1, NULL );/写开始信号HANDLE hRead:CreateSema phore(NULL, 1, 1, NULL );/读开始信号HANDLE hRCMutex=:CreateMutex( NULL, FALSE, NULL );HANDLE hWCMutex=:CreateMutex( NULL, FALSE, NULL );HANDLE hReadMutex=:CreateMutex

16、( NULL, FALSE, NULL );/从测试数据文件中获取的线程信息struct ThreadI nfonSerialNo=0;cType='A' 一 )dDelayTime=0.0;dOp eTime=0.0;intnSerialNo;/线程序号charcType;/线程类别doubledDelayTime;/线程延迟时间doubledOp eTime;/线程读写操作时间;/读者优先-读者线程/ P:读者线程信息 void RP _ReaderThread(void *p)int nSerialNo = (ThreadInfo*)(p)->nSerialNo;

17、/ 从文件中读取 线程序号DWORD dwReadTime = (DWORD)(Threadl nfo*)( p)->d Op eTimeINTE _P ER_SEC );DWORD dwDelay(DWORD)(ThreadI nfo*)( p)->dDelayTimeINTE _P ER_SEC );Slee p( dwDelay );prin tf("Reader thread %d sents the readi ng require.' n",n SerialNo);WaitForSi ngleObject( hRCMutex, INFINIT

18、E );n ReaderC nt+;if( n ReaderC nt = 1 )WaitForSi ngleObject( hWrite, INFINITE );ReleaseMutex(hRCMutex);prin tf( "Reader thread %d beg ins to read file.n", n SerialNo );Slee p( dwReadTime );prin tf( "Reader thread %d fini shed readi ng file.n", n SerialNo );WaitForSi ngleObject(

19、hRCMutex, INFINITE );n ReaderC nt-;if( n ReaderC nt = 0 )ReleaseSema phore( hWrite, 1, NULL );ReleaseMutex(hRCMutex);/读者优先-写者线程/ P:写者线程信息void RP _WriterThread(void *p)从参数中获得信息int nSerialNo = (Threadl nfo*)( p)-> nSerialNo ; /DWORD dwWriteTime = (DWORD)(Threadl nfo*)( p)->d Op eTime *INTE _P ER

20、_SEC );DWORD dwDelay(DWORD)(ThreadI nfo*)( p)->dDelayTimeINTE _P ER_SEC );Slee p( dwDelay );prin tf("Write thread %d sents the writi ng require.' n", nSerialNo);WaitForSi ngleObject( hWrite, INFINITE );prin tf( "Writer thread %d beg ins to write to the file.'n", n Seri

21、alNo );Slee p( dwWriteTime );prin tf( "Write thread %d fin ished writi ng to the file.'n", n SerialNo );ReleaseSema phore( hWrite, 1, NULL );/读者优先处理函数 / file:文件名 void Reader Priority( char *file )DWORD dwThreadID = 0;int n ThreadC nt = 0;n ReaderC nt = 0;/初始化读写者计数HANDLE hThreadsMAX_THR

22、EAD_NUM;Threadl nfo oThreadI nfoMAX_THREAD_NUM;ifstream in File;inF ile. open ( file );prin tf( "Reader Priority:nn");while( in File )/读入每一个读者,写者的信息inF ile>>oThread lnfon ThreadC nt .n SerialNo;inF ile>>oThread lnfon ThreadC nt.cT ype;inF ile>>oThread lnfon ThreadC nt.dDe

23、layTime;inF ile>>oThread lnfon ThreadC nt.d Op eTime;if (S' != oThreadl nfon ThreadC nt.cTy pe )n ThreadC nt+;in File.getO;/创建线程 for( int i = 0; i< n ThreadC nt; i+ )0,if( oThreadl nfoi.cTy pe=READER ) | ( oThreadl nfoi.cTy pe ='r')hThreadsiCreateThread(NULL,(LPTHREAD_START_ROUT

24、INE)(RP_ReaderThread),/ 创建读者进程& oThreadl nfoi.CREATE_SUS PENDED, & dwThreadID );elsehThreadsiCreateThread(NULL,0,(L PTHREAD_START_ROUTINE)(R P_WriterThread),/创建写线程& oThreadl nfoi,CREATE_SUS PENDED, & dwThreadID );for( i = 0; i< n ThreadC nt; i+ )ResumeThread( hThreadsi);WaitForMul

25、t ip leObjects( nThreadC nt, hThreads, TRUE, INFINITE );prin tf( "All reader and writer have fini shed op erati ng.n");/写者优先-读者线程/ P:读者线程信息从参数中得到信息void WP_ReaderThread( void *p )int nSerialNo = (Threadl nfo*)( p)-> nSerialNo ; /DWORD dwReadTime = (DWORD)(Threadl nfo*)( p)->d Op eTime

26、INTE _P ER_SEC );DWORD dwDelay(DWORD)(ThreadI nfo*)( p)->dDelayTimeINTE _P ER_SEC );Slee p( dwDelay );prin tf("Reader thread %d sents the readi ng require.n",n SerialNo);WaitForSi ngleObject( hReadMutex, INFINITE );WaitForSi ngleObject( hRead, INFINITE );WaitForSi ngleObject( hRCMutex,

27、INFINITE );n ReaderC nt+;if(n ReaderC nt=1)WaitForSi ngleObject( hWrite, INFINITE );ReleaseMutex( hRCMutex );ReleaseSema phore( hRead, 1, NULL );ReleaseMutex( hReadMutex );prin tf( "Reader thread %d beg ins to read file.' n", n SerialNo );Slee p( dwReadTime );file.'n", nSerial

28、No );prin tf( "Reader thread %d fini shed readi ngWaitForSi ngleObject( hRCMutex, INFINITE );n ReaderC nt-;if( n ReaderC nt = 0 )ReleaseSema phore( hWrite, 1, NULL );ReleaseMutex( hRCMutex );/写者优先-写者线程/ P:写者线程信息 void WP _WriterThread( void *p )int nSerialNo = (Threadl nfo*)( p)-> nSerialNo;D

29、WORD dwWriteTime = (DWORD)(Threadl nfo*)( p)->d Op eTime *INTE _P ER_SEC );DWORD dwDelay(DWORD)(ThreadI nfo*)( p)->dDelayTimeINTE _P ER_SEC );Slee p( dwDelay );prin tf("Writer thread %d sents the writi ng require.' n", nSerialNo);WaitForSi ngleObject( hWCMutex, INFINITE );n Write

30、rC nt+;if( n WriterC nt = 1 )WaitForSi ngleObject( hRead, INFINITE );ReleaseMutex(hWCMutex);WaitForSi ngleObject( hWrite, INFINITE );prin tf( "Writer thread %d beg ins to write to the file.'n", n SerialNo );Slee p( dwWriteTime );prin tf( "Writer thread %d fini shed writi ng to the

31、 file.'n", n SerialNo );ReleaseSema phore( hWrite, 1, NULL );WaitForSi ngleObject( hWCMutex, INFINITE );n WriterC nt-;if( n WriterC nt = 0 )ReleaseSema phore( hRead, 1, NULL );ReleaseMutex(hWCMutex);/写者优先处理函数/ file:文件名void WriterPnority( char * file )int n ThreadC nt = 0;DWORD dwThreadID;HA

32、NDLE hThreadsMAX_THREAD_NUM;ThreadI nfo oThreadI nfoMAX_THREAD_NUM;n ReaderC nt=O;n WriterC nt=0;ifstream in File;inF ile. open (file);prin tf("Writer priority:nn");while(i nFile)inF ile>>oThread lnfon ThreadC nt .n SerialNo;inF ile>>oThread lnfon ThreadC nt.cT ype;inF ile>>oThread lnfon ThreadC nt.dDelayTime;inF ile>>oThread lnfon ThreadC nt.d Op eTime;if (S' != oThreadl nfon ThreadC nt.cTy pe )n ThreadC nt+;创建线程in File.getO;for( int i = 0 ;i < n ThreadC nt; i+ ) /if( oThreadlnfoi.cTypeR

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论