![操作系統(tǒng)生產(chǎn)者消費者問題C語言(共7頁)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b499/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b4991.gif)
![操作系統(tǒng)生產(chǎn)者消費者問題C語言(共7頁)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b499/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b4992.gif)
![操作系統(tǒng)生產(chǎn)者消費者問題C語言(共7頁)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b499/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b4993.gif)
![操作系統(tǒng)生產(chǎn)者消費者問題C語言(共7頁)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b499/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b4994.gif)
![操作系統(tǒng)生產(chǎn)者消費者問題C語言(共7頁)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b499/b61d0bdc-8dfa-4d9e-a7f2-3a37f6e0b4995.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上操作系統(tǒng)概念第七版 中的實驗項目:生產(chǎn)者消費者問題。本程序中,main()函數(shù)需要三個參數(shù):主線程休眠時間;生產(chǎn)者線程數(shù);消費者線程數(shù)。各線程的休眠等待時間是隨機的。程序代碼:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#define BUFFER_SIZE 5typedef int buffer_item;struct vint i;buffer_item bufferBUFFER_SIZE+1;buffer_item fr
2、ont=0,rear=0;HANDLE mutex,empty,full;int insert_item(buffer_item item)/*insert item into bufferreturn 0 if successful,otherwise return -1 indicating an error condition*/if(rear+1)%(BUFFER_SIZE+1)=front)return 1;bufferrear=item;rear=(rear+1)%(BUFFER_SIZE+1);return 0;int remove_item(buffer_item *item)
3、/*remove an object from buffer placing it in item return 0 if successful,otherwise reutrn -1 indication an error condition */if(front = rear)return 1;*item=bufferfront;front=(front+1) % (BUFFER_SIZE+1);return 0;DWORD WINAPI producer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsign
4、ed)time(0);while (1) Sleep(rand()%101*10);WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1 =rand();printf("producer has producerd %d By %dn",rand1,data.i);if(insert_item(rand1)printf("insert data error!n");ReleaseMutex(mutex);ReleaseSemaphore(full,1,N
5、ULL);DWORD WINAPI consumer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);while (1)Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1)printf("remove data error! n");elseprintf("consumer c
6、onsumed %d By %d n",rand1,data.i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);int main(int argc,char *argv)/*Get command line arguments argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;int *ThreadI
7、dP,*ThreadIdS,i;struct v *countp,*counts;HANDLE *ThreadHandleP,*ThreadHandleS;sleeptime=atoi(argv1);pnum=atoi(argv2);snum=atoi(argv3);/*srand(time(NULL);sleeptime=9000;pnum=3;snum=3;*/ThreadHandleP=(HANDLE * )malloc(pnum * sizeof(HANDLE);ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE);ThreadId
8、P=(int * )malloc(pnum * sizeof(int);ThreadIdS=(int * )malloc(pnum * sizeof(int);mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemaphore(NULL,BUFFER_SIZE,BUFFER_SIZE,NULL);full=CreateSemaphore(NULL,0,BUFFER_SIZE+1,NULL);/*Create producer thread(s)*/countp=(struct v *)malloc(pnum+1)*sizeof(struct v);
9、counts=(struct v *)malloc(snum+1)*sizeof(struct v);for(i=0;i<pnum;i+)countpi+1.i=i+1;ThreadHandlePi=CreateThread(NULL,0,producer,&countpi+1,0,&ThreadIdPi);/*Create consumer thread(s)*/for(i=0;i<snum;i+)countsi+1.i=i+1;ThreadHandleSi=CreateThread(NULL,0,consumer,&countsi+1,0,&Th
10、readIdSi);/*Sleep*/Sleep(sleeptime);/*Exit*/return 0;#include "stdafx.h"#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#define BUFFER_SIZE 5typedef int buffer_item;struct vint i;buffer_item bufferBUFFER_SIZE+1;buffer_item front=0,rear=0;HANDLE
11、 mutex,empty,full;int insert_item(buffer_item item)/*insert item into bufferreturn 0 if successful,otherwise return -1 indicating an error condition*/if(rear+1)%(BUFFER_SIZE+1)=front)return 1;bufferrear=item;rear=(rear+1)%(BUFFER_SIZE+1);return 0;int remove_item(buffer_item *item)/*remove an object
12、from buffer placing it in item return 0 if successful,otherwise reutrn -1 indication an error condition */if(front = rear)return 1;*item=bufferfront;front=(front+1) % (BUFFER_SIZE+1);return 0;DWORD WINAPI producer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);while (1
13、) Sleep(rand()%101*10);WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1 =rand();printf("producer has producerd %d By %dn",rand1,data.i);if(insert_item(rand1)printf("insert data error!n");ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);DWORD WINAPI c
14、onsumer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);while (1)Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1)printf("remove data error! n");elseprintf("consumer consumed %d By %d n&
15、quot;,rand1,data.i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);int main(int argc,char *argv)/*Get command line arguments argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;DWORD *ThreadIdP,*ThreadIdS,i;s
16、truct v *countp,*counts;HANDLE *ThreadHandleP,*ThreadHandleS;/*sleeptime=atoi(argv1);pnum=atoi(argv2);snum=atoi(argv3);*/srand(time(NULL);sleeptime=9000;pnum=3;snum=3;ThreadHandleP=(HANDLE * )malloc(pnum * sizeof(HANDLE);ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE);ThreadIdP=(DWORD * )mallo
17、c(pnum * sizeof(DWORD);ThreadIdS=(DWORD * )malloc(pnum * sizeof(DWORD);mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemaphore(NULL,BUFFER_SIZE,BUFFER_SIZE,NULL);full=CreateSemaphore(NULL,0,BUFFER_SIZE+1,NULL);/*Create producer thread(s)*/countp=(struct v *)malloc(pnum+1)*sizeof(struct v);counts=(struct v *)malloc(snum+1)*sizeof(struct v);for(i=0;i<pnum;i+)countpi+1.i=i+1;ThreadHandlePi=Crea
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 蘇州2025年江蘇蘇州張家港市招聘備案制教師47人筆試歷年參考題庫附帶答案詳解
- 秦皇島2025年河北秦皇島市第二醫(yī)院招聘人事代理人員115人筆試歷年參考題庫附帶答案詳解
- 梧州2025年廣西梧州市事業(yè)單位招聘中小學(xué)幼兒園教師507人筆試歷年參考題庫附帶答案詳解
- 2025年中國多流水表市場調(diào)查研究報告
- 德州2024年山東德州天衢新區(qū)第三批招聘教師60人筆試歷年參考題庫附帶答案詳解
- 2025至2031年中國高亮度充電式手筒行業(yè)投資前景及策略咨詢研究報告
- 2025年自動補償式交流穩(wěn)壓器項目可行性研究報告
- 2025年粉碎自動送料機項目可行性研究報告
- 2025年生物活能全效眼霜項目可行性研究報告
- 2025至2031年中國文丘里噴嘴行業(yè)投資前景及策略咨詢研究報告
- 安裝承包免責(zé)協(xié)議書模板
- 《智能風(fēng)控實踐指南:從模型、特征到?jīng)Q策》記錄
- 砌筑工理論試題庫
- 新疆建設(shè)項目交通影響評價技術(shù)標(biāo)準
- 2024年成都市中考數(shù)學(xué)試卷(含詳細解析)
- 2023-2024學(xué)年浙江省溫州市七年級(上)期末英語試卷
- GMP附錄《無菌藥品》試卷測試題庫含答案
- JBT 7387-2014 工業(yè)過程控制系統(tǒng)用電動控制閥
- 小學(xué)數(shù)學(xué)教學(xué)評一體化教學(xué)探究
- 2024年保安員考試題庫【典型題】
- 人教版數(shù)學(xué)八年級下冊第十九章課堂同步練習(xí)
評論
0/150
提交評論