📄 哲学家进餐问题.c
字号:
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <winbase.h>
using namespace std;
#define MAX_NUM 5
struct tagThread
{
DWORD IDThread;
HANDLE hThread;
int iParam;
};
HANDLE hMutex[MAX_NUM+1];
DWORD WINAPI PhilosopherOrder(LPVOID pParam)
{
int iPhiloID=*(int *)pParam;
char strPhiloID[20];
char info[64];
sprintf(strPhiloID,"%d",iPhiloID );
strcpy(info,"第");
strcat(info,strPhiloID);
strcat(info,"个哲学家");
do
{
MessageBox(NULL,"正在思考。。。",info,MB_OK|MB_ICONINFORMATION);
if (iPhiloID!=MAX_NUM-1)
{ MessageBox(NULL,"想进餐,企图使用左筷子",info,MB_OK|MB_ICONINFORMATION);
WaitForSingleObject(hMutex[iPhiloID],INFINITE);
MessageBox(NULL,"拿到左筷子,企图使用右筷子",info,MB_OK|MB_ICONINFORMATION);
WaitForSingleObject(hMutex[iPhiloID+1],INFINITE);
}
else
{ MessageBox(NULL,"想进餐,企图使用右筷子",info,MB_OK|MB_ICONINFORMATION);
WaitForSingleObject(hMutex[0],INFINITE);
MessageBox(NULL,"拿到右筷子,企图使用左筷子",info,MB_OK|MB_ICONINFORMATION);
WaitForSingleObject(hMutex[iPhiloID],INFINITE);
}
MessageBox(NULL,"拿到两只筷子,正在进餐。。。", info,MB_OK|MB_ICONINFORMATION);
MessageBox(NULL,"筷子使用完毕,准备通告",info,MB_OK|MB_ICONINFORMATION);
ReleaseMutex(hMutex[iPhiloID]);
ReleaseMutex(hMutex[(iPhiloID+1)%MAX_NUM]);
} while(MessageBox(NULL,"通告完毕,继续思考进餐吗?", info, MB_YESNO|MB_ICONINFORMATION)==IDYES);
ExitThread(NO_ERROR);
return 0;
}
DWORD WINAPI PhilosopherAll(LPVOID pParam)
{
int iPhiloID=*(int*)pParam;
char strPhiloID[20];
char info[64];
sprintf(strPhiloID,"%d",iPhiloID);
strcpy(info,"第");
strcat(info,strPhiloID);
strcat(info,"个哲学家");
do
{
MessageBox(NULL,"正在思考。。。", info,MB_OK|MB_ICONINFORMATION);
MessageBox(NULL,"想进餐,企图使用筷子",info,MB_OK|MB_ICONINFORMATION);
WaitForMultipleObjects(
2, // number of handles in the object handle array
&(hMutex[iPhiloID]), // pointer to the object-handle array
TRUE, // wait flag
INFINITE // time-out interval in milliseconds
);
MessageBox(NULL,"拿到两只筷子,正在进餐。。。",info,MB_OK|MB_ICONINFORMATION);
MessageBox(NULL,"筷子使用完毕,准备通告", info,MB_OK|MB_ICONINFORMATION);
ReleaseMutex(hMutex[iPhiloID]);
ReleaseMutex(hMutex[iPhiloID+1]);
}while(MessageBox(NULL,"通告完毕,继续思考进餐吗?", info,MB_YESNO|MB_ICONINFORMATION)==IDYES);
ExitThread(NO_ERROR);
return 0;
}
//-----------------------------------------
int main()
{
int i;
for(i=0;i<MAX_NUM;i++)
hMutex[i]=CreateMutex(NULL,false,NULL);
hMutex[MAX_NUM]=hMutex[0];
tagThread arrThread[MAX_NUM];
int Flag;
Flag=MessageBox(NULL,"全部分配方案(Y),有序分配方案(N)","选择", MB_YESNO|MB_ICONINFORMATION);
cout << "CreateThread Start" << endl;
for(i=0;i<MAX_NUM;i++)
{ arrThread[i].iParam=i;
if (Flag==IDYES)
arrThread[i].hThread =CreateThread(NULL,0, PhilosopherAll, &(arrThread[i].iParam),0,&(arrThread[i].IDThread));
else
arrThread[i].hThread =CreateThread(NULL,0,PhilosopherOrder,&(arrThread[i].iParam),0,&(arrThread[i].IDThread));
if (arrThread[i].hThread == NULL)
cout << "CreateThread error" << endl;
}
cout << "CreateThread End" << endl;
cin.get();
for (i=0;i<MAX_NUM;i++)
CloseHandle(hMutex[i]);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -