📄 mphills.h
字号:
extern void DrawEatingPhilosophor(int nNumber,int nTime);
static int sp[6] = {0,0,0,0,0,0};
CRITICAL_SECTION cs;
HANDLE hndEvents[6];
int phi1=1000;
int phi2=5000;
int phi3=10000;
int phi4=1000;
int phi5=2000;
UINT Philosophor1(LPVOID lVOid)
{
while(1)
{
int nSHouldWait = 0;
//Entry Criteria
EnterCriticalSection(&cs);
//I have my spoons ready
if(!sp[1] && !sp[5])
{
sp[1]=sp[5]=1;
//I will eat
nSHouldWait=0;
}
else //I cannot eat
nSHouldWait=1;
LeaveCriticalSection(&cs);
if(nSHouldWait)
WaitForSingleObject(hndEvents[1],INFINITE);
//Entry Criteria
//######################################
//Eat here as long as you want
//AfxMessageBox("Philosophor1 \n\n EATING");
//::MessageBox(NULL,"Philosophor1 \n\n EATING","lab",0);
DrawEatingPhilosophor(1,phi1);
//######################################
//Exit Criteria
HANDLE hndTmp = NULL;
EnterCriticalSection(&cs);
//Philosophor1 can release only Philosophor2 or Philosophor5
//But I choose to release Philosophor2
sp[1]=sp[5]=0;//surrender my spoons
if(!sp[1] && !sp[2]) // release Philosophor2
{
sp[1]=sp[2]=1;
hndTmp = hndEvents[2];
}
else
if(!sp[4] && !sp[5]) // release Philosophor5
{
sp[4]=sp[5]=1;
hndTmp = hndEvents[5];
}
LeaveCriticalSection(&cs);
//Exit Criteria
if(hndTmp != NULL)
{
SetEvent(hndTmp);
}
else
AfxMessageBox("problem 1");
Sleep(10);
}
return 21;
}
UINT Philosophor2(LPVOID lVOid)
{
while(1)
{
int nSHouldWait = 0;
//Entry Criteria
EnterCriticalSection(&cs);
//I have my spoons ready
if(!sp[1] && !sp[2])
{
//AfxMessageBox("cannot come here");
//I will eat
sp[1]=sp[2]=1;
nSHouldWait=0;
}
else //I cannot eat
{
//AfxMessageBox("perfect");
nSHouldWait=1;
}
LeaveCriticalSection(&cs);
if(nSHouldWait)
WaitForSingleObject(hndEvents[2],INFINITE);
//Entry Criteria
//######################################
//Eat here as long as you want
//::MessageBox(NULL,"Philosophor2 \n\n EATING","lab",0);
//AfxMessageBox("Philosophor2 \n\n EATING");
DrawEatingPhilosophor(2,phi2);
//######################################
//Exit Criteria
HANDLE hndTmp = NULL;
EnterCriticalSection(&cs);
//Philosophor2 can release only Philosophor3 or Philosophor1
//But I choose to release Philosophor3
sp[1]=sp[2]=0;//surrender my spoons
if(!sp[2] && !sp[3]) // release Philosophor3
{
sp[2]=sp[3]=1;
hndTmp = hndEvents[3];
}
else
if(!sp[1] && !sp[5]) // release Philosophor1
{
sp[1]=sp[5]=1;
hndTmp = hndEvents[1];
}
LeaveCriticalSection(&cs);
//Exit Criteria
if(hndTmp != NULL)
{
SetEvent(hndTmp);
}
else
AfxMessageBox("problem 2");
Sleep(10);
}
return 21;
}
UINT Philosophor3(LPVOID lVOid)
{
while(1)
{
int nSHouldWait = 0;
//Entry Criteria
EnterCriticalSection(&cs);
//I have my spoons ready
if(!sp[2] && !sp[3])
{
//I will eat
sp[2]=sp[3]=1;
nSHouldWait=0;
}
else //I cannot eat
nSHouldWait=1;
LeaveCriticalSection(&cs);
if(nSHouldWait)
WaitForSingleObject(hndEvents[3],INFINITE);
//Entry Criteria
//######################################
//Eat here as long as you want
//::MessageBox(NULL,"Philosophor3 \n\n EATING","lab",0);
//AfxMessageBox("Philosophor3 \n\n EATING");
DrawEatingPhilosophor(3,phi3);
//######################################
//Exit Criteria
HANDLE hndTmp = NULL;
EnterCriticalSection(&cs);
//Philosophor3 can release only Philosophor4 or Philosophor2
//But I choose to release Philosophor4
sp[2]=sp[3]=0;//surrender my spoons
if(!sp[3] && !sp[4]) // release Philosophor4
{
//AfxMessageBox("releasing 4");
sp[3]=sp[4]=1;
hndTmp = hndEvents[4];
}
else
if(!sp[1] && !sp[2]) // release Philosophor2
{
sp[1]=sp[2]=1;
hndTmp = hndEvents[2];
}
LeaveCriticalSection(&cs);
//Exit Criteria
if(hndTmp != NULL)
{
SetEvent(hndTmp);
}
else
AfxMessageBox("problem 3");
Sleep(10);
}
return 21;
}
UINT Philosophor4(LPVOID lVOid)
{
while(1)
{
int nSHouldWait = 0;
//Entry Criteria
EnterCriticalSection(&cs);
//I have my spoons ready
if(!sp[3] && !sp[4])
{
//I will eat
sp[3]=sp[4]=1;
nSHouldWait=0;
}
else //I cannot eat
nSHouldWait=1;
LeaveCriticalSection(&cs);
if(nSHouldWait)
WaitForSingleObject(hndEvents[4],INFINITE);
//Entry Criteria
//######################################
//Eat here as long as you want
//::MessageBox(NULL,"Philosophor4 \n\n EATING","lab",0);
//AfxMessageBox("Philosophor4 \n\n EATING");
DrawEatingPhilosophor(4,phi4);
//######################################
//Exit Criteria
HANDLE hndTmp = NULL;
EnterCriticalSection(&cs);
//Philosophor4 can release only Philosophor5 or Philosophor3
//But I choose to release Philosophor5
sp[3]=sp[4]=0;//surrender my spoons
if(!sp[4] && !sp[5]) // release Philosophor5
{
sp[4]=sp[5]=1;
hndTmp = hndEvents[5];
}
else
if(!sp[2] && !sp[3]) // release Philosophor3
{
sp[2]=sp[3]=1;
hndTmp = hndEvents[3];
}
LeaveCriticalSection(&cs);
//Exit Criteria
if(hndTmp != NULL)
{
SetEvent(hndTmp);
}
else
AfxMessageBox("problem 4");
Sleep(10);
}
return 21;
}
UINT Philosophor5(LPVOID lVOid)
{
while(1)
{
int nSHouldWait = 0;
//Entry Criteria
EnterCriticalSection(&cs);
//I have my spoons ready
if(!sp[4] && !sp[5])
{
//I will eat
sp[4]=sp[5]=1;
nSHouldWait=0;
}
else //I cannot eat
nSHouldWait=1;
LeaveCriticalSection(&cs);
if(nSHouldWait)
WaitForSingleObject(hndEvents[5],INFINITE);
//Entry Criteria
//######################################
//Eat here as long as you want
//::MessageBox(NULL,"Philosophor5 \n\n EATING","lab",0);
DrawEatingPhilosophor(5,phi5);
//######################################
//Exit Criteria
HANDLE hndTmp = NULL;
EnterCriticalSection(&cs);
//Philosophor5 can release only Philosophor1 or Philosophor4
//But I choose to release Philosophor1
sp[4]=sp[5]=0;//surrender my spoons
if(!sp[1] && !sp[5]) // release Philosophor1
{
sp[1]=sp[5]=1;
hndTmp = hndEvents[1];
}
else
if(!sp[3] && !sp[4]) // release Philosophor4
{
sp[3]=sp[4]=1;
hndTmp = hndEvents[4];
}
LeaveCriticalSection(&cs);
//Exit Criteria
if(hndTmp != NULL)
{
SetEvent(hndTmp);
}
else
AfxMessageBox("problem 5");
Sleep(10);
}
return 21;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -