實(shí)驗(yàn)五 讀者-寫(xiě)者問(wèn)題_第1頁(yè)
實(shí)驗(yàn)五 讀者-寫(xiě)者問(wèn)題_第2頁(yè)
實(shí)驗(yàn)五 讀者-寫(xiě)者問(wèn)題_第3頁(yè)
實(shí)驗(yàn)五 讀者-寫(xiě)者問(wèn)題_第4頁(yè)
實(shí)驗(yàn)五 讀者-寫(xiě)者問(wèn)題_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、實(shí)驗(yàn)題目:實(shí)驗(yàn)五 讀者-寫(xiě)者問(wèn)題完成人:報(bào)告日期:1、 實(shí)驗(yàn)內(nèi)容簡(jiǎn)要描述1) 創(chuàng)建一個(gè)控制臺(tái)進(jìn)程,此進(jìn)程包含n個(gè)線(xiàn)程。用這n個(gè)線(xiàn)程來(lái)表示n個(gè)讀者或?qū)懻?。每個(gè)線(xiàn)程按相應(yīng)測(cè)試數(shù)據(jù)文件的要求進(jìn)行讀寫(xiě)操作。用信號(hào)量機(jī)制分別實(shí)現(xiàn)讀者優(yōu)先和寫(xiě)者優(yōu)先的讀者-寫(xiě)者問(wèn)題。2) 讀者-寫(xiě)者問(wèn)題的讀寫(xiě)操作限制(包括讀者優(yōu)先和寫(xiě)者優(yōu)先):j 寫(xiě)-寫(xiě)互斥,即不能有兩個(gè)寫(xiě)者同時(shí)進(jìn)行寫(xiě)操作。k 讀-寫(xiě)互斥,即不能同時(shí)有一個(gè)線(xiàn)程在讀,而另一個(gè)線(xiàn)程在寫(xiě)。l 讀-讀允許,即可以有一個(gè)或多個(gè)讀者在讀。3) 讀者優(yōu)先的附加限制:如果一個(gè)讀者申請(qǐng)進(jìn)行讀操作時(shí)已有另一個(gè)讀者正在進(jìn)行讀操作,則該讀者可直接開(kāi)始讀操作。4) 寫(xiě)者優(yōu)先的附加限

2、制:如果一個(gè)讀者申請(qǐng)進(jìn)行讀操作時(shí)已有另一寫(xiě)者在等待訪(fǎng)問(wèn)共享資源,則該讀者必須等到?jīng)]有寫(xiě)者處于等待狀態(tài)后才能開(kāi)始讀操作。5) 運(yùn)行結(jié)果顯示要求:要求在每個(gè)線(xiàn)程創(chuàng)建、發(fā)出讀寫(xiě)操作申請(qǐng)、開(kāi)始讀寫(xiě)操作和結(jié)束讀寫(xiě)操作時(shí)分別顯示一行提示信息,以確定所有處理都遵守相應(yīng)的讀寫(xiě)操作限制。二、程序設(shè)計(jì)1、設(shè)計(jì)思路將所有讀者和所有寫(xiě)者分別存于一個(gè)讀者等待隊(duì)列和一個(gè)寫(xiě)者等待隊(duì)列中,每當(dāng)讀允許時(shí),就從讀者隊(duì)列中釋放一個(gè)或多個(gè)讀者線(xiàn)程進(jìn)行讀操作;每當(dāng)寫(xiě)允許時(shí),就從寫(xiě)者隊(duì)列中釋放一個(gè)寫(xiě)者進(jìn)行寫(xiě)操作。2、 主要數(shù)據(jù)結(jié)構(gòu)1)讀者優(yōu)先讀者優(yōu)先指的是除非有寫(xiě)者在寫(xiě)文件,否則讀者不需要等待。所以可以用一個(gè)整型變量read_count

