3、.h>#include#include#include#include#defineMAX_PERSON100#defineREADER0//读者#defineWRITER1//写者#defineEND-1#defineRREADER#defineWWRITERtypedefstruct_Person{HANDLEm_hThread;//定义处理线程的句柄intm_nType;//进程类型(读写)intm_nStart
4、Time;//开始时间intm_nWorkTime;//运行时间intm_nID;//进程号}Person;Persong_Persons[MAX_PERSON];intg_NumPerson=0;longg_CurrentTime=0;//基本时间片数intg_PersonLists[]={//进程队列1,W,3,5,2,W,16,5,3,R,5,2,4,W,6,5,5,R,4,3,6,R,17,7,END,};intg_NumOfReading=0;intg_NumOfWriteReques
5、t=0;//申请写进程的个数HANDLEg_hReadSemaphore;//读者信号HANDLEg_hWriteSemaphore;//写者信号boolfinished=false;//所有的读完成//boolwfinished=false;//所有的写完成voidCreatePersonList(int*pPersonList);boolCreateReader(intStartTime,intWorkTime,intID);boolCreateWriter(intStartTime,int
6、WorkTime,intID);DWORDWINAPIReaderProc(LPVOIDlpParam);DWORDWINAPIWriterProc(LPVOIDlpParam);intmain(){g_hReadSemaphore=CreateSemaphore(NULL,1,100,NULL);//创建信号灯,当前可用的资源数为1,最大为100g_hWriteSemaphore=CreateSemaphore(NULL,1,100,NULL);//创建信号灯,当前可用的资源数为1,最大为10
7、0CreatePersonList(g_PersonLists);//CreateAllthereaderandwritersprintf("Createdallthereaderandwriter创建");g_CurrentTime=0;while(true){g_CurrentTime++;Sleep(300);//300msprintf("CurrentTime=%d",g_CurrentTime);if(finished)return0;}//return0;}voidCre
8、atePersonList(int*pPersonLists){inti=0;int*pList=pPersonLists;boolRet;while(pList[0]!=END){switch(pList[1]){caseR:Ret=CreateReader(pList[2],pList[3],pList[0]);//351,w452,523,654break;caseW:Ret=CreateWriter(pList[2],pList[3],pList[0]);break;}if(!Ret)p