




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)驗(yàn)2 線程同步機(jī)制一、 實(shí)驗(yàn)?zāi)康模和ㄟ^觀察共享數(shù)據(jù)資源但不受控制的兩個(gè)線程的并發(fā)運(yùn)行輸出結(jié)果,體會(huì)同步機(jī)制的必要性和重要性。然后利用現(xiàn)有操作系統(tǒng)提供的同步機(jī)制編程實(shí)現(xiàn)關(guān)于該兩個(gè)線程的有序控制,同時(shí)要求根據(jù)同步機(jī)制的Peterson軟件解決方案嘗試自己編程實(shí)現(xiàn)同步機(jī)制和用于同一問題的解決,并基于程序運(yùn)行時(shí)間長(zhǎng)短比較兩種同步機(jī)制。二、 實(shí)驗(yàn)設(shè)計(jì)I基于給定銀行賬戶間轉(zhuǎn)賬操作模擬代碼作為線程執(zhí)行代碼,在主線程中創(chuàng)建兩個(gè)并發(fā)線程,編程實(shí)現(xiàn)并觀察程序運(yùn)行結(jié)果和予以解釋說明。II利用Windows互斥信號(hào)量操作函數(shù)解決上述線程并發(fā)問題,并分析、嘗試和討論線程執(zhí)行體中有關(guān)信號(hào)量操作
2、函數(shù)調(diào)用的正確位置。III根據(jù)同步機(jī)制的Peterson軟件解決方案嘗試自己編程實(shí)現(xiàn)線程同步機(jī)制和用于上述線程并發(fā)問題的解決,并基于程序運(yùn)行時(shí)間長(zhǎng)短將其與基于Windows互斥信號(hào)量的線程同步機(jī)制的效率展開比較。其間,可規(guī)定線程主體代碼循環(huán)執(zhí)行次三、 源程序清單和說明1未利用互斥信號(hào)量#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互斥信號(hào)量#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è)置訪問編號(hào)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¾編號(hào)+標(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未利用互斥信號(hào)量2利用Windows互斥信號(hào)量3同步機(jī)制的Peterson結(jié)果分析1沒有應(yīng)用互斥信號(hào)量對(duì)線程進(jìn)行并發(fā)控制,運(yùn)行會(huì)產(chǎn)生錯(cuò)誤。2利用Windows互斥信號(hào)量后,兩存取款線程可并發(fā)正確執(zhí)行。成功轉(zhuǎn)賬次。但加大了系統(tǒng)的時(shí)間開銷。時(shí)間效率低。3應(yīng)用同步機(jī)制的Peterson算法后,兩線程也可順利的并發(fā)執(zhí)行,成功轉(zhuǎn)賬次,但相對(duì)于Wi
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- T-ZRIA 002-2024 工業(yè)巡檢四足機(jī)器人通.用技術(shù)條件
- T-ZSM 0058-2024“領(lǐng)跑者”評(píng)價(jià)技術(shù)要求 飾面木質(zhì)墻板
- 二零二五年度林業(yè)林地經(jīng)營(yíng)權(quán)買賣合同
- T-ZJATA 0022-2024 土壤中揮發(fā)性有機(jī)物測(cè)定用便攜式氣相色譜-質(zhì)譜聯(lián)用儀
- T-ZJZYC 022-2024 靈芝工廠化生產(chǎn)技術(shù)規(guī)程
- 二零二五年度簽約主播與汽車廠商合作直播試駕體驗(yàn)協(xié)議
- 二零二五年度會(huì)展中心物業(yè)管理服務(wù)托管協(xié)議
- 二零二五年度新能源項(xiàng)目投資對(duì)賭協(xié)議
- 二零二五年度股東清算與清算資產(chǎn)評(píng)估及拍賣協(xié)議
- 二零二五年度創(chuàng)新創(chuàng)業(yè)團(tuán)隊(duì)員工合作協(xié)議書
- 2025年安徽職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫(kù)學(xué)生專用
- 2025年黑龍江農(nóng)墾職業(yè)學(xué)院?jiǎn)握新殬I(yè)傾向性測(cè)試題庫(kù)附答案
- 2025年黑龍江農(nóng)業(yè)工程職業(yè)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)完整版
- 小學(xué)科學(xué)點(diǎn)亮我的小燈泡省公開課一等獎(jiǎng)全國(guó)示范課微課金獎(jiǎng)?wù)n件
- 2023-2024學(xué)年高中信息技術(shù)必修一滬科版(2019)第三單元項(xiàng)目六《 解決溫標(biāo)轉(zhuǎn)換問題-認(rèn)識(shí)程序和程序設(shè)計(jì)語(yǔ)言》教學(xué)設(shè)計(jì)
- 2024預(yù)防流感課件完整版
- 校園食品安全培訓(xùn)課件
- 二年級(jí)下冊(cè)數(shù)學(xué)課件-第三單元 對(duì)稱圖形 ppt(29張) 北京版(2021秋)
- 六十四卦爻象全圖(彩色)(共6頁(yè))
- 《各種各樣的橋》ppt課件
- 機(jī)動(dòng)車維修經(jīng)營(yíng)備案表
評(píng)論
0/150
提交評(píng)論