3、記錄當(dāng)前的讀者數(shù)目,用于確定是否需要釋放正在等待的寫(xiě)者線(xiàn)程(當(dāng)read_count=0時(shí),表明所有的讀者讀完,需要釋放寫(xiě)者等待隊(duì)列中的一個(gè)寫(xiě)者)。每一個(gè)讀者開(kāi)始讀文件時(shí),必須修改read_count變量。因此需要一個(gè)互斥對(duì)象mutex來(lái)實(shí)現(xiàn)對(duì)全局變量read_count修改時(shí)的互斥。另外,為了實(shí)現(xiàn)寫(xiě)-寫(xiě)互斥,需要增加一個(gè)臨界區(qū)對(duì)象write。當(dāng)寫(xiě)者發(fā)出寫(xiě)請(qǐng)求時(shí),必須申請(qǐng)臨界區(qū)對(duì)象的所有權(quán)。通過(guò)這種方法,也可以實(shí)現(xiàn)讀-寫(xiě)互斥,當(dāng)read_count=l時(shí)(即第一個(gè)讀者到來(lái)時(shí)),讀者線(xiàn)程也必須申請(qǐng)臨界區(qū)對(duì)象的所有權(quán)。當(dāng)讀者擁有臨界區(qū)的所有權(quán)時(shí),寫(xiě)者阻塞在臨界區(qū)對(duì)象write上。當(dāng)寫(xiě)者擁有臨界區(qū)的所

4、有權(quán)時(shí), 第一個(gè)讀者判斷完 "read_count=1"后阻塞在write上,其余的讀者由于等待對(duì)read_count的判斷,阻塞在mutex上。2)寫(xiě)者優(yōu)先寫(xiě)者優(yōu)先與讀者優(yōu)先類(lèi)似;不同之處在于一旦一個(gè)寫(xiě)者到來(lái),它應(yīng)該盡快對(duì)文件進(jìn)行寫(xiě)操作,如果有一個(gè)寫(xiě)者在等待,則新到來(lái)的讀者不允許進(jìn)行讀操作。為此應(yīng)當(dāng)添加一個(gè)整型變量write_count,用于記錄正在等待的寫(xiě)者的數(shù)目,當(dāng)write_count=0時(shí),才可以釋放等待的讀者線(xiàn)程隊(duì)列。為了對(duì)全局變量write_count實(shí)現(xiàn)互斥,必須增加一個(gè)互斥對(duì)象mutex3。為了實(shí)現(xiàn)寫(xiě)者優(yōu)先,應(yīng)當(dāng)添加一個(gè)臨界區(qū)對(duì)象read,當(dāng)有寫(xiě)者在寫(xiě)文件

5、或等待時(shí),讀者必須阻塞在read上。 ·讀者線(xiàn)程除了要對(duì)全局變量read_count實(shí)現(xiàn)操作上的互斥外,還必須有一個(gè)互斥對(duì)象對(duì)阻塞,read這一過(guò)程實(shí)現(xiàn)互斥。這兩個(gè)互斥對(duì)象分別命名為mutex1和mutex2。 三、實(shí)驗(yàn)結(jié)果1、基本數(shù)據(jù)源程序代碼行數(shù)完成該實(shí)驗(yàn)投入的時(shí)間(小時(shí)數(shù))與其他同學(xué)討論次數(shù)355532、 測(cè)試數(shù)據(jù)設(shè)計(jì)測(cè)試數(shù)據(jù)文件包括n行測(cè)試數(shù)據(jù),分別描述創(chuàng)建的n個(gè)線(xiàn)程是讀者還是寫(xiě)者,以及讀寫(xiě)操作的開(kāi)始時(shí)間和持續(xù)時(shí)間。每行測(cè)試數(shù)據(jù)包括四個(gè)字段,各個(gè)字段間用空格分隔。第一字段為一個(gè)正整數(shù),表示線(xiàn)程序號(hào)。第二字段表示相應(yīng)線(xiàn)程角色,R表示讀者,W表示寫(xiě)者。第三字段為一個(gè)正數(shù),表示讀寫(xiě)

