




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、操作系統(tǒng)試驗(yàn)報(bào)告班級(jí)物聯(lián)網(wǎng) 1302 班學(xué)號(hào)姓名試驗(yàn) 3 進(jìn)程同步和通信 - 生產(chǎn)者和消費(fèi)者問題模擬1. 目的:調(diào)試、修改、運(yùn)行模擬程序,通過形象化的狀態(tài)顯示,使同學(xué)懂得進(jìn)程的概念,明白同步和通信的過程, 把握進(jìn)程通信和同步的機(jī)制, 特殊是利用緩沖區(qū)進(jìn)行同步和通信的過程;通過補(bǔ)充新功能,使同學(xué)能敏捷運(yùn)用相關(guān)學(xué)問,培育創(chuàng)新才能;2. 內(nèi)容及要求:1 調(diào)試、運(yùn)行模擬程序;2 發(fā)覺并修改程序中不完善的地方;3 修改程序,使用隨機(jī)數(shù)掌握創(chuàng)建生產(chǎn)者和消費(fèi)者的過程;4 在原先程序的基礎(chǔ)上, 加入緩沖區(qū)的寫互斥掌握功能,模擬多個(gè)進(jìn)程存取一個(gè)公共緩沖區(qū),當(dāng)有進(jìn)程正在寫緩沖區(qū)時(shí),其他要拜訪該緩沖區(qū)的進(jìn)程必需等待
2、,當(dāng)有進(jìn)程正在讀取 緩沖區(qū)時(shí),其他要求讀取的進(jìn)程可以拜訪,而要求寫的進(jìn)程應(yīng)當(dāng)?shù)却? 完成 1 、2 、3)功能的 , 得基本分 , 完成 4 功能的加 2 分, 有其它功能改進(jìn)的再加 2 分3. 程序說明:本程序是模擬兩個(gè)進(jìn)程,生產(chǎn)者(producer )和消費(fèi)者 Consumer工作;生產(chǎn)者每次產(chǎn)生一個(gè)數(shù)據(jù),送入緩沖區(qū)中;消費(fèi)者每次從緩沖區(qū)中取走一個(gè)數(shù)據(jù);緩沖區(qū)可以容納 8 個(gè)數(shù)據(jù);由于緩沖區(qū)是有限的,因此當(dāng)其滿了時(shí)生產(chǎn)者進(jìn)程應(yīng)當(dāng)?shù)却?而空時(shí),消費(fèi)者進(jìn) 程應(yīng)當(dāng)?shù)却?;?dāng)生產(chǎn)者向緩沖區(qū)放入了一個(gè)數(shù)據(jù),應(yīng)喚醒正在等待的消費(fèi)者進(jìn)程,同樣,當(dāng)消費(fèi)者取走一個(gè)數(shù)據(jù)后,應(yīng)喚醒正在等待的生產(chǎn)者進(jìn)程;就是生產(chǎn)
3、者和消費(fèi)者之間的同步;每次寫入和讀出數(shù)據(jù)時(shí),都將讀和寫指針加一;當(dāng)讀寫指針同樣時(shí),又一起退回起點(diǎn);當(dāng)寫指針指向最終時(shí),生產(chǎn)者就等待;當(dāng)讀指針為零時(shí),再次要讀取的消費(fèi)者也應(yīng)當(dāng)?shù)却?;為?jiǎn)潔起見,每次產(chǎn)生的數(shù)據(jù)為 是通過運(yùn)行者使用鍵盤來實(shí)現(xiàn)的;4. 程序使用的數(shù)據(jù)結(jié)構(gòu)0-99 的整數(shù),從 0 開頭,次序遞增;兩個(gè)進(jìn)程的調(diào)度進(jìn)程掌握塊:包括進(jìn)程名,進(jìn)程狀態(tài)和執(zhí)行次數(shù);緩沖區(qū):一個(gè)整數(shù)數(shù)組;緩沖區(qū)說明塊:包括類型,讀指針,寫指針,讀等待指針和寫等待指針;5. 程序使用說明啟動(dòng)程序后,假如使用p 鍵就運(yùn)行一次生產(chǎn)者進(jìn)程,使用c 鍵就運(yùn)行一次消費(fèi)者進(jìn)程;通過屏幕可以觀看到兩個(gè)進(jìn)程的狀態(tài)和緩沖區(qū)變化的情形;6
4、. 試驗(yàn)流程圖源程序#include #include #include #define PIPESIZE 8 enum Status RUN, WAIT, READY ; #define NORMAL 0 #define SLEEP 1 #define AWAKE 2 struct PCB char name3; enum Status status; int time; ; struct waitqueue struct PCB pcb; struct waitqueue *next; ; struct PCB *producer; /* write wait point */ struc
5、t PCB *consumer; /* read wait point */ struct waitqueue *headerqueue, *tailqueue; int writeptr; int readptr; int writein, readout; int bufferPIPESIZE; int empty, full; void runpchar in3, runcchar in3, print; int main char in3; writeptr = 0; readptr = 0; writein = 0; empty = PIPESIZE; full = 0; produ
6、cer = struct PCB *mallocsizeofstruct PCB; consumer = struct PCB *mallocsizeofstruct PCB; headerqueue = struct waitqueue *mallocsizeofstruct waitqueue; headerqueue-next = NULL; tailqueue = headerqueue; producer-status = READY; consumer-status = WAIT; producer-time = consumer-time = 0; printfNow start
7、ing the program.n; printfPress p1 to run PRODUCER1,Press p1 to run PRODUCER2 n; printfPress c1 to run CONSUMER1,Press c2 to run CONSUMER2 n; printfPress e to exit from the program.n; while 1 strcpyin, N; while .strcmpin, N printfn; scanf%s, in; if strcmpin, e & strcmpin, p1 & strcmpin, p2 & strcmpin
8、, c1 & strcmpin, c2 printferror,please input again.n; strcpyin, N; if p = in0 runpin; producer-time+; else if c = in0 runcin; consumer-time+; else n, in; struct waitqueue *p = struct waitqueue *mallocsizeofstruct waitqueue; strcpyp-, in; p- = WAIT; p- = producer-time + 1; p-next = NULL; tailqueue-ne
9、xt = p; tailqueue = p; full+; else writein = writein + 1 % 100; producer-status = RUN; printfrun PRODUCER %s process . product %d , in, writein; bufferwriteptr = writein; if empty8 struct waitqueue *p; p = headerqueue-next; printfrun CONSUMER %s process. use %d, p-, bufferwriteptr; if tailqueue = p
10、tailqueue = headerqueue; headerqueue-next = p-next; freep; consumer-status = WAIT; empty-; else if writeptr readptr writeptr+; if writeptr = PIPESIZE writeptr = 0; if readptr = 0 producer-status = WAIT; else producer-status = READY; else writeptr+; if writeptr = readptr producer-status = WAIT; else
11、producer-status = READY; consumer-status = READY; empty-; full+; void runcchar in3 if empty = 8 struct waitqueue *search; search = headerqueue-next; while search .= NULL if .strcmpin, search- printferror.n; exit1; search = search-next; consumer-status = WAIT; printfCONSUMER %s is waiting, cant be sc
12、heduled.n,in; struct waitqueue *p = struct waitqueue *mallocsizeofstruct waitqueue; strcpyp-, in; p- = WAIT; p- = consumer-time + 1; p-next = NULL; tailqueue-next = p; tailqueue = p; empty+; else consumer-status = RUN; readout = bufferreadptr; printfrun CONSUMER %s process. use %d , in, readout; if
13、full8 writein = writein + 1 % 100; bufferwriteptr = writein; struct waitqueue *p; p = headerqueue-next; printfrun PRODUCER %s process. product %d , p-, bufferwriteptr; if tailqueue = p tailqueue = headerqueue; headerqueue-next = p-next; freep; producer-status = WAIT; full-; writeptr+; readptr+; else
14、 if readptr writeptr readptr+; if readptr = PIPESIZE readptr = 0; if writeptr = 0 consumer-status = WAIT; else consumer-status = READY; else consumer-status = READY; else readptr+; if readptr = writeptr consumer-status = WAIT; writeptr = readptr = 0; else consumer-status = READY; producer-status = R
15、EADY; full-; empty+; void print int i = 0; int j = 0; int low = 0; int high = 0; printfn; for i; i PIPESIZE; i+ printf-; printfn; if readptr writeptr for low = 0; low readptr; low+ printf| |; for low = readptr; low writeptr; low+ printf| %2d |, bufferlow; for low = writeptr; low writeptr for low = 0
16、; low writeptr; low+ printf| %2d |, bufferlow; for low = writeptr; low readptr; low+ printf| |; for low = readptr; low status = WAIT for low = 0; low PIPESIZE; low+ printf| %2d |, bufferlow; else for low = 0; low PIPESIZE; low+ printf| |; printfn; for i = 0; i status = WAIT & NULL .=headerqueue-next struct waitqueue *p = headerqueue-next; while NULL .= p printf PRODUCER %s proc
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 南京醫(yī)科大學(xué)《物聯(lián)網(wǎng)自動(dòng)識(shí)別技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 醫(yī)療用品購銷合同
- 哪位手上有裝修包清工合同
- 合同之伸縮縫施工合同
- 住宅套內(nèi)公共區(qū)域裝修工程合同
- 茶藝師(高級(jí))復(fù)習(xí)測(cè)試題
- 統(tǒng)編版語文三年級(jí)下冊(cè)第六單元習(xí)作身邊那些有特點(diǎn)的人 公開課一等獎(jiǎng)創(chuàng)新教學(xué)設(shè)計(jì)
- 糧食玉米購銷合同范本
- 樹木供銷合同范本
- 主播月合同范本
- 9.1.1 西亞 第1課時(shí) 課件 七年級(jí)地理下冊(cè) 人教版
- 校外培訓(xùn)機(jī)構(gòu)預(yù)收費(fèi)資金托管協(xié)議書范本
- DB37-T4827-2025 水利工程運(yùn)行管理標(biāo)牌設(shè)置指南
- 2025山東能源集團(tuán)中級(jí)人才庫選拔高頻重點(diǎn)模擬試卷提升(共500題附帶答案詳解)
- 《餐廳托盤的使用技》課件
- 【化學(xué)】化學(xué)與可持續(xù)發(fā)展教學(xué)設(shè)計(jì)-2024-2025學(xué)年九年級(jí)化學(xué)人教版下冊(cè)
- Unit 2 Know your body(說課稿) -2024-2025學(xué)年外研版(三起)(2024)英語三年級(jí)下冊(cè)
- 斷絕父母關(guān)系協(xié)議書
- 個(gè)人征信逾期修復(fù)計(jì)劃說明
- 6.3.1+平面向量基本定理(教學(xué)課件)-高一數(shù)學(xué)(人教A版2019)
- 2024-2030年中國便攜式超聲行業(yè)市場(chǎng)發(fā)展監(jiān)測(cè)及投資潛力預(yù)測(cè)報(bào)告
評(píng)論
0/150
提交評(píng)論