




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上操作系統(tǒng)概念第七版 中的實(shí)驗(yàn)項(xiàng)目:生產(chǎn)者消費(fèi)者問題。本程序中,main()函數(shù)需要三個(gè)參數(shù):主線程休眠時(shí)間;生產(chǎn)者線程數(shù);消費(fèi)者線程數(shù)。各線程的休眠等待時(shí)間是隨機(jī)的。程序代碼:#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;T
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025餐飲服務(wù)承包經(jīng)營合同樣本
- 導(dǎo)尿病人護(hù)理要點(diǎn)與流程
- 苗族女孩創(chuàng)意美術(shù)課件
- 2025年壓力容器管理人員試題
- 學(xué)生會權(quán)益部工作總結(jié)模版
- 2025年2月高一下學(xué)期入學(xué)考試生物試題總結(jié)模版
- 小學(xué)書法進(jìn)校園活動總結(jié)模版
- 合同管理工作總結(jié)模版
- 新質(zhì)生產(chǎn)力策略
- 浙江省衢州市五校聯(lián)盟2024-2025學(xué)年高二下學(xué)期期中聯(lián)考試題 地理 PDF版含答案
- 建設(shè)工程質(zhì)量管理手冊范本
- 中國文化遺產(chǎn)資料長城100字
- 高中生物選擇性必修1基礎(chǔ)背誦 課件
- 中醫(yī)適宜技術(shù)操作規(guī)程及評分標(biāo)準(zhǔn)
- 2023-2024學(xué)年貴州省六盤水市小學(xué)語文六年級期末提升測試題詳細(xì)參考答案解析
- 江蘇南通軌道交通集團(tuán)有限公司運(yùn)營分公司社會招聘工作人員考試真題及答案2022
- 頸椎JOA腰椎JOA 評分-表格-日本骨科協(xié)會評估治療
- 人工智能時(shí)代小學(xué)勞動教育的現(xiàn)實(shí)困境與突破路徑 論文
- 野生動物管理學(xué)智慧樹知到答案章節(jié)測試2023年東北林業(yè)大學(xué)
- 國際友人在中國智慧樹知到答案章節(jié)測試2023年西北大學(xué)
- 函數(shù)的零點(diǎn)與方程的解(說課稿)
評論
0/150
提交評論