東北大學(xué) 操作系統(tǒng)實(shí)驗(yàn)三報(bào)告.doc_第1頁
東北大學(xué) 操作系統(tǒng)實(shí)驗(yàn)三報(bào)告.doc_第2頁
東北大學(xué) 操作系統(tǒng)實(shí)驗(yàn)三報(bào)告.doc_第3頁
東北大學(xué) 操作系統(tǒng)實(shí)驗(yàn)三報(bào)告.doc_第4頁
東北大學(xué) 操作系統(tǒng)實(shí)驗(yàn)三報(bào)告.doc_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

操作系統(tǒng)實(shí)驗(yàn)報(bào)告 班 級(jí)物聯(lián)網(wǎng)1302班學(xué) 號(hào)姓 名 實(shí)驗(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)知識(shí),培養(yǎng)創(chuàng)新能力。2. 內(nèi)容及要求:1) 調(diào)試、運(yùn)行模擬程序。2) 發(fā)現(xiàn)并修改程序中不完善的地方。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)程必須等待,當(dāng)有進(jìn)程正在讀取緩沖區(qū)時(shí),其他要求讀取的進(jìn)程可以訪問,而要求寫的進(jìn)程應(yīng)該等待。5) 完成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ù)。因?yàn)榫彌_區(qū)是有限的,因此當(dāng)其滿了時(shí)生產(chǎn)者進(jìn)程應(yīng)該等待,而空時(shí),消費(fèi)者進(jìn)程應(yīng)該等待;當(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)者和消費(fèi)者之間的同步。每次寫入和讀出數(shù)據(jù)時(shí),都將讀和寫指針加一。當(dāng)讀寫指針同樣時(shí),又一起退回起點(diǎn)。當(dāng)寫指針指向最后時(shí),生產(chǎn)者就等待。當(dāng)讀指針為零時(shí),再次要讀取的消費(fèi)者也應(yīng)該等待。 為簡(jiǎn)單起見,每次產(chǎn)生的數(shù)據(jù)為0-99的整數(shù),從0開始,順序遞增。兩個(gè)進(jìn)程的調(diào)度是通過運(yùn)行者使用鍵盤來實(shí)現(xiàn)的。4. 程序使用的數(shù)據(jù)結(jié)構(gòu)進(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. 實(shí)驗(yàn)流程圖源程序#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 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 = 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 = 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 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)printf(error,please input again!n);strcpy(in, N);if (p = in0)runp(in);producer-time+;else if (c = in0)runc(in);consumer-time+;else/printf(PRODUCER product %d timesn, producer-time);/printf(CONSUMER consumer %d timesn, consumer-time);exit(0);print();printf(n);void runp(char in3)if (full = 8)struct waitqueue *search;search = headerqueue-next;while (search != NULL)if (!strcmp(in, )printf(error!n);exit(1);search = search-next;producer-status = WAIT;printf(PRODUCER %s process is waiting, cant be scheduled.n, in);struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue);strcpy(, in);p-pcb.status = WAIT;p-pcb.time = 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 (empty8)struct waitqueue *p;p = headerqueue-next;printf(run CONSUMER %s process. use %d, , bufferwriteptr);if (tailqueue = p)tailqueue = headerqueue;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;consumer-status = READY;empty-;full+;void runc(char in3)if (empty = 8)struct waitqueue *search;search = headerqueue-next;while (search != NULL)if (!strcmp(in, )printf(error!n);exit(1);search = search-next;consumer-status = WAIT;printf(CONSUMER %s is waiting, cant be scheduled.n,in);struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue);strcpy(, in);p-pcb.status = WAIT;p-pcb.time = 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 + 1) % 100;bufferwriteptr = writein;struct waitqueue *p;p = headerqueue-next;printf(run PRODUCER %s process. product %d , , bufferwriteptr);if (tailqueue = p)tailqueue = headerqueue;headerqueue-next = p-next;free(p);producer-status = WAIT;full-;writeptr+;readptr+;elseif (readptr writeptr)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 = READY;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+)printf(| %2d|, bufferlow);for (low = writeptr; low writeptr)for (low = 0; 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);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

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論