操作系統(tǒng)實(shí)驗(yàn)三 進(jìn)程同步的經(jīng)典算法_第1頁
操作系統(tǒng)實(shí)驗(yàn)三 進(jìn)程同步的經(jīng)典算法_第2頁
操作系統(tǒng)實(shí)驗(yàn)三 進(jìn)程同步的經(jīng)典算法_第3頁
操作系統(tǒng)實(shí)驗(yàn)三 進(jìn)程同步的經(jīng)典算法_第4頁
操作系統(tǒng)實(shí)驗(yàn)三 進(jìn)程同步的經(jīng)典算法_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、寧德師范學(xué)院計(jì)算機(jī)系實(shí)驗(yàn)報(bào)告(20142015學(xué)年 第二學(xué)期)課程名稱 操作系統(tǒng) 實(shí)驗(yàn)名稱 實(shí)驗(yàn)三 進(jìn)程同步的經(jīng)典算法專 業(yè) 計(jì)算機(jī)科學(xué)與技術(shù)(非師)年 級 2012級 學(xué)號 B2012102147 姓名 王 秋 指導(dǎo)教師 王遠(yuǎn)帆 實(shí)驗(yàn)日期 2015-04-9 實(shí)驗(yàn)?zāi)康呐c要求:(1) 回顧系統(tǒng)進(jìn)程、線程的有關(guān)概念,加深對Windows 2000線程的理解。(2)了解互斥體對象,通過對生產(chǎn)者消費(fèi)者等進(jìn)程間同步與互斥經(jīng)典算法的實(shí)現(xiàn),加深對P、V原語以及利用P、V原語進(jìn)行進(jìn)程間同步與互斥操作的理解。實(shí)驗(yàn)設(shè)備(環(huán)境):(1)一臺安裝有Cygwin Terminal的計(jì)算機(jī)(2) Windows XP操

2、作系統(tǒng)(3)VC+6.0實(shí)驗(yàn)內(nèi)容:(1)生產(chǎn)者消費(fèi)者問題(2)讀者寫者問題根據(jù)實(shí)驗(yàn)(1)中所熟悉的P、V原語對應(yīng)的實(shí)際Windows API函數(shù),并參考教材中讀者、寫者問題的算法原理,嘗試?yán)肳indows API函數(shù)實(shí)現(xiàn)第一類讀者寫者問題(讀者優(yōu)先)。實(shí)驗(yàn)步驟、實(shí)驗(yàn)結(jié)果及分析: (1)生產(chǎn)者消費(fèi)者問題1:創(chuàng)建一個(gè)“Win32 Consol Application”工程,然后拷貝清單3-1中的程序,編譯成可執(zhí)行文件。2:在“命令提示符”窗口運(yùn)行步驟1中生成的可執(zhí)行文件。運(yùn)行結(jié)果:在命令窗口運(yùn)行過程圖 3-1運(yùn)行結(jié)果圖3:仔細(xì)閱讀源程序,找出創(chuàng)建線程的WINDOWS API函數(shù),回答下列問題:線

3、程的第一個(gè)執(zhí)行函數(shù)是什么(從哪里開始執(zhí)行)?它位于創(chuàng)建線程的API函數(shù)的第幾個(gè)參數(shù)中?第一個(gè)執(zhí)行函數(shù)是Producer;位于第三個(gè)參數(shù)中。4:修改清單3-1中的程序,調(diào)整生產(chǎn)者線程和消費(fèi)者線程的個(gè)數(shù),使得消費(fèi)者數(shù)目大與生產(chǎn)者,看看結(jié)果有何不同。運(yùn)行結(jié)果:3-1修改后結(jié)果圖從中你可以得出什么結(jié)論:5:修改清單3-1中的程序,按程序注釋中的說明修改信號量EmptySemaphore的初始化方法,看看結(jié)果有何不同。運(yùn)行結(jié)果:6:根據(jù)步驟4的結(jié)果,并查看MSDN,回答下列問題1) CreateMutex中有幾個(gè)參數(shù),各代表什么含義。有3個(gè)參數(shù) 1. LPSECURITY_ATTRIBUTES IpMu

4、texAttributes代表安全屬性的指針 2. BOOL bInitialOwner 代表布爾bInitialOwner 3. LPCTSTR IpName 代表LPCTSTR類型IpName2)CreateSemaphore中有幾個(gè)參數(shù),各代表什么含義,信號量的初值在第幾個(gè)參數(shù)中。有4個(gè)參數(shù):1、表示采用不允許繼承的默認(rèn)描述符 2、設(shè)置信號機(jī)的初始計(jì)數(shù) 3、設(shè)置信號機(jī)的最大計(jì)數(shù) 4、指定信號機(jī)對象的名稱。3)程序中P、V原語所對應(yīng)的實(shí)際Windows API函數(shù)是什么,寫出這幾條語句。4)CreateMutex能用CreateSemaphore替代嗎?嘗試修改程序3-1,將信號量Mute

5、x完全用CreateSemaphore及相關(guān)函數(shù)實(shí)現(xiàn)。寫出要修改的語句:P:Take() V:Append()4)CreateMutex能用CreateSemaphore替代嗎?嘗試修改程序3-1,將信號量Mutex完全用CreateSemaphore及相關(guān)函數(shù)實(shí)現(xiàn)。寫出要修改的語句:Mutex=CreateSemaphore(NULL,false,false,NULL); (2) 讀者寫者問題根據(jù)實(shí)驗(yàn)(1)中所熟悉的P、V原語對應(yīng)的實(shí)際Windows API函數(shù),并參考教材中讀者、寫者問題的算法原理,嘗試?yán)肳indows API函數(shù)實(shí)現(xiàn)第一類讀者寫者問題(讀者優(yōu)先)。拷貝清單3-2中的程序

6、,編譯成可執(zhí)行文件,運(yùn)行截圖:讀者寫者運(yùn)行結(jié)果 程序清單:程序3-1代碼:#include #include const unsigned short SIZE_OF_BUFFER = 2; unsigned short ProductID = 0; unsigned short ConsumeID = 0; unsigned short in = 0; unsigned short out = 0; int bufferSIZE_OF_BUFFER; bool p_ccontinue = true; HANDLE Mutex; HANDLE FullSemaphore; HANDLE Emp

7、tySemaphore; DWORD WINAPI Producer(LPVOID); DWORD WINAPI Consumer(LPVOID); int main() Mutex = CreateMutex(NULL,FALSE,NULL);EmptySemaphore = CreateSemaphore(NULL,SIZE_OF_BUFFER,SIZE_OF_BUFFER,NULL); FullSemaphore = CreateSemaphore(NULL,0,SIZE_OF_BUFFER,NULL); const unsigned short PRODUCERS_COUNT = 3;

8、 /生產(chǎn)者的個(gè)數(shù) const unsigned short CONSUMERS_COUNT = 1; /消費(fèi)者的個(gè)數(shù) /總的線程數(shù) const unsigned short THREADS_COUNT = PRODUCERS_COUNT+CONSUMERS_COUNT; HANDLE hThreadsTHREADS_COUNT; DWORD producerIDPRODUCERS_COUNT; DWORD consumerIDCONSUMERS_COUNT; /創(chuàng)建生產(chǎn)者線程 for (int i=0;iPRODUCERS_COUNT;+i) hThreadsi=CreateThread(NU

9、LL,0,Producer,NULL,0,&producerIDi); if (hThreadsi=NULL) return -1; /創(chuàng)建消費(fèi)者線程 for (i=0;iCONSUMERS_COUNT;+i) hThreadsPRODUCERS_COUNT+i=CreateThread(NULL,0,Consumer,NULL,0,&consumerIDi); if (hThreadsi=NULL) return -1; while(p_ccontinue) if(getchar() /按回車后終止程序運(yùn)行 p_ccontinue = false; return 0;/生產(chǎn)一個(gè)產(chǎn)品。void

10、 Produce() std:cout std:endl Producing +ProductID . ; std:cout Succeed std:endl;/把新生產(chǎn)的產(chǎn)品放入緩沖區(qū)void Append() std:cerr Appending a product . ; bufferin = ProductID; in = (in+1)%SIZE_OF_BUFFER; std:cerr Succeed std:endl; /輸出緩沖區(qū)當(dāng)前的狀態(tài) for (int i=0;iSIZE_OF_BUFFER;+i) std:cout i : bufferi; if (i=in) std:co

11、ut - 生產(chǎn); if (i=out) std:cout - 消費(fèi); std:cout std:endl; /從緩沖區(qū)中取出一個(gè)產(chǎn)品void Take() std:cerr Taking a product . ; ConsumeID = bufferout;bufferout = 0; out = (out+1)%SIZE_OF_BUFFER; std:cerr Succeed std:endl; /輸出緩沖區(qū)當(dāng)前的狀態(tài) for (int i=0;iSIZE_OF_BUFFER;+i) std:cout i : bufferi; if (i=in) std:cout - 生產(chǎn); if (i=

12、out) std:cout - 消費(fèi); std:cout std:endl; /消耗一個(gè)產(chǎn)品void Consume() std:cout Consuming ConsumeID . ; std:cout Succeed std:endl;/生產(chǎn)者DWORD WINAPI Producer(LPVOID lpPara) while(p_ccontinue) WaitForSingleObject(EmptySemaphore,INFINITE);/p(empty); WaitForSingleObject(Mutex,INFINITE);/p(mutex); Produce(); Append

13、(); Sleep(1500); ReleaseMutex(Mutex);/V(mutex); ReleaseSemaphore(FullSemaphore,1,NULL);/V(full); return 0;/消費(fèi)者DWORD WINAPI Consumer(LPVOID lpPara) while(p_ccontinue) WaitForSingleObject(FullSemaphore,INFINITE);/P(full); WaitForSingleObject(Mutex,INFINITE);/P(mutex); Take(); Consume(); Sleep(1500); R

14、eleaseMutex(Mutex);/V(mutex); ReleaseSemaphore(EmptySemaphore,1,NULL);/V(empty); return 0;3-2代碼:#include #include #include #include #include #include #define MAX_PERSON 100 /最多100人#define READER 0 /讀者#define WRITER 1 /寫者#define END -1 /結(jié)束#define R READER#define W WRITERtypedef struct _Person HANDLE

15、m_hThread;/定義處理線程的句柄 int m_nType;/進(jìn)程類型(讀寫) int m_nStartTime;/開始時(shí)間 int m_nWorkTime;/運(yùn)行時(shí)間 int m_nID;/進(jìn)程號Person;Person g_PersonsMAX_PERSON;int g_NumPerson = 0;long g_CurrentTime= 0;/基本時(shí)間片數(shù)int g_PersonLists = /進(jìn)程隊(duì)列 1, W, 3, 5, 2, W, 16, 5, 3, R, 5, 2, 4, W, 6, 5, 5, R, 4, 3, 6, R, 17,7, END,;int g_NumOf

16、Reading = 0;int g_NumOfWriteRequest = 0;/申請寫進(jìn)程的個(gè)數(shù)HANDLE g_hReadSemaphore;/讀者信號HANDLE g_hWriteSemaphore;/寫者信號bool finished = false; /所有的讀完成/bool wfinished = false; /所有的寫完成void CreatePersonList(int *pPersonList);bool CreateReader(int StartTime,int WorkTime,int ID);bool CreateWriter(int StartTime,int W

17、orkTime,int ID);DWORD WINAPI ReaderProc(LPVOID lpParam);DWORD WINAPI WriterProc(LPVOID lpParam);int main() g_hReadSemaphore = CreateSemaphore(NULL,1,100,NULL); /創(chuàng)建信號燈,當(dāng)前可用的資源數(shù)為1,最大為100 g_hWriteSemaphore = CreateSemaphore(NULL,1,100,NULL); /創(chuàng)建信號燈,當(dāng)前可用的資源數(shù)為1,最大為100 CreatePersonList(g_PersonLists); / C

18、reate All the reader and writers printf(Created all the reader and writern.n); g_CurrentTime = 0; while(true) g_CurrentTime+;Sleep(300); / 300 msprintf(CurrentTime = %dn,g_CurrentTime);if(finished) return 0; / return 0;void CreatePersonList(int *pPersonLists) int i=0; int *pList = pPersonLists; bool

19、 Ret; while(pList0 != END) switch(pList1) case R: Ret = CreateReader(pList2,pList3,pList0);/351,w452,523,654 break; case W: Ret = CreateWriter(pList2,pList3,pList0); break;if(!Ret) printf(Create Person %d is wrongn,pList0); pList += 4; / move to next person listDWORD WINAPI ReaderProc(LPVOID lpParam

20、)/讀過程 Person *pPerson = (Person*)lpParam; / wait for the start time while(g_CurrentTime != pPerson-m_nStartTime) /讀操作還沒有到達(dá)執(zhí)行時(shí)間,則等待 printf(Reader %d is Requesting .n,pPerson-m_nID); printf(nn*n); / wait for the write request/* while(g_NumOfWriteRequest != 0) /g_NumOfWriteRequest != 0 表示現(xiàn)在正在寫,不能讀 */該語

21、句在寫者優(yōu)先的時(shí)候是認(rèn)為寫者優(yōu)先級高于讀者,在有寫者的時(shí)候讀者需/要等候,而在讀者優(yōu)先的時(shí)候,不用判斷是否存在寫者,有讀者時(shí)即開始讀操/作。 WaitForSingleObject(g_hReadSemaphore,INFINITE);/等待g_hReadSemaphore讀信號,即當(dāng)g_hReadSemaphore有信號時(shí)等待結(jié)束 相當(dāng)于p操作 if(g_NumOfReading =0) WaitForSingleObject(g_hWriteSemaphore,INFINITE); /當(dāng)?shù)谝粋€(gè)讀者到了,如果g_hWriteSemaphore信號燈滅了,說明有寫者再寫,讀者必須等待。即互斥寫

