




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、-1-1-北京郵電大學(xué)操作系統(tǒng)實(shí)驗(yàn)-實(shí)驗(yàn)報(bào)告班號(hào):姓名:學(xué)號(hào):實(shí)驗(yàn)日期:20門門18實(shí)驗(yàn)名稱:哲學(xué)家就餐問題(避免死鎖)一、實(shí)驗(yàn)?zāi)康?、熟悉LINUX的基本環(huán)境,了解LINUX下進(jìn)程和線程的實(shí)現(xiàn)三、實(shí)驗(yàn)內(nèi)容(1)安裝LINUX;(2)熟悉UNIX/LINUX的常用基本命令如Is、who、wc、pwd、ps、pstree、top,cat,cd,chgrp,chmod,chown,comm,cmp,cp,rm,diff,mv,rmdir等,了解系統(tǒng)的壞境。(3)利用pthread及系統(tǒng)的信號(hào)量(semaphore,h),實(shí)現(xiàn)哲學(xué)家進(jìn)餐問題(避免死鎖):四、項(xiàng)目要求及分析假如所有的訴學(xué)家都同時(shí)拿起左
2、側(cè)筷子,看到右側(cè)筷子不可用,又都放下左側(cè)筷子,等一會(huì)兒,又同時(shí)拿起左側(cè)筷子,如此這般,永遠(yuǎn)重復(fù)。對(duì)于這種情況,即所有的程序都在無(wú)限期地運(yùn)行,但是都無(wú)法取得任何進(jìn)展,即出現(xiàn)饑餓,所有哲學(xué)家都吃不上飯。規(guī)定在拿到左側(cè)的筷子后,先檢查右面的筷子是否可用。如果不可用,則先放下左側(cè)筷子,等一段時(shí)間再重復(fù)整個(gè)過(guò)程。分析:當(dāng)出現(xiàn)以卞情形,在某一個(gè)瞬間,所有的哲學(xué)家都同時(shí)啟動(dòng)這個(gè)算法,拿起左側(cè)的筷子,而看到右側(cè)筷子不可用,又都放下左側(cè)筷子,等一會(huì)兒,又同時(shí)拿起左側(cè)筷子如此這樣永遠(yuǎn)重復(fù)下去。對(duì)于這種情況,所有的程序都在運(yùn)行,但卻無(wú)法取得進(jìn)展,即出現(xiàn)饑餓,所有的哲學(xué)家都吃不上飯。解決死鎖問題:為了避免死鎖,把哲學(xué)
3、家分為三種狀態(tài),思考,饑餓,進(jìn)食,并且一次拿到兩只筷子,否則不拿。僅當(dāng)一個(gè)哲學(xué)家左右兩邊筷子都可用時(shí),才允許他拿筷子。這樣要么一次占有兩只筷子在吃面,然后釋放所有資源;要么不占用資源。這樣就不會(huì)導(dǎo)致死鎖了。五、具體實(shí)現(xiàn)5.1、流程圖(參考)5.2、添加函數(shù)的代碼(所用到的各子函數(shù))voidp_operate(intsemid,ushortseiraium)structsembufthesops;thesops.sem_num二seninum;thesopssem_op=l;thesops.SEm_flg二SEM_UND0;/thesopssem_flg=0:semop(semid,&theso
4、ps,1);voidvoperate(intsemid,ushortseiraium)structsembufthesops;thesops.sem_num二seninum;thesopssem_op=l;thesops.SEm_flg二SEM_UND0;/thesopssem_flg=0;semop(semid,&thesops,1);voidthink(intargi)#%d,Iwillthinkfor%dprintfClmphilosopher#%d,Iwillthinkfor%dsecondsnv,argi,argi+THINKTINE);sleep(argi+THINKTM);voi
5、deat(intargi)printf(/ZImphilosopher#%d,Iwill已a(bǔ)tfor%dsecondsnz/,argi,argi+EATTIME);sleep(argi+EATTIME);voidtest(intargi,char*state,intsemid)if(stateargi=HUNGRY&stateLEFTI=EATING&stateRIGHTEATING)stateargiSEATING;printf(/zHehe,philosopher#%dcan已a(bǔ)tnown,/,argi);v_operate(semid,argi);)else辻(stateargi!二HU
6、NGRY)printf(zzFaint,philosopher#%ddontwantto已a(bǔ)tnz,,argi);elseprintfCOh,philosopher#%dcanteatnow!n,argi);voidtake_forks(intargi,char*state,intsemid)properate(semid,PhNum);stateargiHUNGRY;printf(“Philosopher#%dishungryCanheEat.n?,,argi);test(argi,statsemid);v_operate(semid,PhNum);properate(semid,argi)
7、;voidput_forks(intargi,char*state,intsemid)properate(semid,PhNum);stateargi=THINKING;printf(“Philosopher#%dEat0KCanhisneighbours已a(bǔ)t.nargi);test(LEFT,statsemid);test(RIGHT,state,semid);v_operate(semid,PhNum);voidphilosopher(intargi,char*state,intsemid)while(1)-4-4think(argi);take_forks(argi,state,sem
8、id);eat(argi);put_forks(argi,state,semid);)5.3、修改原有函數(shù)部分代碼(頭文件變量定義主程序)#include#include#include#include#includesignalh#include#include#includestdlibh#definePhNum#defineLEFT#defineRIGHT5(PhNum+argil)%PhNum(argi+l)%PhNum#defineTHINKING0#defineHUNGRY1#defineEATING2#defineTHINKTINE4#defineEATTINE3unionsei
9、nunintval;struetsemid_ds*buf;ushort*array;intmain(intargc,char*argv)intsemkey,shmkey,semid,shmid,fori,forki,mynum;char*state;unionsemunargl;structshmid_dsarg2;ushortarrayPhNum+1;if(semid=semget(IPC.PRIVATE,PhNum+1,06001IPC.CREAT)二二一1)perror(zzSemgeterror!”);exit(l);辻(shmid=shmget(IPC.PRIVATE,PhNum,0
10、6001IPC.CREAT)二二T)semct1(semid,0,IPC_RMID,argl);perror(Z/Shmgeterror!“);exit(l);)arrayPhNum=l;for(fori=0:foriPhIIum;fori+)arrayfori=0:/*設(shè)置信號(hào)量數(shù)組初值*/arglarray二array;辻(semct1(semid,PhNum+1,SETALL,argl)=l)/*設(shè)置各信號(hào)量的初始值*/semct1(semid,0,IPC_RMID,argl);shmct1(shmid,IPC_RMID,&arg2);perrorCSetdefaulterror1);e
11、xit(l);if(state=shmat(shmid,0,0)=(char*)-1)/*將共享內(nèi)存映射到當(dāng)前進(jìn)程地址空間*/semct1(semid,0,IPC_RMID,argl);shmct1(shmid,IPC_RMID,&arg2);perror(Shmaterror!);exit(l);)for(fori=0;foriPhNum;fori+)stateforiTHINKING;/*初始化狀態(tài)數(shù)組*/signal(SIGINT,SIG_IGN);/*將進(jìn)程對(duì)中斷信號(hào)的處理設(shè)為忽略*/for(fori=l;fori=PhNum;fori+)/*創(chuàng)建5個(gè)哲學(xué)家子進(jìn)程*/wh訂e(fork
12、i=fork0)=1);辻(!forki)/*子進(jìn)程執(zhí)行代碼*/mynum=foril;signal(SIGINT,SIG.DFL);/*設(shè)置子進(jìn)程對(duì)SIGINT信號(hào)的處理過(guò)程*/-4-4break;辻(forki)vdiile(wait(char*)0)!=1);/*等待子進(jìn)程執(zhí)行結(jié)束,此處的(char*)0相當(dāng)于NULL,即忽略返回狀態(tài)*/semct1(semid,0,IPC_RMID,argl);shmdt(state);shmct1(shndd,IPC_RMID,&arg2);printf(z/nTmmonitor.Allisovern);)elsephilosopher(mynum,
13、state,semid);/*子進(jìn)程調(diào)用*/六、調(diào)試運(yùn)行結(jié)果唾)厲&創(chuàng)盟11月21日星期一10:21UfQmuyeTOQmuyemuye-laptop:文件(F)=6(V)終床E斜助(H)muy&?iTfuye-laptop:Sgccphilosopher.cImuyerouye-laptop:-S./a.outII*!philosopherImphilosophermuy&?iTfuye-laptop:Sgccphilosopher.cImuyerouye-laptop:-S./a.outII*!philosopherImphilosopherrmphilosopherInphilosoph
14、erImphilosopher#2.1willthink*14willthink#3,1willthink#4.1willthink*9,1willthinkfor6secondsfor5secondsfor7secondsfor8secondsfor4secondsPhilosopher#eishungry.canheeat.iHeherphilosophercaneatnowphilosopher#8,1willeatfor3secondsPhilosopher#1ishungry.Canheeat.Oh,philosopher#1canteatnow!Philosopher#2ishun
15、gry.canheeat.Hehe,philosopher#2caneatnowImphilosopher#2,1willeatfor5secondsPhilosopher#3ishungry.canheeat.:0h,philosopher#3canteatnow!.PhilosophereatO.K.CanhisneighbourseatFaint,philosopher#4dontwanttoeatOh,philos叩her#1canteatnow!rmphilosopher#8,1willthinkfor4secondsPhilosopher#4ishungry.Canheeat.Heherphllosopher#4caneatnow-HImph譏osopher#4,1willeatfor7secondsjPhilosopher2eatO.K.Canhisneighbourseat.Hehe.philosopher#1caneatnowOh,philosopher#3canteatnow!Imphilosopher#2,1willthinkfor6secondsll*mphilosopher#1,1willeatfor4seconds(PhilosopherMishungry
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 酒店資產(chǎn)投資與經(jīng)營(yíng)管理合伙協(xié)議書二零二五
- 二零二五年度私人住宅裝修工人安全責(zé)任合同
- 2025年度海洋資源開發(fā)橫向課題執(zhí)行協(xié)議
- 二零二五年度小程序游戲運(yùn)營(yíng)合作協(xié)議
- 2025年度電子元器件采購(gòu)合同主要內(nèi)容簡(jiǎn)述
- 二零二五年度購(gòu)房合同定金支付及變更協(xié)議書
- 2025年度酒店員工勞動(dòng)權(quán)益保障合同
- 二零二五年度綠色建筑股權(quán)協(xié)議及合伙人合作開發(fā)協(xié)議
- 2025年度美發(fā)店員工工傷事故處理勞動(dòng)合同
- 空調(diào)安裝工勞動(dòng)合同
- 北京聯(lián)合大學(xué)《電力電子技術(shù)》2023-2024學(xué)年期末試卷
- 公安機(jī)關(guān)保密協(xié)議
- 小學(xué)語(yǔ)文學(xué)科集體備課實(shí)施方案
- 2024-2025學(xué)年初中信息技術(shù)(信息科技)七年級(jí)全一冊(cè)義務(wù)教育版(2024)教學(xué)設(shè)計(jì)合集
- 教學(xué)設(shè)計(jì)初中勞動(dòng)教育創(chuàng)意設(shè)計(jì)的教學(xué)設(shè)計(jì)
- 水利水電工程單元工程施工質(zhì)量驗(yàn)收評(píng)定表及填表說(shuō)明
- 《欣賞 中華人民共和國(guó)國(guó)歌(簡(jiǎn)譜、五線譜)》課件
- 初三化學(xué)一輪復(fù)習(xí)計(jì)劃
- 關(guān)于進(jìn)一步加強(qiáng)路基路面施工質(zhì)量的通知
- (正式版)YS∕T 5040-2024 有色金屬礦山工程項(xiàng)目可行性研究報(bào)告編制標(biāo)準(zhǔn)
- AQ/T 2080-2023 金屬非金屬地下礦山在用人員定位系統(tǒng)安全檢測(cè)檢驗(yàn)規(guī)范(正式版)
評(píng)論
0/150
提交評(píng)論