6、操作的開(kāi)始時(shí)間:線(xiàn)程創(chuàng)建后,延遲相應(yīng)時(shí)間(單位為秒)后發(fā)出對(duì)共享資源的讀寫(xiě)申請(qǐng)。第四字段為一個(gè)正數(shù),表示讀寫(xiě)操作的持續(xù)時(shí)間。當(dāng)線(xiàn)程讀寫(xiě)申請(qǐng)成功后,開(kāi)始對(duì)共享資源的讀寫(xiě)操作,該操作持續(xù)相應(yīng)時(shí)間后結(jié)束,并釋放共享資源。下面是一個(gè)測(cè)試數(shù)據(jù)文件的例子:2 W 4 53 R 5 24 R 6 5 5 W 5.1 33、測(cè)試結(jié)果分析四、實(shí)驗(yàn)體會(huì)1、實(shí)驗(yàn)過(guò)程中遇到的問(wèn)題及解決過(guò)程2、實(shí)驗(yàn)體會(huì)和收獲五、源代碼#include "windows.h"#include <conio.h>#include <stdlib.h>#include <fstream.h&

7、gt;#include <io.h>#include <string.h>#include <stdio.h>#define READER 'R' / 讀者#define WRITER 'W' / 寫(xiě)者#define INTE_PER_SEC 1000 / 每秒時(shí)鐘中斷數(shù)目。#define MAX_THREAD_NUM 64 / 最大線(xiàn)程數(shù)目#define MAX_FILE_NUM 32 / 最大數(shù)據(jù)文件數(shù)目#define MAX_STR_LEN 32 / 字符串長(zhǎng)度int readcount=0; / 讀者數(shù)目int wri

8、tecount=0; / 寫(xiě)者數(shù)目CRITICAL_SECTION RP_Write; /臨界區(qū)CRITICAL_SECTION cs_Write;CRITICAL_SECTION cs_Read;struct ThreadInfoint serial; / 線(xiàn)程序號(hào)char entity; /線(xiàn)程類(lèi)別(判斷讀者線(xiàn)程還是寫(xiě)者線(xiàn)程)double delay;double persist;/ 讀者優(yōu)先-讀者線(xiàn)程/p:讀者線(xiàn)程信息void RP_ReaderThread(void* p)/互斥變量HANDLE h_Mutex;h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FA

9、LSE,"mutex_for_readcount");DWORD wait_for_mutex; /等待互斥變量所有權(quán)DWORD m_delay; / 延遲時(shí)間DWORD m_persist; / 讀文件持續(xù)時(shí)間int m_serial; /線(xiàn)程序號(hào)/從參數(shù)中獲得信息m_serial=(ThreadInfo*)(p)->serial;m_delay=(DWORD)(ThreadInfo*)(p)->delay*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)->persist*INTE_PER_SEC);Sle

10、ep(m_delay); /延遲等待printf("Reader thread %d sents the reading require.n",m_serial);/ 等待互斥信號(hào),保證對(duì)readcount的訪(fǎng)問(wèn)、修改互斥wait_for_mutex=WaitForSingleObject(h_Mutex,-1);/讀者數(shù)目增加readcount+;if(readcount=1) /第一個(gè)讀者,等待資源 EnterCriticalSection(&RP_Write);ReleaseMutex(h_Mutex); /釋放互斥信號(hào)/讀文件printf("Rea

11、der thread %d begins to read file.n",m_serial);Sleep(m_persist);/ 退出線(xiàn)程printf("Reader thread %d finished reading file.n",m_serial);/等待互斥信號(hào),保證對(duì)readcount的訪(fǎng)問(wèn)、修改互斥wait_for_mutex=WaitForSingleObject(h_Mutex,-1);/讀者數(shù)目減少readcount-;if(readcount=0) /如果所有讀者讀完,喚醒寫(xiě)者 LeaveCriticalSection(&RP_Wr

12、ite);ReleaseMutex(h_Mutex); /釋放互斥信號(hào)/ 讀者優(yōu)先-寫(xiě)者線(xiàn)程/p:寫(xiě)者線(xiàn)程信息void RP_WriterThread(void* p)DWORD m_delay; / 延遲時(shí)間DWORD m_persist; / 寫(xiě)文件持續(xù)時(shí)間int m_serial; /線(xiàn)程序號(hào)/從參數(shù)中獲得信息m_serial=(ThreadInfo*)(p)->serial;m_delay=(DWORD)(ThreadInfo*)(p)->delay*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p) ->persist*I

