操作系统 生产者-消费者模型模拟进程调度.docVIP

  • 71
  • 0
  • 约4.9千字
  • 约 8页
  • 2017-01-30 发布于重庆
  • 举报

操作系统 生产者-消费者模型模拟进程调度.doc

(A4纸或A3纸折叠)#includestdlib.h #includetime.h #includewindows.h #includesys/timeb.h #includestdio.h HANDLE hFull; HANDLE hEmpty; HANDLE hMutex; HANDLE hThreadP[50]; HANDLE hThreadC[50]; int iHandle_Count=0;//操作数 int iStore=0;//共享资源数 int iGood_id=0;//货物ID /*******货物*********/ struct goods { int iGood_id; int iProduct_id; }goods[100]; /**********生产者和消费者信息*************/ typedef struct { int iId; int iMax_Store; }threadinfo; /************运行函数*************/ DWORD WINAPI Producer(LPVOID *INFO)//生产者函数 { int i; threadinfo* info=(threadinfo*)INFO; for(i=0;iinfo-iMax_Store;i++) { WaitForSingleObject(hEmpty,INFINITE); WaitForSingleObject(hMutex,INFINITE); iHandle_Count++; iStore++; iGood_id++; goods[i].iGood_id=iGood_id; goods[i].iProduct_id=info-iId; printf(%d号生产者 生产了%d号产品 目前操作次数:%d 仓库中共享资源数量:%d\n ,goods[i].iProduct_id,goods[i].iGood_id,iHandle_Count,iStore); ReleaseSemaphore(hFull,1,NULL); ReleaseMutex(hMutex); } } DWORD WINAPI Consumer(LPVOID *INFO)//消费者函数 { int i; threadinfo* info=(threadinfo*)INFO; for(i=0;iinfo-iMax_Store;i++) { WaitForSingleObject(hFull,INFINITE); WaitForSingleObject(hMutex,INFINITE); iHandle_Count++; iStore--; printf(%d号消费者 消费了%d号产品 目前操作次数:%d 仓库中共享资源数量:%d\n ,info-iId,goods[i].iGood_id,iHandle_Count,iStore); ReleaseSemaphore(hEmpty,1,NULL); ReleaseMutex(hMutex); } } void WINAPI run(int iMax_Store, int iProducer_num,int iConsumer_num) { threadinfo infoP[50]; threadinfo infoC[50]; int iProducer,iConsumer; hMutex=CreateMutex(NULL,FALSE,NULL); hFull=CreateSemaphore(NULL,0,iMax_Store,NULL); hEmpty=CreateSemaphore(NULL,iMax_Store,iMax_Store,NULL); WaitForSingleObject(hMutex,INFINITE); /**********创建消费者和生产者线程**************/ printf(******创建生产者和消费者线程******\n); for(iProducer=1;iProducer=iProducer_num;iProducer++) { infoP[iProducer-1].iId=iProducer; infoP[iProducer-1].iMax_Store=iMax_Store; hThreadP[iProducer-1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Producer,infoP[iProducer-1],0,NULL);//创建生产

文档评论(0)

1亿VIP精品文档

相关文档