實(shí)驗(yàn)2-線程同步機(jī)制_第1頁
實(shí)驗(yàn)2-線程同步機(jī)制_第2頁
實(shí)驗(yàn)2-線程同步機(jī)制_第3頁
實(shí)驗(yàn)2-線程同步機(jī)制_第4頁
實(shí)驗(yàn)2-線程同步機(jī)制_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)驗(yàn)2 線程同步機(jī)制一、 實(shí)驗(yàn)?zāi)康模和ㄟ^觀察共享數(shù)據(jù)資源但不受控制的兩個(gè)線程的并發(fā)運(yùn)行輸出結(jié)果,體會同步機(jī)制的必要性和重要性。然后利用現(xiàn)有操作系統(tǒng)提供的同步機(jī)制編程實(shí)現(xiàn)關(guān)于該兩個(gè)線程的有序控制,同時(shí)要求根據(jù)同步機(jī)制的Peterson軟件解決方案嘗試自己編程實(shí)現(xiàn)同步機(jī)制和用于同一問題的解決,并基于程序運(yùn)行時(shí)間長短比較兩種同步機(jī)制。二、 實(shí)驗(yàn)設(shè)計(jì)I基于給定銀行賬戶間轉(zhuǎn)賬操作模擬代碼作為線程執(zhí)行代碼,在主線程中創(chuàng)建兩個(gè)并發(fā)線程,編程實(shí)現(xiàn)并觀察程序運(yùn)行結(jié)果和予以解釋說明。II利用Windows互斥信號量操作函數(shù)解決上述線程并發(fā)問題,并分析、嘗試和討論線程執(zhí)行體中有關(guān)信號量操作

2、函數(shù)調(diào)用的正確位置。III根據(jù)同步機(jī)制的Peterson軟件解決方案嘗試自己編程實(shí)現(xiàn)線程同步機(jī)制和用于上述線程并發(fā)問題的解決,并基于程序運(yùn)行時(shí)間長短將其與基于Windows互斥信號量的線程同步機(jī)制的效率展開比較。其間,可規(guī)定線程主體代碼循環(huán)執(zhí)行次三、 源程序清單和說明1未利用互斥信號量#include <windows.h>#include <stdlib.h>#include <stdio.h>int nAccount1 = 0, nAccount2 = 0;int nLoop = 0;int nTemp1, nTemp2, nRandom;DWORD W

3、INAPI ThreadFunc(HANDLE Thread)donTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;nLoop+; while (nAccount1 + nAccount2) = 0);printf("循環(huán)次數(shù)為%dn", nLoop);return 0;int main() HANDLE Thread2; Thread0 = CreateThread(NULL,0,ThreadFun

4、c,NULL,0,NULL); Thread1 = CreateThread(NULL,0,ThreadFunc,NULL,0,NULL); WaitForMultipleObjects(2,Thread,TRUE,INFINITE); CloseHandle(Thread); return 0;2利用Windows互斥信號量#include <windows.h>#include <stdlib.h>#include <stdio.h>#define COUNT int nAccount1 = 0, nAccount2 = 0;HANDLE mutex;D

5、WORD WINAPI ThreadFunc(HANDLE Thread)int nLoop = 0;int nTemp1, nTemp2, nRandom;WaitForSingleObject(mutex,INFINITE);donTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;nLoop+; ReleaseMutex(mutex); WaitForSingleObject(mutex,INFINITE);while

6、 (nAccount1 + nAccount2) = 0&&nLoop < COUNT);ReleaseMutex(mutex);WaitForSingleObject(mutex,INFINITE);printf("循環(huán)次數(shù)為%dn", nLoop);ReleaseMutex(mutex);return 0;int main() HANDLE Thread2; DWORD start, end; start = GetTickCount(); mutex = CreateMutex(NULL,FALSE,NULL); Thread0 = Create