13、NTE_PER_SEC);Sleep(m_delay); /延遲等待printf("Writer thread %d sents the writing require.n",m_serial);/ 等待資源EnterCriticalSection(&RP_Write);/寫(xiě)文件printf("Writer thread %d begins to Write to the file.n",m_serial);Sleep(m_persist);/ 退出線(xiàn)程printf("Writer thread %d finished Writing

14、to the file.n",m_serial);/釋放資源LeaveCriticalSection(&RP_Write);/ 讀者優(yōu)先處理函數(shù)/file:文件名void ReaderPriority(char* file)DWORD n_thread=0; /線(xiàn)程數(shù)目DWORD thread_ID; /線(xiàn)程IDDWORD wait_for_all; /等待所有線(xiàn)程結(jié)束/互斥對(duì)象HANDLE h_Mutex;h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");/線(xiàn)程對(duì)象的數(shù)組HANDLE h_Thre

15、adMAX_THREAD_NUM;ThreadInfo thread_infoMAX_THREAD_NUM;readcount=0; / 初始化 readcountInitializeCriticalSection(&RP_Write); /初始化臨界區(qū)ifstream inFile;inFile.open(file); /打開(kāi)文件printf("Reader Priority:nn");while(inFile) /讀入每一個(gè)讀者、寫(xiě)者的信息 inFile>>thread_infon_thread.serial; inFile>>threa

16、d_infon_thread.entity; inFile>>thread_infon_thread.delay; inFile>>thread_infon_thread+.persist; inFile.get( );for(int i=0;i< (int)(n_thread);i+) if(thread_infoi.entity=READER | thread_infoi.entity='R') h_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReaderThread),&am

17、p;thread_infoi,0,&thread_ID); /創(chuàng)建讀者線(xiàn)程 else /創(chuàng)建寫(xiě)者線(xiàn)程h_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thread_infoi,0,&thread_ID); /等待所有線(xiàn)程結(jié)束wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);printf("All reader and writer have finished operating.n"

18、;);/ 寫(xiě)者優(yōu)先-讀者線(xiàn)程/p:讀者線(xiàn)程信息void WP_ReaderThread(void* p)/互斥變量HANDLE h_Mutex1;h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex1");HANDLE h_Mutex2;h_Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex2");DWORD wait_for_mutex1; /等待互斥變量所有權(quán)DWORD wait_for_mutex2;DWORD m_delay; / 延遲時(shí)間DWORD m_per

