欢迎来到天天文库
浏览记录
ID:49615208
大小:166.07 KB
页数:9页
时间:2020-03-02
《OS实验6——信号量.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验六信号量一、实验内容(1)生产者/消费者问题:在windows和linux操作系统上,利用各自操作系统提供的mutex和信号量机制,实现生产者/消费者问题。具体要求见“OperatingSystemConcepts(SeventhEdition)”Chapter6后的project。(2)利用信号量解决读者——写者问题(选做)。二、实验目的通过实验,熟练掌握进程同步和互斥算法,理解生产者/消费者问题;掌握windows和linux中多进程/线程的同步互斥方法的使用。三、设计思路1、创建三个信号量:mutex,full,empty。2、主程序分别创建10个生产者线程和1
2、0个消费者线程。3、生产者线程:睡眠一段随机时间;向缓冲区插入一个随机数。4、消费者线程:睡眠一段随机时间;从缓冲区内取出一项。5、主程序睡眠一段时间后结束整个程序。四、源程序及注释1、windows#include#include#include#include#include#defineBUFFER_SIZE5intbuffer[BUFFER_SIZE];intcnt=0;//现有产品数量//定义并初始化信号量HANDLEmutex,empty,full;voidinit
3、(){mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemaphore(NULL,5,5,NULL);full=CreateSemaphore(NULL,0,5,NULL);}intinsert_item(intitem,intorder){//向缓冲插入一个随机量intflag=-1;WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);//临界区if(cnt4、0;//插入成功printf("producer%dproduced%d",order,cnt);}ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);returnflag;}intremove_item(int&item,intorder){//删除缓冲区最后一个量intflag=-1;WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);//临界区if(cnt>0){item=buffer[cnt-1];buffer[cnt-1]5、=0;flag=0;printf("consumer%dconsumed%d",order,cnt);cnt--;}ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);returnflag;}DWORDWINAPIproducer(void*param){srand((unsigned)time(NULL));intrandom;while(true){Sleep((rand()%10+1)*1000);random=rand()%50+1;if(insert_item(random,(DWORD)param))pri6、ntf("reporterrorcondition");}}DWORDWINAPIconsumer(void*param){srand((unsigned)time(NULL));intrandom;while(true){Sleep((rand()%10+1)*1000);if(remove_item(random,(DWORD)param))printf("reporterrorcondition");}}intmain(intargc,char*argv[]){init();//主线程睡眠时间staticconstintsleepTime=10000;//生7、产者线程数量staticconstintproducerThs=10;//消费者线程数量staticconstintconsumerThs=10;//缓存区初始化memset(buffer,0,sizeofbuffer);DWORDProducerThreadId[producerThs];DWORDConsumerThreadId[consumerThs];HANDLEProducerThreadHandles[producerThs];HANDLEConsumerThreadHandles[consumerThs];int
4、0;//插入成功printf("producer%dproduced%d",order,cnt);}ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);returnflag;}intremove_item(int&item,intorder){//删除缓冲区最后一个量intflag=-1;WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);//临界区if(cnt>0){item=buffer[cnt-1];buffer[cnt-1]
5、=0;flag=0;printf("consumer%dconsumed%d",order,cnt);cnt--;}ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);returnflag;}DWORDWINAPIproducer(void*param){srand((unsigned)time(NULL));intrandom;while(true){Sleep((rand()%10+1)*1000);random=rand()%50+1;if(insert_item(random,(DWORD)param))pri
6、ntf("reporterrorcondition");}}DWORDWINAPIconsumer(void*param){srand((unsigned)time(NULL));intrandom;while(true){Sleep((rand()%10+1)*1000);if(remove_item(random,(DWORD)param))printf("reporterrorcondition");}}intmain(intargc,char*argv[]){init();//主线程睡眠时间staticconstintsleepTime=10000;//生
7、产者线程数量staticconstintproducerThs=10;//消费者线程数量staticconstintconsumerThs=10;//缓存区初始化memset(buffer,0,sizeofbuffer);DWORDProducerThreadId[producerThs];DWORDConsumerThreadId[consumerThs];HANDLEProducerThreadHandles[producerThs];HANDLEConsumerThreadHandles[consumerThs];int
此文档下载收益归作者所有