操作系統(tǒng)-生產(chǎn)者與消費(fèi)者源代碼.doc_第1頁(yè)
操作系統(tǒng)-生產(chǎn)者與消費(fèi)者源代碼.doc_第2頁(yè)
操作系統(tǒng)-生產(chǎn)者與消費(fèi)者源代碼.doc_第3頁(yè)
操作系統(tǒng)-生產(chǎn)者與消費(fèi)者源代碼.doc_第4頁(yè)
操作系統(tǒng)-生產(chǎn)者與消費(fèi)者源代碼.doc_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

/*生產(chǎn)者/消費(fèi)者*/#include#include#includestruct PCB int flag;/1為生產(chǎn)者,2為消費(fèi)者int numLabel;typedef struct QNodePCB data;/數(shù)據(jù)域struct QNode* next;/指針域QNode, *QueuePtr;typedef structQueuePtr front;/隊(duì)頭指針QueuePtr rear;/隊(duì)尾指針LinkQueue;typedef struct LNodeQueuePtr data;struct LNode *next;LNode, *LinkList;void QueueInit(LinkQueue &Q)/初始化隊(duì)列Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode);Q.front-next = NULL;void LinkListInit(LinkList& L)/初始化鏈表L = (LinkList)malloc(sizeof(LNode);void EnQueue(LinkQueue &Q, QueuePtr p)/入隊(duì)p-next = NULL;Q.rear-next = p;Q.rear = p;QueuePtr DeQueue(LinkQueue &Q)/出隊(duì)QueuePtr p = Q.front-next;Q.front-next = p-next;if(Q.rear = p)Q.rear =Q.front;return p;void outqueue(LinkQueue& Q) QueuePtr p = Q.front-next;Q.front-next = p-next;if(Q.rear = p)Q.rear =Q.front;void LinkListInsert(LinkList &L, QueuePtr e)/進(jìn)入鏈表LinkList p = L;LinkList q = (LinkList)malloc(sizeof(LNode);while(p-next)p+;q-data = e;q-next = NULL;p-next = q;void processproc(LinkQueue &Q)/創(chuàng)建進(jìn)程進(jìn)入初始隊(duì)列int processNum = 0;coutprocessNum;for(int i = 0; i processNum; i+)cout輸入第i+1pcb.flag;/輸入進(jìn)程的種類pcb.numLabel = i+1;/進(jìn)程序號(hào)賦值QueuePtr p = (QueuePtr)malloc(sizeof(QNode);p-data = pcb;EnQueue(Q, p);bool HasElement(LinkQueue Q)/判斷隊(duì)列是否為空if(Q.front = Q.rear)return 0;elsereturn 1;int ProduceRun(int &full, int BufferSize)/運(yùn)行生產(chǎn)者進(jìn)程if(full 0)full-;return 1;return 0;void DisPlay(LinkQueue Q)/打印隊(duì)列QueuePtr p = Q.front;while(p-next)cout進(jìn)程next-data.numLabelnext;void main()int BufferSize;/設(shè)置緩沖區(qū)大小coutBufferSize; int full = 0;/當(dāng)前緩沖區(qū)中的進(jìn)程數(shù)目int temp = 1;LinkList over;/用于收集已經(jīng)運(yùn)行結(jié)束的進(jìn)程LinkListInit(over);LinkQueue ReadyQueue;/就緒隊(duì)列LinkQueue ProducerWaitQueue;/生產(chǎn)者等待隊(duì)列LinkQueue ConsumerWaitQueue;/消費(fèi)者等待隊(duì)列/初始化QueueInit(ReadyQueue);QueueInit(ProducerWaitQueue);QueueInit(ConsumerWaitQueue);while(temp)/死循環(huán)processproc(ReadyQueue);/創(chuàng)建進(jìn)程進(jìn)入就緒隊(duì)列bool element=HasElement(ReadyQueue);/判斷隊(duì)列是否為空while(element)/當(dāng)它不是空的cout進(jìn)程next-data.numLabelnext-data.flag = 1)/如果它是一個(gè)生產(chǎn)者cout生產(chǎn)者endl;if(ProduceRun(full, BufferSize) = 1)/判斷緩存區(qū)是否還有空間cout進(jìn)程next-data.numLabel執(zhí)行完畢endl;LinkListInsert(over, DeQueue(ReadyQueue);/運(yùn)行結(jié)束,進(jìn)入over鏈表/*/if(HasElement(ConsumerWaitQueue) ConsumeRun(full, BufferSize);outqueue(ConsumerWaitQueue); /*/if(HasElement(ProducerWaitQueue)/檢查生產(chǎn)者等待隊(duì)列,激活隊(duì)列中的進(jìn)程進(jìn)入就緒隊(duì)列EnQueue(ReadyQueue, DeQueue(ProducerWaitQueue);else if(ProduceRun(full, BufferSize) = 0)/就緒隊(duì)列出一個(gè)元素,進(jìn)入生產(chǎn)者等待隊(duì)列EnQueue(ProducerWaitQueue, DeQueue(ReadyQueue);else/*/如果它是一個(gè)消費(fèi)者cout消費(fèi)者endl;if(ConsumeRun(full, BufferSize) = 1)/判斷緩存區(qū)是否存在進(jìn)程LinkListInsert(over, DeQueue(ReadyQueue);/運(yùn)行結(jié)束,進(jìn)入over鏈表if(HasElement(ConsumerWaitQueue)/檢查消費(fèi)者等待隊(duì)列,激活隊(duì)列中的進(jìn)程進(jìn)入就緒隊(duì)列EnQueue(ReadyQueue, DeQueue(ConsumerWaitQueue); /*/if(HasElement(ProducerWaitQueue) ProduceRun(full, BufferSize);outqueue(ProducerWaitQueue); /*/else if(ConsumeRun(full, BufferSize) = 0)/若沒有進(jìn)程,則進(jìn)入消費(fèi)者等待隊(duì)列EnQueue(ConsumerWaitQueue, DeQueue(ReadyQueue);/*/*輸出就緒隊(duì)列*/element = HasElement(ReadyQueue);if(HasElement(ReadyQueue)cout就緒隊(duì)列中有下列進(jìn)程:endl;DisPlay(ReadyQueue);else cout就緒隊(duì)列里面沒有進(jìn)程endl;/*/*輸出生產(chǎn)者等待隊(duì)列*/if(HasElement(ProducerWaitQueue)cout生產(chǎn)者等待隊(duì)列中有下列進(jìn)程:endl;DisPlay(ProducerWaitQueue);else cout

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論