




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
操作系統(tǒng)實(shí)驗(yàn)報(bào)告讀者寫者問題PAGEPAGE10讀者寫者問題(20032320鮑小偉)設(shè)計(jì)要求:在windows98/2000下,創(chuàng)建一個控制臺進(jìn)程,此進(jìn)程包含n個線程。用這n個線程來表示n個讀者或?qū)懻?。每個線程按相應(yīng)測試數(shù)據(jù)文件的要求進(jìn)行讀寫操作。用信號量機(jī)制實(shí)現(xiàn)讀者優(yōu)先的讀者——寫入者問題。設(shè)計(jì)分析:讀者-寫者的讀寫限制(包括讀者優(yōu)先和寫者優(yōu)先)1.寫-寫互斥,即不能有兩個寫者同時進(jìn)行寫操作2.讀-寫互斥,即不能同時有一個讀者在讀,同時卻有一個寫者在寫3.讀讀允許,即可以有2個以上的讀者同時讀讀者優(yōu)先的限制:如果一個讀者申請讀操作時,已經(jīng)有一個讀者在讀,則該讀者可以直接讀寫者優(yōu)先的限制:如果一個讀者申請讀操作時,有寫者在等待訪問共享資源時,則該讀者要等到?jīng)]有寫者處于等的狀態(tài)時才能開始讀操作測試數(shù)據(jù)的格式在文件thread.dat中1R352W453R524R655W5.13其中第一個代表線程的ID,第二個字段代表是讀操作還是寫操作,第三個字段代表操作的開始時間,第4個字段是持續(xù)時間。分析:將所有的讀者和所有的寫者分別放進(jìn)兩個等待隊(duì)列中,當(dāng)讀允許時就讓讀者隊(duì)列釋放一個或多個讀者,當(dāng)寫允許時,釋放第一個寫者操作。讀者優(yōu)先:如果沒有寫者正在操作,則讀者不需要等待,用一個整型變量readcount記錄當(dāng)前的讀者數(shù)目,用于確定是否釋放寫者線程,(當(dāng)readcout=0時,說明所有的讀者都已經(jīng)讀完,釋放一個寫者線程),每個讀者開始讀之前都要修改readcount,為了互斥的實(shí)現(xiàn)對readcount的修改,需要一個互斥對象Mutex來實(shí)現(xiàn)互斥。另外,為了實(shí)現(xiàn)寫-寫互斥,需要一個臨界區(qū)對象write,當(dāng)寫者發(fā)出寫的請求時,必須先得到臨界區(qū)對象的所有權(quán)。通過這種方法,可以實(shí)現(xiàn)讀寫互斥,當(dāng)readcount=1時,(即第一個讀者的到來時,),讀者線程也必須申請臨界區(qū)對象的所有權(quán).當(dāng)讀者擁有臨界區(qū)的所有權(quán),寫者都阻塞在臨界區(qū)對象write上。當(dāng)寫者擁有臨界區(qū)對象所有權(quán)時,第一個判斷完readcount==1后,其余的讀者由于等待對readcount的判斷,阻塞在Mutex上!寫者優(yōu)先:寫者優(yōu)先和讀者優(yōu)先有相同之處,不同的地方在:一旦有一個寫者到來時,應(yīng)該盡快讓寫者進(jìn)行寫,如果有一個寫者在等待,則新到的讀者操作不能讀操作,為此添加一個整型變量writecount,記錄寫者的數(shù)目,當(dāng)writecount=0時才可以釋放讀者進(jìn)行讀操作!為了實(shí)現(xiàn)對全局變量writecount的互斥訪問,設(shè)置了一個互斥對象Mutex3。為了實(shí)現(xiàn)寫者優(yōu)先,設(shè)置一個臨界區(qū)對象read,當(dāng)有寫者在寫或等待時,讀者必須阻塞在臨界區(qū)對象read上。讀者除了要一個全局變量readcount實(shí)現(xiàn)操作上的互斥外,還需要一個互斥對象對阻塞在read這一個過程實(shí)現(xiàn)互斥,這兩個互斥對象分別為mutex1和mutex2。程序所用結(jié)構(gòu)類型說明: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//每秒時鐘中斷數(shù)目#defineMAX_THREAD_NUM64//最大線程數(shù)目#defineMAX_FILE_NUM32//最大數(shù)據(jù)文件數(shù)目#defineMAX_STR_LEN32//字符串長度//全局變量intreadcount=0;//讀者數(shù)目intwritecount=0;//寫者數(shù)目CRITICAL_SECTIONRP_Write;//臨界區(qū)CRITICAL_SECTIONcs_Write;CRITICAL_SECTIONcs_Read;structThreadInfo{ intserial;//線程序號 charentity;//線程類別 doubledelay;//線程開始時間 doublepersist;//線程讀寫持續(xù)時間};//讀者優(yōu)先處理函數(shù),已經(jīng)給出voidReaderPriority(char*file);voidRP_ReaderThread(void*p);voidRP_WriterThread(void*p);//寫者優(yōu)先處理函數(shù),精讀"讀者優(yōu)先"程序和設(shè)計(jì)分析,自己完成voidWriterPriority(char*file);voidWP_ReaderThread(void*p);voidWP_WriterThread(void*p);//為了保證現(xiàn)在程序編譯通過,寫一個空函數(shù),請你寫出自己的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;}//讀者優(yōu)先處理函數(shù)voidReaderPriority(char*file){ DWORDn_thread=0;//線程數(shù)目 DWORDthread_ID;//線程ID DWORDwait_for_all;//等待所有線程結(jié)束 //互斥對象 HANDLEh_Mutex; h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount"); //線程對象數(shù)組 HANDLEh_Thread[MAX_THREAD_NUM]; ThreadInfothread_info[MAX_THREAD_NUM]; readcount=0;//初始化readcount InitializeCriticalSection(&RP_Write);//初始化臨界區(qū) 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') //創(chuàng)建讀者線程 h_Thread[i]=CreateThread(NULL,0,\ (LPTHREAD_START_ROUTINE)(RP_ReaderThread),\ &thread_info[i],0,&thread_ID); else //創(chuàng)建寫者線程 h_Thread[i]=CreateThread(NULL,0,\ (LPTHREAD_START_ROUTINE)(RP_WriterThread),\ &thread_info[i],0,&thread_ID); } //等待所有線程結(jié)束 wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1); printf("AllReaderandWriterhavefinishedoperating.\n");}//讀者優(yōu)先讀者線程voidRP_ReaderThread(void*p){ //互斥變量 HANDLEh_Mutex; h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount"); DWORDwait_for_mutex;//等待互斥變量所有權(quán) DWORDm_delay;//延遲時間 DWORDm_persist;//讀文件持續(xù)時間 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++;//讀者數(shù)目增加 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--;//讀者數(shù)目減少 if(readcount==0) LeaveCriticalSection(&RP_Write); //如果所有讀者讀完,喚醒寫者 ReleaseMutex(h_Mutex);}//讀者優(yōu)先寫者線程voidRP_WriterThread(void*p){ DWORDm_delay;//延遲時間 DWORDm_persist;//讀文件持續(xù)時間 intm_serial;//線程序號 //從參數(shù)中獲得信息 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); //如果所有讀者讀完,喚醒寫者}//寫者優(yōu)先處理函數(shù)voidWriterPriority(char*file){ DWORDn_thread=0;//線程數(shù)目 DWORDthread_ID;//線程ID DWORDwait_for_all;//等待所有線程結(jié)束 //互斥對象 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"); //線程對象數(shù)組 HANDLEh_Thread[MAX_THREAD_NUM]; ThreadInfothread_info[MAX_THREAD_NUM]; readcount=0;//初始化readcount InitializeCriticalSection(&cs_Write);//初始化臨界區(qū) 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') //創(chuàng)建讀者線程 h_Thread[i]=CreateThread(NULL,0,\ (LPTHREAD_START_ROUTINE)(WP_ReaderThread),\ &thread_info[i],0,&thread_ID); else //創(chuàng)建寫者線程 h_Thread[i]=CreateThread(NULL,0,\ (LPTHREAD_START_ROUTINE)(WP_WriterThread),\ &thread_info[i],0,&thread_ID); } //等待所有線程結(jié)束 wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1); printf("AllReaderandWriterhavefinishedoperating.\n");}//寫者優(yōu)先寫者線程voidWP_WriterThread(void*p){ //互斥變量 HANDLEh_Mutex1; h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_writecount"); DWORDwait_for_mutex;//等待互斥變量所有權(quán) DWORDm_delay;//延遲時間 DWORDm_persist;//讀文件持時續(xù)間 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);}//寫者優(yōu)先讀者線程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;//等待互斥變量所有權(quán) DWORDm_delay;//延遲時間 DWORDm_persist;//讀文件持續(xù)時間 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 房地產(chǎn)行業(yè)品牌宣傳策略計(jì)劃
- 主管如何制定績效標(biāo)準(zhǔn)計(jì)劃
- 年度工作計(jì)劃中的關(guān)鍵項(xiàng)目推進(jìn)方法
- 社團(tuán)活動與課程的結(jié)合理念計(jì)劃
- 企業(yè)風(fēng)險(xiǎn)評估與防范體系建設(shè)
- 體育教育中的性別平等問題探討
- 專業(yè)課程對學(xué)生問題解決能力的培訓(xùn)策略
- 體育賽事與城市青少年健康成長的關(guān)系研究
- 企業(yè)員工時間管理技巧
- Unit 8 Our Clothes Topic 2 We can design our own uniforms. Section D 教學(xué)設(shè)計(jì)2024-2025學(xué)年仁愛科普版八年級英語下冊
- 三年級勞動課1ppt
- 《乘法交換律和結(jié)合律》教學(xué)課件數(shù)學(xué)四年級下冊
- 大數(shù)據(jù)在金融領(lǐng)域的應(yīng)用方案
- 錨桿(索)檢驗(yàn)批質(zhì)量驗(yàn)收記錄
- 生產(chǎn)作業(yè)指導(dǎo)書SOP表格模板
- 花卉生產(chǎn)設(shè)施課件
- 云南省主要礦產(chǎn)資源
- 傳統(tǒng)體育養(yǎng)生概論
- 電力建設(shè)工程預(yù)算定額2006版
- 地鐵活塞風(fēng)相關(guān)計(jì)算
- DLT5216-2005 35kV~220kV城市地下變電站設(shè)計(jì)規(guī)定
評論
0/150
提交評論