22、操作 g_NumOfReading+; ReleaseSemaphore(g_hReadSemaphore,1,NULL); /還有讀者,但是允許下一個(gè)讀進(jìn)程讀取,相當(dāng)于V操作/ modify the readers real start time pPerson-m_nStartTime = g_CurrentTime; printf(Reader %d is Reading the Shared Buffer.n,pPerson-m_nID); printf(nn*n); while(g_CurrentTime m_nStartTime + pPerson-m_nWorkTime) / .

23、 perform read operations printf(Reader %d is Exit.n,pPerson-m_nID); printf(nn*n); WaitForSingleObject(g_hReadSemaphore,INFINITE); g_NumOfReading-; if(g_NumOfReading = 0) ReleaseSemaphore(g_hWriteSemaphore,1,NULL);/此時(shí)沒有讀者,可以寫 ReleaseSemaphore(g_hReadSemaphore,1,NULL); if(pPerson-m_nID = 4) finished =

24、 true; /所有的讀寫完成 ExitThread(0); return 0;DWORD WINAPI WriterProc(LPVOID lpParam) Person *pPerson = (Person*)lpParam; / wait for the start time while(g_CurrentTime != pPerson-m_nStartTime) printf(Writer %d is Requesting .n,pPerson-m_nID); printf(nn*n); /g_NumOfWriteRequest+;/在寫者優(yōu)先的時(shí)候需要用自加來初始信號值,而在讀者優(yōu)先

25、的時(shí)是通過讀者操作來控制信號值 WaitForSingleObject(g_hWriteSemaphore,INFINITE); / modify the writers real start time pPerson-m_nStartTime = g_CurrentTime; printf(Writer %d is Writting the Shared Buffer.n,pPerson-m_nID); while(g_CurrentTime m_nStartTime + pPerson-m_nWorkTime) / . perform write operationsprintf(Writ

26、er %d is Exit.n,pPerson-m_nID);printf(nn*n);/g_NumOfWriteRequest-;ReleaseSemaphore(g_hWriteSemaphore,1,NULL);if(pPerson-m_nID = 4) finished = true;/所有的讀寫完成ExitThread(0);return 0;bool CreateReader(int StartTime,int WorkTime,int ID) DWORD dwThreadID; if(g_NumPerson = MAX_PERSON) return false; Person *

27、pPerson = &g_Personsg_NumPerson; pPerson-m_nID = ID; pPerson-m_nStartTime = StartTime; pPerson-m_nWorkTime = WorkTime; pPerson-m_nType = READER; g_NumPerson+; / Create an New Thread pPerson-m_hThread= CreateThread(NULL,0,ReaderProc,(LPVOID)pPerson,0,&dwThreadID); if(pPerson-m_hThread = NULL) return false; return true;bool CreateWriter(int StartTime,int WorkTime,int ID) DWORD dwThreadID; if(g_NumPerson = MAX_PERSON) return false; Person *pPerson = &g_Personsg_NumPerson; pPerson-m_nID =

溫馨提示

  • 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論