7、Thread(NULL,0,ThreadFunc,NULL,0,NULL); Thread1 = CreateThread(NULL,0,ThreadFunc,NULL,0,NULL); WaitForMultipleObjects(2,Thread,TRUE,INFINITE); end = GetTickCount(); printf("總共用時(shí)%ldn",end-start); CloseHandle(Thread); CloseHandle(mutex); return 0;3同步機(jī)制的Peterson#include <windows.h>#inclu

8、de <stdlib.h>#include <stdio.h>#define COUNT int nAccount1 = 0, nAccount2 = 0, flag2, turn;int nLoop = 0;int nTemp1, nTemp2, nRandom;/HANDLE mutex;void request ( int id )int other = 1 - id;flagid = 1;turn = other;while ( flagother = 1 && turn = other );DWORD WINAPI ThreadFunc0(HA

9、NDLE Thread)request(0);donTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;nLoop+; flag0 = 0; request(0);while (nAccount1 + nAccount2) = 0 && nLoop < COUNT);flag0 = 0;request(0);flag0 = 0;printf("循環(huán)次數(shù)為%dn", nLoop);re

10、turn 0;DWORD WINAPI ThreadFunc1(HANDLE Thread)request(1);donTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;nLoop+; flag1 = 0; request(1);while (nAccount1 + nAccount2) = 0 && nLoop < COUNT);flag1 = 0;request(1);flag1 = 0;prin

11、tf("循環(huán)次數(shù)為%dn", nLoop);return 0;int main() HANDLE Thread2; DWORD start, end; start = GetTickCount(); Thread0 = CreateThread(NULL,0,ThreadFunc0,NULL,0,NULL); Thread1 = CreateThread(NULL,0,ThreadFunc1,NULL,0,NULL); WaitForMultipleObjects(2,Thread,TRUE,INFINITE); end = GetTickCount(); printf(&

12、quot;總共用時(shí)%ldn",end-start); CloseHandle(Thread); return 0;四、 算法及關(guān)鍵數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)1. 銀行賬戶間轉(zhuǎn)賬操作模擬int nAccount1 = 0, nAccount2 = 0; /主線程創(chuàng)建的全局變量int nLoop = 0;int nTemp1, nTemp2, nRandom;donTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;nLoop+;

13、while (nAccount1 + nAccount2) = = 0);printf("循環(huán)次數(shù)為%dn", nLoop);2. 進(jìn)程互斥算法1¾設(shè)置訪問編號Var turn: integer :=i;repeat while turn¹i do no_op; 臨界區(qū) turn:=j; until false;3. 進(jìn)程互斥算法2¾設(shè)置訪問標(biāo)志Var flagi, flagj: boolean :=false, false;repeat while flagj do no_op; flagi := true; 臨界區(qū) flagi := fal

14、se;until false;4. 進(jìn)程互斥算法3¾設(shè)置訪問標(biāo)志Var flagi, flagj: boolean :=false, false;repeat flagi := true; while flagj do no_op; 臨界區(qū) flagi := false;until false;5. 進(jìn)程互斥算法4¾編號+標(biāo)志Var flagi, flagj: boolean; turn: integer; repeat flagi := true; turn := j; while (flagj and turn=j) do no_op; 臨界區(qū) flagi := false;until false;五、 實(shí)驗(yàn)過程中間結(jié)果屏幕截圖實(shí)驗(yàn)結(jié)果1未利用互斥信號量2利用Windows互斥信號量3同步機(jī)制的Peterson結(jié)果分析1沒有應(yīng)用互斥信號量對線程進(jìn)行并發(fā)控制,運(yùn)行會產(chǎn)生錯(cuò)誤。2利用Windows互斥信號量后,兩存取款線程可并發(fā)正確執(zhí)行。成功轉(zhuǎn)賬次。但加大了系統(tǒng)的時(shí)間開銷。時(shí)間效率低。3應(yīng)用同步機(jī)制的Peterson算法后,兩線程也可順利的并發(fā)執(zhí)行,成功轉(zhuǎn)賬次,但相對于Wi

溫馨提示

  • 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

提交評論