多线程实现哲学家进餐问题

设置一个临界区用于五个哲学家线程的创建
设置五个互斥量分别用于每一根筷子

伪代码
void phi()
{
	EnterCriticalSection(&cs);
	//进入临界区;
	printf(哲学家正在思考...);
	LeaveCriticalSection(&cs);
	//离开临界区;
	while(true)
	{
		printf(哲学家饥饿...);
		if(WaitForSingleObject(Mutex[左边的筷子],有限的等待时间))
		//拿到左边筷子
		{
			if(WaitForSingleObject(Mutex[右边的筷子],有限的等待时间)
			//等待有限的时间后,如果未获得右边的筷子,将放下左边的筷子
			//防止所有人同时拿起左边筷子出现死锁
			{
				printf(哲学家开始进餐...);
				ReleaseMutex[右边的筷子];
				//放下右边的筷子
			}
			else
			{
				printf(哲学家不具备进餐条件...);
			}
			ReleaseMutex(Mutex[左边的筷子]);
			//因为进餐结束或者未获得右边的筷子而放下左边的筷子
		}
	}
}

程序代码:

#include
#include
HANDLE PhiThread1, PhiThread2, PhiThread3, PhiThread4, PhiThread5;
//五个哲学家线程句柄
#define n 5
HANDLE Mutex[n];
//为五根筷子创建五个互斥量
CRITICAL_SECTION cs;
//创建临界区用于五个线程生成
DWORD WINAPI PhiFunc1(PVOID pvParam)//哲学家1
{
	EnterCriticalSection(&cs);
	printf("哲学家1正在思考...\n");
	LeaveCriticalSection(&cs);
	while (true)
	{
		printf("哲学家1饥饿...\n");
		if (WaitForSingleObject(Mutex[0], 5000) == 0)
		{
			if (WaitForSingleObject(Mutex[1], 5000) == 0)
			{
				printf("哲学家1正在进餐...\n");
				Sleep(3000);
				ReleaseMutex(Mutex[1]);
			}
			else
			{
				printf("哲学家1不具备进餐条件...\n");
			}
			ReleaseMutex(Mutex[0]);
		}
	}
	return 0;
}
DWORD WINAPI PhiFunc2(PVOID pvParam)
{
	EnterCriticalSection(&cs);
	printf("哲学家2正在思考...\n");
	LeaveCriticalSection(&cs);
	while (true)
	{
		printf("哲学家2饥饿...\n");
		if (WaitForSingleObject(Mutex[1], 5000) == 0)
		{
			if (WaitForSingleObject(Mutex[2], 5000) == 0)
			{
				printf("哲学家2正在进餐...\n");
				Sleep(3000);
				ReleaseMutex(Mutex[2]);
			}
			else
			{
				printf("哲学家2不具备进餐条件...\n");
			}
			ReleaseMutex(Mutex[1]);
		}
	}
	return 0;
}
DWORD WINAPI PhiFunc3(PVOID pvParam)
{
	EnterCriticalSection(&cs);
	printf("哲学家3正在思考...\n");
	LeaveCriticalSection(&cs);
	while (true)
	{
		printf("哲学家3饥饿...\n");
		if (WaitForSingleObject(Mutex[2], 5000) == 0)
		{
			if (WaitForSingleObject(Mutex[3], 5000) == 0)
			{
				printf("哲学家3正在进餐...\n");
				Sleep(3000);
				ReleaseMutex(Mutex[3]);
			}
			else
			{
				printf("哲学家3不具备进餐条件...\n");
			}
			ReleaseMutex(Mutex[2]);
		}
	}
	return 0;
}
DWORD WINAPI PhiFunc4(PVOID pvParam)
{
	EnterCriticalSection(&cs);
	printf("哲学家4正在思考...\n");
	LeaveCriticalSection(&cs);
	while (true)
	{
		printf("哲学家4饥饿...\n");
		if (WaitForSingleObject(Mutex[3], 5000) == 0)
		{
			if (WaitForSingleObject(Mutex[4], 5000) == 0)
			{
				printf("哲学家4正在进餐...\n");
				Sleep(3000);
				ReleaseMutex(Mutex[4]);
			}
			else
			{
				printf("哲学家4不具备进餐条件...\n");
			}
			ReleaseMutex(Mutex[3]);
		}
	}
	return 0;
}
DWORD WINAPI PhiFunc5(PVOID pvParam)
{
	EnterCriticalSection(&cs);
	printf("哲学家5正在思考...\n");
	LeaveCriticalSection(&cs);
	while (true)
	{
		printf("哲学家5饥饿...\n");
		if (WaitForSingleObject(Mutex[4], 5000) == 0)
		{
			if (WaitForSingleObject(Mutex[0], 5000) == 0)
			{
				printf("哲学家5正在进餐...\n");
				Sleep(3000);
				ReleaseMutex(Mutex[0]);
			}
			else
			{
				printf("哲学家5不具备进餐条件...\n");
			}
			ReleaseMutex(Mutex[4]);
		}
	}
	return 0;
}
int main(int argc, char* argv[])
{
	InitializeCriticalSection(&cs);
	//初始化临界区
	for (int i = 0; i < n; i++)
	//创建五个互斥量
	{
		Mutex[i] = CreateMutex(NULL, false, NULL);
	}
	PhiThread1 = CreateThread(NULL, 0, PhiFunc1, NULL, 0, NULL);
	PhiThread2 = CreateThread(NULL, 0, PhiFunc2, NULL, 0, NULL);
	PhiThread3 = CreateThread(NULL, 0, PhiFunc3, NULL, 0, NULL);
	PhiThread4 = CreateThread(NULL, 0, PhiFunc4, NULL, 0, NULL);
	PhiThread5 = CreateThread(NULL, 0, PhiFunc5, NULL, 0, NULL);
	getchar();
	LeaveCriticalSection(&cs);
	return 0;
}

多线程实现哲学家进餐问题_第1张图片

你可能感兴趣的:(操作系统,哲学家进餐,多线程,临界区,互斥量)