读者写者问题.docx
- 文档编号:341631
- 上传时间:2022-10-09
- 格式:DOCX
- 页数:5
- 大小:203.25KB
读者写者问题.docx
《读者写者问题.docx》由会员分享,可在线阅读,更多相关《读者写者问题.docx(5页珍藏版)》请在冰豆网上搜索。
#include
#include
#include
#include
#include
#include
#defineMAX_PERSON100
#defineREADER0//读者
#defineWRITER1//写者
#defineEND-1
#defineRREADER
#defineWWRITER
typedefstruct_Person
{
HANDLEm_hThread;//定义处理线程的句柄
intm_nType;//进程类型(读写)
intm_nStartTime;//开始时间
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_NumOfWriteRequest=0;//申请写进程的个数
HANDLEg_hReadSemaphore;//读者信号
HANDLEg_hWriteSemaphore;//写者信号
boolfinished=false;//所有的读完成
//boolwfinished=false;//所有的写完成
voidCreatePersonList(int*pPersonList);
boolCreateReader(intStartTime,intWorkTime,intID);
boolCreateWriter(intStartTime,intWorkTime,intID);
DWORDWINAPIReaderProc(LPVOIDlpParam);
DWORDWINAPIWriterProc(LPVOIDlpParam);
intmain()
{
g_hReadSemaphore=CreateSemaphore(NULL,1,100,NULL);//创建信号灯,当前可用的资源数为1,最大为100
g_hWriteSemaphore=CreateSemaphore(NULL,1,100,NULL);//创建信号灯,当前可用的资源数为1,最大为100
CreatePersonList(g_PersonLists);//CreateAllthereaderandwriters
printf("创建所有读者写者\n");
g_CurrentTime=0;
while(true)
{
g_CurrentTime++;
Sleep(300);//300ms
printf("当前时间=%d\n",g_CurrentTime);
if(finished)return0;
}//return0;
}
voidCreatePersonList(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,654
break;caseW:
Ret=CreateWriter(pList[2],pList[3],pList[0]);
break;
}
if(!
Ret)
printf("创建%d错误\n",pList[0]);
pList+=4;//movetonextpersonlist
}
}
DWORDWINAPIReaderProc(LPVOIDlpParam)//读过程
{
Person*pPerson=(Person*)lpParam;
//waitforthestarttime
while(g_CurrentTime!
=pPerson->m_nStartTime)
{}
printf("读者%d正在请求\n",pPerson->m_nID);
printf("\n\n************************************************\n");
//waitforthewriterequest
WaitForSingleObject(g_hReadSemaphore,INFINITE);if(g_NumOfReading==0)
{
WaitForSingleObject(g_hWriteSemaphore,INFINITE);}
g_NumOfReading++;
ReleaseSemaphore(g_hReadSemaphore,1,NULL);
pPerson->m_nStartTime=g_CurrentTime;
printf("读者%d正在读取共享缓冲区\n",pPerson->m_nID);
printf("\n\n************************************************\n");
while(g_CurrentTime<=pPerson->m_nStartTime+pPerson->m_nWorkTime)
{}
printf("读者%d正在退出\n",pPerson->m_nID);
printf("\n\n************************************************\n");
WaitForSingleObject(g_hReadSemaphore,INFINITE);
g_NumOfReading--;
if(g_NumOfReading==0)
{ReleaseSemaphore(g_hWriteSemaphore,1,NULL);//此时没有读者,可以写
}
ReleaseSemaphore(g_hReadSemaphore,1,NULL);
if(pPerson->m_nID==4)finished=true;//所有的读写完成
ExitThread(0);
return0;
}
DWORDWINAPIWriterProc(LPVOIDlpParam)
{
Person*pPerson=(Person*)lpParam;
//waitforthestarttime
while(g_CurrentTime!
=pPerson->m_nStartTime)
{}
printf("写者%d正在请求进行写操作\n",pPerson->m_nID);
printf("\n\n************************************************\n");
WaitForSingleObject(g_hWriteSemaphore,INFINITE);
//modifythewriter'srealstarttime
pPerson->m_nStartTime=g_CurrentTime;
printf("写者%d正在向共享缓冲区写内容\n",pPerson->m_nID);
while(g_CurrentTime<=pPerson->m_nStartTime+pPerson->m_nWorkTime)
{}
printf("写者%d正在退出\n",pPerson->m_nID);
printf("\n\n************************************************\n");
//g_NumOfWriteRequest--;
ReleaseSemaphore(g_hWriteSemaphore,1,NULL);
if(pPerson->m_nID==4)finished=true;//所有的读写完成
ExitThread(0);
return0;
}
boolCreateReader(intStartTime,intWorkTime,intID)
{
DWORDdwThreadID;
if(g_NumPerson>=MAX_PERSON)
returnfalse;
Person*pPerson=&g_Persons[g_NumPerson];
pPerson->m_nID=ID;
pPerson->m_nStartTime=StartTime;
pPerson->m_nWorkTime=WorkTime;
pPerson->m_nType=READER;
g_NumPerson++;
//CreateanNewThread
pPerson->m_hThread=CreateThread(NULL,0,ReaderProc,(LPVOID)pPerson,0,&dwThreadID);
if(pPerson->m_hThread==NULL)
returnfalse;
returntrue;
}
boolCreateWriter(intStartTime,intWorkTime,intID)
{
DWORDdwThreadID;
if(g_NumPerson>=MAX_PERSON)
returnfalse;
Person*pPerson=&g_Persons[g_NumPerson];
pPerson->m_nID=ID;
pPerson->m_nStartTime=StartTime;
pPerson->m_nWorkTime=WorkTime;
pPerson->m_nType=WRITER;
g_NumPerson++;
//CreateanNewThread
pPerson->m_hThread=CreateThread(NULL,0,WriterProc,(LPVOID)pPerson,0,&dwThreadID);
if(pPerson->m_hThread==NULL)
returnfalse;
returntrue;
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 读者 问题
![提示](https://static.bdocx.com/images/bang_tan.gif)