19、sist; / 讀文件持續(xù)時(shí)間int m_serial; /線(xiàn)程序號(hào)/從參數(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("Reader thread %d sents the reading require.n",m_serial);wait_for

20、_mutex1= WaitForSingleObject(h_Mutex1,-1);/進(jìn)入讀者臨界區(qū) EnterCriticalSection(&cs_Read);/ 阻塞互斥對(duì)象mutex2,保證對(duì)readcount的訪(fǎng)問(wèn)、修改互斥wait_for_mutex2= WaitForSingleObject(h_Mutex2,-1);/修改讀者數(shù)目readcount+;if(readcount=1) /如果是第一個(gè)讀者,等待寫(xiě)者寫(xiě)完 EnterCriticalSection(&cs_Write);ReleaseMutex(h_Mutex2); /釋放互斥信號(hào)mutex2/ 讓其他

21、讀者進(jìn)入臨界區(qū)LeaveCriticalSection(&cs_Write);ReleaseMutex(h_Mutex1);/讀文件printf("Reader thread %d begins to read file.n",m_serial);Sleep(m_persist);/ 退出線(xiàn)程printf("Reader thread %d finished reading file.n",m_serial);/ 阻塞互斥對(duì)象mutex2,保證對(duì)readcount的訪(fǎng)問(wèn)、修改互斥wait_for_mutex2= WaitForSingleObje

22、ct(h_Mutex2,-1);readcount-;if(readcount=0) / 最后一個(gè)讀者,喚醒寫(xiě)者 LeaveCriticalSection(&cs_Write);ReleaseMutex(h_Mutex2); /釋放互斥信號(hào)/ 寫(xiě)者優(yōu)先-寫(xiě)者線(xiàn)程/p:寫(xiě)者線(xiàn)程信息void WP_WriterThread(void* p)DWORD m_delay; / 延遲時(shí)間DWORD m_persist; / 寫(xiě)文件持續(xù)時(shí)間int m_serial; /線(xiàn)程序號(hào)DWORD wait_for_mutex3;/互斥對(duì)象HANDLE h_Mutex3;h_Mutex3= OpenMute

23、x(MUTEX_ALL_ACCESS,FALSE,"mutex3");/從參數(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("Writer thread %d sents the writing require.n",m_serial

24、);/ 阻塞互斥對(duì)象mutex3,保證對(duì)writecount的訪(fǎng)問(wèn)、修改互斥wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1);writecount+; /修改讀者數(shù)目if(writecount=1) /第一個(gè)寫(xiě)者,等待讀者讀完 EnterCriticalSection(&cs_Read);ReleaseMutex(h_Mutex3);/進(jìn)入寫(xiě)者臨界區(qū)EnterCriticalSection(&cs_Write);/寫(xiě)文件printf("Writer thread %d begins to Write to the fi

25、le.n",m_serial);Sleep(m_persist);/ 退出線(xiàn)程printf("Writer thread %d finishing Writing to the file.n",m_serial);/離開(kāi)臨界區(qū)LeaveCriticalSection(&cs_Write);/ 阻塞互斥對(duì)象mutex3,保證對(duì)writecount的訪(fǎng)問(wèn)、修改互斥wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1);writecount-; /修改讀者數(shù)目if(writecount=0) /寫(xiě)者寫(xiě)完,讀者可以讀 L

26、eaveCriticalSection(&cs_Read);ReleaseMutex(h_Mutex3);/ 寫(xiě)者優(yōu)先處理函數(shù)/file:文件名void WriterPriority(char* file)DWORD n_thread=0; /線(xiàn)程數(shù)目DWORD thread_ID; /線(xiàn)程IDDWORD wait_for_all; /等待所有線(xiàn)程結(jié)束/互斥對(duì)象HANDLE h_Mutex1;h_Mutex1=CreateMutex(NULL,FALSE,"mutex1");HANDLE h_Mutex2;h_Mutex2=CreateMutex(NULL,FALS

27、E,"mutex2");HANDLE h_Mutex3;h_Mutex3=CreateMutex(NULL,FALSE,"mutex3");/線(xiàn)程對(duì)象HANDLE h_ThreadMAX_THREAD_NUM;ThreadInfo thread_infoMAX_THREAD_NUM;readcount=0; / 初始化 readcountwritecount=0; / 初始化writecountInitializeCriticalSection(&cs_Write); /初始化臨界區(qū)InitializeCriticalSection(&c

28、s_Read);ifstream inFile;inFile.open(file); /打開(kāi)文件printf("Writer Priority:nn");while(inFile) /讀入每一個(gè)讀者、寫(xiě)者的信息 inFile>>thread_infon_thread.serial; inFile>>thread_infon_thread.entity; inFile>>thread_infon_thread.delay; inFile>>thread_infon_thread+.persist; inFile.get( );for(int i=0;i< (int)(n_thread);i+) if (thread_infoi.entity=READER | thread_infoi.entity='R') /創(chuàng)建讀者線(xiàn)程 h_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_W

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論