操作系统报告读者写者问题读者写者问题20032320鲍小伟_第1页
操作系统报告读者写者问题读者写者问题20032320鲍小伟_第2页
操作系统报告读者写者问题读者写者问题20032320鲍小伟_第3页
操作系统报告读者写者问题读者写者问题20032320鲍小伟_第4页
操作系统报告读者写者问题读者写者问题20032320鲍小伟_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

操作系统实验报告读者写者问题PAGEPAGE10读者写者问题(20032320鲍小伟)设计要求:在windows98/2000下,创建一个控制台进程,此进程包含n个线程。用这n个线程来表示n个读者或写者。每个线程按相应测试数据文件的要求进行读写操作。用信号量机制实现读者优先的读者——写入者问题。设计分析:读者-写者的读写限制(包括读者优先和写者优先)1.写-写互斥,即不能有两个写者同时进行写操作2.读-写互斥,即不能同时有一个读者在读,同时却有一个写者在写3.读读允许,即可以有2个以上的读者同时读读者优先的限制:如果一个读者申请读操作时,已经有一个读者在读,则该读者可以直接读写者优先的限制:如果一个读者申请读操作时,有写者在等待访问共享资源时,则该读者要等到没有写者处于等的状态时才能开始读操作测试数据的格式在文件thread.dat中1R352W453R524R655W5.13其中第一个代表线程的ID,第二个字段代表是读操作还是写操作,第三个字段代表操作的开始时间,第4个字段是持续时间。分析:将所有的读者和所有的写者分别放进两个等待队列中,当读允许时就让读者队列释放一个或多个读者,当写允许时,释放第一个写者操作。读者优先:如果没有写者正在操作,则读者不需要等待,用一个整型变量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。程序所用结构类型说明:1.CreateThread();2.ExitThread();3.Sleep();4.CreateMutex();5.ReleaseMutex();6.WaitForSingleObject();7.WaitForMutipleObjects();8.CreateSemapore();9.ReleaseSemapore();10.InitializeCriticalSection();11.EnterCriticalSection();12.LeaveCriticalSection();源代码:#include<windows.h>#include<conio.h>#include<stdlib.h>#include"fstream.h"#include<io.h>#include<string.h>#include<stdio.h>#defineREADER'R'#defineWRITER'W'#defineINTE_PER_SEC100//每秒时钟中断数目#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;//线程读写持续时间};//读者优先处理函数,已经给出voidReaderPriority(char*file);voidRP_ReaderThread(void*p);voidRP_WriterThread(void*p);//写者优先处理函数,精读"读者优先"程序和设计分析,自己完成voidWriterPriority(char*file);voidWP_ReaderThread(void*p);voidWP_WriterThread(void*p);//为了保证现在程序编译通过,写一个空函数,请你写出自己的WriterPriority//voidWriterPriority(char*file){}intmain(){ charch; while(true) { printf("*********************************************************\n"); printf("1:ReaderPriority\n");printf("2:WriterPriority\n"); printf("3:ExittoWindows\n"); printf("*********************************************************\n"); printf("Enteryourchoice(1,2or3)\n"); do { ch=(char)_getch(); }while(ch!='1'&&ch!='2'&&ch!='3'); system("cls"); if(ch=='3') return0; elseif(ch=='1') ReaderPriority("thread.txt"); else WriterPriority("thread.txt"); printf("\nPressAnykeytocontinue.\n"); _getch(); system("cls"); } return0;}//读者优先处理函数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; if(-1==inFile.get()) break; n_thread++; } 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");}//读者优先读者线程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); wait_for_mutex=WaitForSingleObject(h_Mutex,-1);//等待互斥信号,保证对readcount的访问、 //修改互斥 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); wait_for_mutex=WaitForSingleObject(h_Mutex,-1);//等待互斥信号,保证对readcount的访问、修改互斥 readcount--;//读者数目减少 if(readcount==0) LeaveCriticalSection(&RP_Write); //如果所有读者读完,唤醒写者 ReleaseMutex(h_Mutex);}//读者优先写者线程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%dbeginstowritefile.\n",m_serial); Sleep(m_persist); //退出线程 printf("Writerthread%dfinishedwritingfile.\n",m_serial); LeaveCriticalSection(&RP_Write); //如果所有读者读完,唤醒写者}//写者优先处理函数voidWriterPriority(char*file){ DWORDn_thread=0;//线程数目 DWORDthread_ID;//线程ID DWORDwait_for_all;//等待所有线程结束 //互斥对象 HANDLEh_Mutex1,h_Mutex2,h_Mutex3; h_Mutex1=CreateMutex(NULL,FALSE,"mutex_for_writecount"); h_Mutex2=CreateMutex(NULL,FALSE,"mutex_for_readcount"); h_Mutex3=CreateMutex(NULL,FALSE,"mutex_for_read"); //线程对象数组 HANDLEh_Thread[MAX_THREAD_NUM]; ThreadInfothread_info[MAX_THREAD_NUM]; readcount=0;//初始化readcount 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; if(-1==inFile.get()) break; n_thread++; } 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)(WP_ReaderThread),\ &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");}//写者优先写者线程voidWP_WriterThread(void*p){ //互斥变量 HANDLEh_Mutex1; h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_writecount"); 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("Writerthread%dsentsthewritingrequire.\n",m_serial); wait_for_mutex=WaitForSingleObject(h_Mutex1,-1); writecount++; if(writecount==1) EnterCriticalSection(&cs_Read); ReleaseMutex(h_Mutex1); EnterCriticalSection(&cs_Write); printf("Writerthread%dbeginstowritefile.\n",m_serial); Sleep(m_persist); printf("Writerthread%dfinishedwritingfile.\n",m_serial); LeaveCriticalSection(&cs_Write); wait_for_mutex=WaitForSingleObject(h_Mutex1,-1); writecount--; if(writecount==0) LeaveCriticalSection(&cs_Read); ReleaseMutex(h_Mutex1);}//写者优先读者线程voidWP_ReaderThread(void*p){ HANDLEh_Mutex2,h_Mutex3; h_Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount"); h_Mutex3=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_read"); DWORDwait_for_mutex,wait_for_mutex1;//等待互斥变量所有权 DWORDm_delay;//延迟时间 DWORDm_persist;//读文件持续时间 intm_serial;//线程序号 m_serial=((ThreadInfo*)(p))->serial; m_delay=(DWORD)(((ThreadInfo*)(p))->delay*INTE_PER_SEC); m_persist=(DWORD)(((ThreadInfo

温馨提示

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

评论

0/150

提交评论