操作系統(tǒng)生產(chǎn)者與消費者源代碼_第1頁
操作系統(tǒng)生產(chǎn)者與消費者源代碼_第2頁
操作系統(tǒng)生產(chǎn)者與消費者源代碼_第3頁
操作系統(tǒng)生產(chǎn)者與消費者源代碼_第4頁
免費預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

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

文檔簡介

1、/*生產(chǎn)者/消費者*/ #include #include #include struct PCB int flag;/1為生產(chǎn)者,2為消費者 int numLabel; typedef struct QNode( PCB data;/ 數(shù)據(jù)域 struct QNode* next;/ 指針域 JQNode, *QueuePtr; typedef struct next = NULL; void LinkListlnit(LinkList void EnQueue(LinkQueue Q.rear- n ext = p; Q.rear = p; QueuePtr DeQueue(LinkQue

2、ue Q.front-next = pnext; if(Q.rear = p)Q.rear =Q.front; return p; void outqueue(LinkQueue Q.front-next = pnext; if(Q.rear = p)Q.rear =Q.front; ) void LinkListlnsert(LinkList LinkList q = (LinkList)malloc(sizeof(LNode); while(p-next)p+; q-data = e; qnext = NULL; pn ext = q; void processproc(LinkQueue

3、 coutvv“請輸入進程的個數(shù):”;/確定進程個數(shù),默認為0 cin processNum; for(int i = 0; i data = pcb; En Queue(Q, p); bool HasElement(LinkQueue Q)/ 判斷隊列是否為空 ( if(Q.front = Q.rear) return 0; else return 1; int ProduceRun(int return 1; return 0; int ConsumeRun(int return 1; return 0; void DisPlay(LinkQueue Q)/ 打印隊列 ( QueuePtr

4、 p = Q.front; while(p-next) ( coutH 進程fp-next-data.numLabelvvendl; p = pnext; void main() ( int BufferSize;/設(shè)置緩沖區(qū)大小 cout請設(shè)置緩沖區(qū)的大小: cinBufferSize; int full = 0; /當(dāng)前緩沖區(qū)中的進程數(shù)目 int temp = 1 ; LinkList over;/用于收集已經(jīng)運行結(jié)束的進程 Lin kListl nit(over); LinkQueue ReadyQueue;/ 就緒隊列 LinkQueue ProducerWaitQueue;/ 生產(chǎn)者

5、等待隊列 LinkQueue ConsumerWaitQueue;/ 消費者等待隊列 初始化 Queuelnit(ReadyQueue); Queuelnit(ProducerWaitQueue); Queuelnit(C on sumerWaitQueue); while(temp)/ 死循環(huán) ( processproc(ReadyQueue);/創(chuàng)建進程進入就緒隊列 bool element=HasElement(ReadyQueue);/ 判斷隊列是否為空 while(element)/當(dāng)它不是空的 ( cout進程,ReadyQueue.front-next-data.numLabel

6、,申請運行,他是一個 if(ReadyQueue.front-next-data.flag = 1 ) 如果它是一個生產(chǎn)者 coutvv” 生產(chǎn)者 nendl; if(ProduceRun(full, BufferSize) = 1) 判斷緩存區(qū)是否還有空間 cout進程 ”vvReadyQueue.frontnextdata.numLabelvv” 執(zhí)行完畢 endl; LinkListlnsert(over5 DeQueue(ReadyQueue);/jK行結(jié)束、進入 over 鏈 if(HasElement(ConsumerWaitQueue) 1 表 1 ConsumeRun(full

7、, BufferSize); outqueue(ConsumerWaitQueue); * y* /*/ if(HasElement(ProducerWaitQueue)檢杳牛產(chǎn)者等待隊列,激活從列 中的進程進入就緒隊列 EnQueue(ReadyQueue, DeQueue(ProducerWaitQueue); else if(ProduceRun(full, BufferSize) = 0)/ 就緒隊列出一個元素、進入生產(chǎn) 者等待隊列 EnQueue(ProducerWaitQueue, DeQueue(ReadyQueue); ) e|se /*j 如果它是一個消費者cout消費者ne

8、ndl; if(ConsumeRun(full, BufferSize) = 1) 判斷緩存區(qū)是否存在進程( LinkListlnsert(over, DeQueue(ReadyQueue);/ 運行結(jié)束,進入 over 鏈 if(HasElement(ConsumerWaitQueue) /檢查消費者等待隊列,激活隊 列中 的進程進入就緒隊列 EnQueue(ReadyQueue, DeQueue(ConsumerWaitQueue); * y*I if(HasElement(ProducerWaitQueue) ProduceRun(full, BufferSize); outqueue(ProducerWaitQueue); ) else if(ConsumeRun(full, BufferSize) = 0)/若沒有進程,則進入消費者等 待隊列 EnQueue(ConsumerWaitQueue, DeQueue(ReadyQueue); 廣輸出就緒隊列V element = HasEleme nt(ReadyQueue); if(HasEleme nt(R

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論