




已閱讀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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣西梧州市本年度(2025)小學(xué)一年級(jí)數(shù)學(xué)部編版隨堂測(cè)試(上學(xué)期)試卷及答案
- 廣西貴港市本年度(2025)小學(xué)一年級(jí)數(shù)學(xué)統(tǒng)編版期中考試(下學(xué)期)試卷及答案
- VR技術(shù)應(yīng)用模擬習(xí)題含答案
- 基礎(chǔ)營(yíng)養(yǎng)??荚囶}(含參考答案)
- 山西省部分學(xué)校2024-2025學(xué)年高二下學(xué)期期中測(cè)評(píng)考試歷史試題(原卷版+解析版)
- 水球場(chǎng)地水質(zhì)監(jiān)測(cè)與過濾考核試卷
- 電視設(shè)備智能生物藥品政策法規(guī)研究技術(shù)考核試卷
- 紡織設(shè)備客戶需求分析與產(chǎn)品設(shè)計(jì)考核試卷
- 生物質(zhì)燃?xì)獍l(fā)電技術(shù)在新能源領(lǐng)域的應(yīng)用考核試卷
- 稀土金屬提煉過程中的資源保障與可持續(xù)發(fā)展策略考核試卷
- 2024-2025學(xué)年杭州市余杭區(qū)七年級(jí)上英語(yǔ)期中試題(含答案和音頻)
- 揚(yáng)塵治理培訓(xùn)課件
- 5《以工匠精神雕琢?xí)r代品質(zhì)》說課稿 2024-2025學(xué)年統(tǒng)編版高中語(yǔ)文必修上冊(cè)
- 2024年新疆區(qū)公務(wù)員錄用考試《行測(cè)》真題及答案解析
- 《數(shù)字營(yíng)銷》全套教學(xué)課件
- 2024年考研政治復(fù)習(xí)要點(diǎn)解析
- 人美版八年級(jí)美術(shù)下冊(cè)《1. 繪畫的多元化》說課稿
- DB34T4829-2024公路工程泡沫輕質(zhì)土設(shè)計(jì)與施工技術(shù)規(guī)程
- 過敏性休克應(yīng)急預(yù)案-2
- 【新課標(biāo)核心素養(yǎng)目標(biāo)】6.2.1二氧化碳的性質(zhì)和用途教案(表格式)初中化學(xué)人教版(2024)九年級(jí)上冊(cè)
- 渣土、余土運(yùn)輸服務(wù)方案(技術(shù)方案)
評(píng)論
0/150
提交評(píng)論