![操作系統(tǒng)生產(chǎn)者與消費者源代碼_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/8/cc00179e-453c-4a69-ac18-065e3a4624e6/cc00179e-453c-4a69-ac18-065e3a4624e61.gif)
![操作系統(tǒng)生產(chǎn)者與消費者源代碼_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/8/cc00179e-453c-4a69-ac18-065e3a4624e6/cc00179e-453c-4a69-ac18-065e3a4624e62.gif)
![操作系統(tǒng)生產(chǎn)者與消費者源代碼_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/8/cc00179e-453c-4a69-ac18-065e3a4624e6/cc00179e-453c-4a69-ac18-065e3a4624e63.gif)
![操作系統(tǒng)生產(chǎn)者與消費者源代碼_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/8/cc00179e-453c-4a69-ac18-065e3a4624e6/cc00179e-453c-4a69-ac18-065e3a4624e64.gif)
下載本文檔
版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 法院調(diào)取證據(jù)申請書
- 生態(tài)文明下的城市綠化與生物多樣性保護
- 電商平臺的數(shù)據(jù)安全管理體系建設(shè)
- 2024-2025學(xué)年八年級科學(xué)下冊第一章電與磁第三節(jié)電磁鐵的應(yīng)用作業(yè)設(shè)計新版浙教版
- 2024年高考物理一輪復(fù)習(xí)專題4.2平拋運動精練含解析
- 2024-2025學(xué)年新教材高中地理課后作業(yè)提升八水循環(huán)含解析新人教版必修第一冊
- 2024-2025學(xué)年新教材高中歷史第二單元三國兩晉南北朝的民族交融與隋唐統(tǒng)一多民族封建國家的發(fā)展第5課三國兩晉南北朝的政權(quán)更迭與民族交融課后課時作業(yè)新人教版必修中外歷史綱要上
- 文明禮儀申請書
- 隨母姓申請書
- 2025年度養(yǎng)豬場養(yǎng)殖環(huán)境優(yōu)化與設(shè)施改造合同
- 2025年大慶職業(yè)學(xué)院高職單招語文2018-2024歷年參考題庫頻考點含答案解析
- 山東省濟南市2024-2024學(xué)年高三上學(xué)期1月期末考試 地理 含答案
- 【課件】液體的壓強(課件)-2024-2025學(xué)年人教版物理八年級下冊
- 實施彈性退休制度暫行辦法解讀課件
- 冷凍食品配送售后服務(wù)體系方案
- 2024-2030年中國自動光學(xué)檢測儀(AOI)市場競爭格局與前景發(fā)展策略分析報告
- 2024-2025學(xué)年人教版數(shù)學(xué)八年級上冊期末模擬試卷
- 銷售培訓(xùn)合同范例
- 財務(wù)工作總結(jié)與計劃-財務(wù)經(jīng)理總結(jié)與計劃
- 四川省成都市2023年中考道德與法治真題試卷(含答案)
- 【初中生物】病毒課件2024-2025學(xué)年人教版生物七年級上冊
評論
0/150
提交評論