版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上操作系統(tǒng)實驗報告班 級物聯(lián)網(wǎng)1302班學 號姓 名實驗3 進程同步和通信-生產(chǎn)者和消費者問題模擬1. 目的: 調(diào)試、修改、運行模擬程序,通過形象化的狀態(tài)顯示,使學生理解進程的概念,了解同步和通信的過程,掌握進程通信和同步的機制,特別是利用緩沖區(qū)進行同步和通信的過程。通過補充新功能,使學生能靈活運用相關(guān)知識,培養(yǎng)創(chuàng)新能力。2. 內(nèi)容及要求:1) 調(diào)試、運行模擬程序。2) 發(fā)現(xiàn)并修改程序中不完善的地方。3) 修改程序,使用隨機數(shù)控制創(chuàng)建生產(chǎn)者和消費者的過程。4) 在原來程序的基礎上,加入緩沖區(qū)的寫互斥控制功能,模擬多個進程存取一個公共緩沖區(qū),當有進程正在寫緩沖區(qū)時,其他要
2、訪問該緩沖區(qū)的進程必須等待,當有進程正在讀取緩沖區(qū)時,其他要求讀取的進程可以訪問,而要求寫的進程應該等待。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ū)是有限的,因此當其滿了時生產(chǎn)者進程應該等待,而空時,消費者進程應該等待;當生產(chǎn)者向緩沖區(qū)放入了一個數(shù)據(jù),應喚醒正在等待的消費者進程,同樣,當消費者取走一個數(shù)據(jù)后,應喚醒正在等待的生產(chǎn)者進程。就是生產(chǎn)者
3、和消費者之間的同步。每次寫入和讀出數(shù)據(jù)時,都將讀和寫指針加一。當讀寫指針同樣時,又一起退回起點。當寫指針指向最后時,生產(chǎn)者就等待。當讀指針為零時,再次要讀取的消費者也應該等待。 為簡單起見,每次產(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'鍵則運行一次消費者進程。通過屏幕可以觀察到兩個進程
4、的狀態(tài)和緩沖區(qū)變化的情況。6. 實驗流程圖源程序#include<>#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
5、wait point */struct PCB *consumer;/* read wait point */struct 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
6、 = 0;producer = (struct PCB *)malloc(sizeof(struct PCB);consumer = (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 =
7、consumer->time = 0;printf("Now starting the program!n");printf("Press 'p1' to run 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
8、 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(
9、in, "c2")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->
10、;, in);p-> = WAIT;p-> = producer->time + 1;p->next = NULL;tailqueue->next = p;tailqueue = p;full+;elsewritein = (writein + 1) % 100;producer->status = RUN;printf("run PRODUCER %s process . product %d ", in, writein);bufferwriteptr = writein;if (empty>8)struct waitqueue
11、 *p;p = headerqueue->next;printf("run CONSUMER %s process. use %d", p->, bufferwriteptr);if (tailqueue = p)tailqueue = headerqueue;headerqueue->next = p->next;free(p);consumer->status = WAIT;empty-;elseif (writeptr > readptr)writeptr+;if (writeptr >= PIPESIZE)writeptr
12、= 0;if (readptr = 0)producer->status = WAIT;else producer->status = READY;elsewriteptr+;if (writeptr = readptr)producer->status = WAIT;else producer->status = READY;consumer->status = READY;empty-;full+;void runc(char in3)if (empty >= 8)struct waitqueue *search;search = headerqueue
13、->next;while (search != NULL)if (!strcmp(in, search->)printf("error!n");exit(1);search = search->next;consumer->status = WAIT;printf("CONSUMER %s is waiting, can't be scheduled.n",in);struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue);strcpy
14、(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 (full>8)writein = (writein + 1) % 100;bufferwriteptr =
15、 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
16、)readptr+;if (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 = R
17、EADY;full-;empty+;void print()int i = 0;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+)pr
18、intf("| %2d|", bufferlow);for (low = writeptr; low <PIPESIZE; low+)printf("|");else if (readptr > writeptr)for (low = 0; low < writeptr; low+)printf("| %2d|", bufferlow);for (low = writeptr; low < readptr; low+)printf("|");for (low = readptr; low < PIPESIZE; low+)printf("| %2d|", bufferlow);else if (producer->status = WAIT)for (low = 0; low < PIPESIZE; low+)printf("| %2d |", bufferlow);elsefor (low = 0; low < PIPESIZE; l
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 福建省閩西南水資源開發(fā)有限責任公司2025年招聘備考題庫及1套參考答案詳解
- 建筑設計師招聘與面試題設計
- 文化傳媒公司財務管理崗位試題解析
- 軟件測試高工職位面經(jīng)
- 財務經(jīng)理職稱考試題集含答案
- 市場營銷總監(jiān)面試題及高分技巧含答案
- 攝影師面試常見問題解答手冊
- 京東物流項目協(xié)調(diào)助理面試題目及答案
- 2025云南昆明市尋甸縣衛(wèi)生健康系統(tǒng)第二批招聘編外人員40人筆試考試參考試題及答案解析
- 2026湖南長沙市雨花區(qū)楓樹山明宸小學春季合同制教師招聘考試筆試備考試題及答案解析
- 2025四川資陽現(xiàn)代農(nóng)業(yè)發(fā)展集團有限公司招聘1人筆試歷年參考題庫附帶答案詳解
- 2025河北廊坊燕京職業(yè)技術(shù)學院選聘專任教師20名(公共基礎知識)測試題附答案解析
- 0901 溶液顏色檢查法:2020年版 VS 2025年版對比表
- 各部門環(huán)境因素識別評價表-塑膠公司
- 2025遼寧丹東市融媒體中心下半年面向普通高校招聘急需緊缺人才5人筆試考試參考試題及答案解析
- 2025年10月自考04184線性代數(shù)經(jīng)管類試題及答案含評分參考
- 國開2025年秋《心理學》形成性考核練習1-6答案
- 科技研發(fā)項目管理辦法
- 個體診所藥品清單模板
- 267條表情猜成語【動畫版】
- 檢驗科授權(quán)書
評論
0/150
提交評論