您好,欢迎访问三七文档
当前位置:首页 > 幼儿/小学教育 > 小学教育 > 任务5--事件对象解决读写者问题
1.基本信息实践题目:事件对象实现P、V操作并解决读写者问题完成人:班级:07062301姓名:陈杨学号:0706230101报告日期:2011年1月5日2.实践内容简要描述实践目标1.掌握事件对象的使用方法2.理解P、V操作的内部机制及读者-写者问题的实现方法。实践内容用Win32提供的同步对象实现P、V操作,并使用它们解决读者-写者问题利用事件机制模拟多值信号量。算法如下:binary-semaphoreS1,S2;//initializtion:S1=1,S2=0intC;//initializtion:thecountofrecourcewait():Wait(S1);C--;if(C0){signal(S1);wait(S2);}signal(S2);signal():wait(S1);C++;if(C=0){signal(S2);}else{signal(S1);}读者-写者问题算法如下:Writer:wait(Wmutex);//dowritingsignal(Wmutex);Reader:wait(Rmutex);if(Rcount==0){wait(Wmutex);}Rcount++;signal(Rmutex);//doreadingwait(Rmutex);Rcount--;if(Rcount==0){signal(Wmutex);}signal(Rmutex);设计思路利用事件对象实现P、V操作主要数据结构intreadcount=0;intwritecount=0;structMySemaphore{HANDLEs1,s2;intc;};MySemaphoreReaderS;MySemaphoreWriterS;structThreadInfo{intserial;charentity;doubledelay;doublepersist;};主要代码结构及分析/////////////////////////////////////////////////////////////////ReaderPriorityfuction//file:filename//////////////////////////////////////////////////////////////voidReaderPriority(char*file){DWORDn_thread=0;DWORDthread_ID;DWORDwait_for_all;initMySemaphore(&ReaderS,1);initMySemaphore(&WriterS,1);readcount=0;//initreadercount//TreadObjectArrayHANDLEh_Thread[MAX_THREAD_NUM];ThreadInfothread_info[MAX_THREAD_NUM];ifstreaminFile;inFile.open(file);//openfileprintf(ReaderPriority:\n\n);while(inFile){//readeveryreader/writerinfoinFilethread_info[n_thread].serial;inFilethread_info[n_thread].entity;inFilethread_info[n_thread].delay;inFilethread_info[n_thread++].persist;inFile.get();}//endwhileinFile.close();for(inti=0;i(int)(n_thread);i++){if(thread_info[i].entity=='R'||thread_info[1].entity=='r'){//CreateReaderthreadh_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReaderThread),&thread_info[i],0,&thread_ID);}else{//CreateWriterthreadh_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thread_info[i],0,&thread_ID);}}//endfor//waitingallthreadwillbeenfinishedwait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);printf(AllreaderandwriterthreadhavefinishedOperating.\n);closeMySemaphore(&WriterS);closeMySemaphore(&ReaderS);}//endreaderPriorityvoidwait(MySemaphore*myS){WaitForSingleObject(myS-s1,INFINITE);myS-c--;if(myS-c0){SetEvent(myS-s1);WaitForSingleObject(myS-s2,INFINITE);}SetEvent(myS-s1);}voidsignal(MySemaphore*myS){WaitForSingleObject(myS-s1,INFINITE);myS-c++;if(myS-c=0){SetEvent(myS-s2);}else{SetEvent(myS-s1);}}//////////////////////////////////////readerpriority--readerthread//p:readerthreadinfo///////////////////////////////////DWORDWINAPIRP_ReaderThread(void*p){DWORDm_delay;DWORDm_persist;intm_serial;//getinfofroamparam_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(&ReaderS);if(readcount==0){wait(&WriterS);}readcount++;signal(&ReaderS);//readfileprintf(Readerthread%dbeginstoreadfile.\n,m_serial);Sleep(m_persist);//exitthreadprintf(Readerthread%dfinishedreadingfile.\n,m_serial);wait(&ReaderS);readcount--;if(readcount==0){signal(&WriterS);}signal(&ReaderS);return0;}//////////////////////////////////////Readerpriority--writerthread//p:writerthreadinfo///////////////////////////////////DWORDWINAPIRP_WriterThread(void*p){DWORDm_delay;DWORDm_persist;intm_serial;//getinfofroamparam_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);//waitresourcewait(&WriterS);//writetothefileprintf(Writerthread%dbeginstowritetothefile.\n,m_serial);Sleep(m_persist);//exitthreadprintf(Writerthread%dfinishedWritingtothefile.\n,m_serial);//releaseresourcesignal(&WriterS);return0;}3.实践结果基本数据:源程序代码行数:256完成实践投入的时间(小时数):5资料查阅时间:1.5编程调试时间:3.5测试数据设计“ex5.dat”中测试数据为:1R352W453R524R655W53测试结果分析待所有读进程执行完毕后写进程才开始一一执行,结果正确。4.实践体会实践过程中遇到的问题及解决过程在调试时,一开始总会出现某个进程sentsrequire的信息重复出现两次,但该线程只执行过一次的现象,调整显示代码的位置得不到正确的结果。后将读写进程函数由voidRP_ReaderThread(void*p);voidRP_WriterThread(void*p);改为DWORDWINAPIRP_ReaderThread(void*p);DWORDWINAPIRP_WriterThread(void*p);并在函数体最后加上return0语句。该问题顺利解决。实践体会和收获通过此次实验,我学会了事件对象的使用方法,并深入了解了P、V算法的机制及用其解决读着-写者问题的实现方法。同时我也锻炼了自己的编程能力。5.参考文献[1]汤子灜著.《计算机操作系统》(修订版)[M].西安:西安电子科技大学出版社.2000[2]AbrahamSilberschatz,PeterBaerGalvinandGregGagne.郑扣根等译.操作系统概念第六版,翻译版.[M].北京:高等教育出版社.2004
本文标题:任务5--事件对象解决读写者问题
链接地址:https://www.777doc.com/doc-1659826 .html