操作系统生产者消费者问题C语言

上传人:大米 文档编号:509307986 上传时间:2024-01-11 格式:DOCX 页数:7 大小:36.10KB
返回 下载 相关 举报
操作系统生产者消费者问题C语言_第1页
第1页 / 共7页
操作系统生产者消费者问题C语言_第2页
第2页 / 共7页
操作系统生产者消费者问题C语言_第3页
第3页 / 共7页
操作系统生产者消费者问题C语言_第4页
第4页 / 共7页
操作系统生产者消费者问题C语言_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《操作系统生产者消费者问题C语言》由会员分享,可在线阅读,更多相关《操作系统生产者消费者问题C语言(7页珍藏版)》请在金锄头文库上搜索。

1、操作系统概念第七版中的实验项目:生产者消费者问题。本程序中,main()函数需要 三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机 的。程序代码:#include stdio h# includestdloilh# include#includewindows.h#define BUFFER_SIZE 5typedef int buffer_;itemstruct vint ;i);buffer_item bufferBUFFER_SIZE+1buffer_item front=0,re;ar=0HANDLE mutex , empty,full;int ins

2、ert_i(ebuffer_item item)/*insert item into bufferreturn 0 if successful,otherwisereturn-1 indicating an error cond/tionif(rear+1弘(BUFFER_SIZE+1)=front )return 1;bufferrear=itemrear=(rear+1)%(BUFFER_SIZE+1);return 0;int remove_item(buffer_item *item)/*remove an object from bufferplacing it in itemret

3、urn 0 if successful,otherwisereutrn-1 indication an error condition */if(front = r)earreturn ;1* item=buffef front;front=(front+) % (BUFFER_SIZE+1);return 0;DWORD WINAPI producer (PVOID Param ) int randlstruct v data=*(struct) Waram;srand(unsigned)time 0);while (1Sleep(rand()%101*10WaitForSingleObje

4、ctempty, INFINITE );WaitForSingleObject(mutexINFINITE );rand1 =ran();printf producer has producerd %d Byn%drand1 data i);if( insert_itemrand1)printf(insert data nrrb;r!ReleaseMutex(mutex)ReleaseSemaphore(full, NULL);DWORD WINAPI consumer(PVOID Param )int rand1;struct v data=struct v ) Param;srand(un

5、sign)edtime(0) ;while (1)Sleep (rand()%101* 10);WaitForSingleObject(f,ulIlNFINITE);WaitForSingleObjectmutex,INFINITE);if(remove_item &rand1 )printf(remove data errf,);elseprintf consumer consumed d By % d nn,rand1,data);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL) ;int main(int argxhar *argv/*

6、 Get command line arguments argv)1( the number of producer threads), argtAh/ number of consumer thread,sargv3 (sleep ti)e*/* Initialize buffer int sleeptime,pnun$num; int *ThreadIdP*ThreadIdS,i; struct * countp* counts; HANDLE *ThreadHandleP,*ThreadHandleS; sleeptime=atoi(argv) pnum=atoi ( argv 2);

7、snum=atoi ( argv 3); /*srand( time (NULL); sleeptime=9;0 pnum=3; snum=3 ;*/ ThreadHandleP= ( HANDLE * ) malloc(pnum * sizeof(HANDLE); ThreadHandleS= ( HANDLE *) malloc ( snum * sizeof(HANDLE );ThreadIdP= ( int * )mallOpnum * sizeof in); ThreadIdS=(int* ) malloc ( pnum * sizeoff int); mutex=CreateMut

8、ex (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(struqt v) counts=(struct v *)malloc(sn)um*+1sizeo(f struct)v ; for(i=0 i# include time。h#include#def

9、ine BUFFER-SIZE 5 typedef int buffer-item;struct v int i;);buffer-item buffer BUFFER-SIZE+1;buffer-item front=0, rear=0;HANDLE mutex, empty,full;int insert-item(buffer-item item)(/*insert item into bufferreturn 0 if successful,otherwisereturn -1 indicating an error condition* /if (rear+1)% (BUFFER-S

10、IZE + 1 ) =front)return 1;buffer rear =item;rear=(rear+1)%(BUFFER-SIZE+1);return 0;int remove-item(buffer-item *item)/*remove an object from bufferplacing it in itemreturn 0 if successful, otherwisereutrn -1 indication an error condition */if(front = rear)return 1;*item=bufferfront;front= ( front+1)

11、 % (BUFFER-SIZE+1);return 0;)DWORD WINAPI producer (PVOID Param) int randl;struct v data=* ( struct v *)Param;srand ( unsigned) time (0 );while ( 1)Sleep ( rand () %101 * 10);WaitForSingleObject (empty,INFINITE);WaitForSingleObject ( mutex, INFINITE);rand1 =rand ();printf (producer has producerd %d

12、By %dn”,rand1,data.i);if(insert_item(rand1)printf (insert data error!n);ReleaseMutex (mutex);ReleaseSemaphore (full, 1, NULL );)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);

13、WaitForSingleObject ( mutex, INFINITE );if(remove_item(&rand1)printf(remove data error! n);elseprintf ( consumer consumed %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;DWORD *ThreadIdP,*ThreadIdS,i;struct v *countp,*counts;HANDLE *ThreadHandleP, * ThreadHandleS; /*sleeptime=atoi ( argv1); pnum=atoi ( argv2); snum=a

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 试题/考题 > 初中试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号