




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、操作系統(tǒng)實驗報告班 級物聯(lián)網(wǎng)1302班學(xué) 號姓 名實驗3 進程同步和通信-生產(chǎn)者和消費者問題模擬1. 目的: 調(diào)試、修改、運行模擬程序,通過形象化的狀態(tài)顯示,使學(xué)生理解進程的概念,了解同步和通信的過程,掌握進程通信和同步的機制,特別是利用緩沖區(qū)進行同步和通信的過程。通過補充新功能,使學(xué)生能靈活運用相關(guān)知識,培養(yǎng)創(chuàng)新能力。2. 內(nèi)容及要求:1) 調(diào)試、運行模擬程序。2) 發(fā)現(xiàn)并修改程序中不完善的地方。3) 修改程序,使用隨機數(shù)控制創(chuàng)建生產(chǎn)者和消費者的過程。4) 在原來程序的基礎(chǔ)上,加入緩沖區(qū)的寫互斥控制功能,模擬多個進程存取一個公共緩沖區(qū),當(dāng)有進程正在寫緩沖區(qū)時,其他要訪問該緩沖區(qū)的進程必須等待
2、,當(dāng)有進程正在讀取緩沖區(qū)時,其他要求讀取的進程可以訪問,而要求寫的進程應(yīng)該等待。5) 完成1)、2)、3)功能的,得基本分,完成4)功能的加2分,有其它功能改進的再加2分3. 程序說明:本程序是模擬兩個進程,生產(chǎn)者(producer)和消費者(Consumer)工作。生產(chǎn)者每次產(chǎn)生一個數(shù)據(jù),送入緩沖區(qū)中。消費者每次從緩沖區(qū)中取走一個數(shù)據(jù)。緩沖區(qū)可以容納8個數(shù)據(jù)。因為緩沖區(qū)是有限的,因此當(dāng)其滿了時生產(chǎn)者進程應(yīng)該等待,而空時,消費者進程應(yīng)該等待;當(dāng)生產(chǎn)者向緩沖區(qū)放入了一個數(shù)據(jù),應(yīng)喚醒正在等待的消費者進程,同樣,當(dāng)消費者取走一個數(shù)據(jù)后,應(yīng)喚醒正在等待的生產(chǎn)者進程。就是生產(chǎn)者和消費者之間的同步。每次寫
3、入和讀出數(shù)據(jù)時,都將讀和寫指針加一。當(dāng)讀寫指針同樣時,又一起退回起點。當(dāng)寫指針指向最后時,生產(chǎn)者就等待。當(dāng)讀指針為零時,再次要讀取的消費者也應(yīng)該等待。 為簡單起見,每次產(chǎn)生的數(shù)據(jù)為0-99的整數(shù),從0開始,順序遞增。兩個進程的調(diào)度是通過運行者使用鍵盤來實現(xiàn)的。4. 程序使用的數(shù)據(jù)結(jié)構(gòu)進程控制塊:包括進程名,進程狀態(tài)和執(zhí)行次數(shù)。緩沖區(qū):一個整數(shù)數(shù)組。緩沖區(qū)說明塊:包括類型,讀指針,寫指針,讀等待指針和寫等待指針。5. 程序使用說明啟動程序后,如果使用p鍵則運行一次生產(chǎn)者進程,使用c鍵則運行一次消費者進程。通過屏幕可以觀察到兩個進程的狀態(tài)和緩沖區(qū)變化的情況。6. 實驗流程圖源程序#include#
4、include#include#define PIPESIZE 8enum Status RUN, WAIT, READY ;#define NORMAL 0#define SLEEP 1#define AWAKE 2struct PCBchar name3;enum Status status;int time;struct waitqueuestruct PCB pcb;struct waitqueue *next;struct PCB *producer; /* write wait point */struct PCB *consumer;/* read wait point */st
5、ruct waitqueue *headerqueue, *tailqueue;int writeptr;int readptr;int writein, readout;int bufferPIPESIZE;int empty, full;void runp(char in3), runc(char in3), print();int main()char in3;writeptr = 0;readptr = 0;writein = 0;empty = PIPESIZE;full = 0;producer = (struct PCB *)malloc(sizeof(struct PCB);c
6、onsumer = (struct PCB *)malloc(sizeof(struct PCB);headerqueue = (struct waitqueue *)malloc(sizeof(struct waitqueue);headerqueue-next = NULL;tailqueue = headerqueue;producer-status = READY;consumer-status = WAIT;producer-time = consumer-time = 0;printf(Now starting the program!n);printf(Press p1 to r
7、un PRODUCER1,Press p1 to run PRODUCER2 n);printf(Press c1 to run CONSUMER1,Press c2 to run CONSUMER2 n);printf(Press e to exit from the program.n);while (1)strcpy(in, N);while (!strcmp(in, N)printf(n);scanf(%s, in);if (strcmp(in, e) & strcmp(in, p1) & strcmp(in, p2) & strcmp(in, c1) & strcmp(in, c2)
8、printf(error,please input again!n);strcpy(in, N);if (p = in0)runp(in);producer-time+;else if (c = in0)runc(in);consumer-time+;elsen, in);struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue);strcpy(p-, in);p- = WAIT;p- = producer-time + 1;p-next = NULL;tailqueue-next = p;tailqueu
9、e = p;full+;elsewritein = (writein + 1) % 100;producer-status = RUN;printf(run PRODUCER %s process . product %d , in, writein);bufferwriteptr = writein;if (empty8)struct waitqueue *p;p = headerqueue-next;printf(run CONSUMER %s process. use %d, p-, bufferwriteptr);if (tailqueue = p)tailqueue = header
10、queue;headerqueue-next = p-next;free(p);consumer-status = WAIT;empty-;elseif (writeptr readptr)writeptr+;if (writeptr = PIPESIZE)writeptr = 0;if (readptr = 0)producer-status = WAIT;else producer-status = READY;elsewriteptr+;if (writeptr = readptr)producer-status = WAIT;else producer-status = READY;c
11、onsumer-status = READY;empty-;full+;void runc(char in3)if (empty = 8)struct waitqueue *search;search = headerqueue-next;while (search != NULL)if (!strcmp(in, search-)printf(error!n);exit(1);search = search-next;consumer-status = WAIT;printf(CONSUMER %s is waiting, cant be scheduled.n,in);struct wait
12、queue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue);strcpy(p-, in);p- = WAIT;p- = consumer-time + 1;p-next = NULL;tailqueue-next = p;tailqueue = p;empty+;elseconsumer-status = RUN;readout = bufferreadptr;printf(run CONSUMER %s process. use %d , in, readout);if (full8)writein = (writein +
13、1) % 100;bufferwriteptr = writein;struct waitqueue *p;p = headerqueue-next;printf(run PRODUCER %s process. product %d , p-, bufferwriteptr);if (tailqueue = p)tailqueue = headerqueue;headerqueue-next = p-next;free(p);producer-status = WAIT;full-;writeptr+;readptr+;elseif (readptr writeptr)readptr+;if
14、 (readptr = PIPESIZE)readptr = 0;if (writeptr = 0)consumer-status = WAIT;elseconsumer-status = READY;else consumer-status = READY;elsereadptr+;if (readptr = writeptr)consumer-status = WAIT;writeptr = readptr = 0;else consumer-status = READY;producer-status = READY;full-;empty+;void print()int i = 0;
15、int j = 0;int low = 0;int high = 0;printf(n);for (i; i PIPESIZE; i+)printf(-);printf(n);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; low writeptr; low+)printf(| %2d|, bu
16、fferlow);for (low = writeptr; low readptr; low+)printf(|);for (low = readptr; low status = WAIT)for (low = 0; low PIPESIZE; low+)printf(| %2d |, bufferlow);elsefor (low = 0; low PIPESIZE; low+)printf(|);printf(n);for (i = 0; i status = WAIT & NULL !=headerqueue-next)struct waitqueue *p = headerqueue-next;while (NULL != p)printf( PRODUCER %s pro
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 保育老師健康知識培訓(xùn)
- 項目工程應(yīng)急演練課件
- 《平面設(shè)計》課件-第6章 設(shè)計符號學(xué)基礎(chǔ)
- 音樂信息技術(shù)課件
- 市政污水管網(wǎng)改造項目建設(shè)管理方案(模板范文)
- 城鎮(zhèn)污水管網(wǎng)建設(shè)工程運營管理方案(模板范文)
- xx片區(qū)城鄉(xiāng)供水一體化項目規(guī)劃設(shè)計方案(范文參考)
- 2025年氯鉑酸合作協(xié)議書
- 基于風(fēng)險指標的低壓設(shè)備退役優(yōu)化及其在新加坡電網(wǎng)中的應(yīng)用
- 2025年專用小麥新品種項目合作計劃書
- 手工編織團建活動方案
- GB 17051-2025二次供水設(shè)施衛(wèi)生規(guī)范
- 四川省眉山市東坡區(qū) 2024-2025學(xué)年七年級下學(xué)期期末道德與法治試卷(含答案)
- 2025至2030中國生物質(zhì)鍋爐行業(yè)當(dāng)前競爭現(xiàn)狀及未來應(yīng)用規(guī)模預(yù)測報告
- 少兒健康運動課件
- 應(yīng)急救援無人機系統(tǒng)應(yīng)用解析
- 2025年廣西公需科目答案01
- JJF1033-2023計量標準考核規(guī)范
- 三級安全教育登記卡(新)
- 少兒美術(shù)國畫- 少兒希望 《紫藤課件》
- 建立良好的同伴關(guān)系-課件-高二心理健康
評論
0/150
提交評論