#include<stdio.h>
#include<process.h>
#include<windows.h>
#include<stdlib.h>
#include<math.h>
HANDLE Mutext[5];
int chopstick = 0;
int *Physical;
unsigned int _stdcall ThreadFun(void *p)
{
int* current = (int*)p;
while (1)
{
if (abs(*current - chopstick)<4)
{
WaitForSingleObject(Mutext[*current], INFINITE);//加锁
WaitForSingleObject(Mutext[(*current + 1) % 5], INFINITE);
printf("哲学家%d正在就餐...\n", *current);
printf("哲学家放下左边%d筷子\n", chopstick);
chopstick = (chopstick + 1) % 5;
printf("哲学家放下右边%d筷子\n\n", chopstick);
Sleep(2000);
ReleaseMutex(Mutext[(*current + 1) % 5]);//解锁
ReleaseMutex(Mutext[(*current) % 5]);
}
}
return 0;
}
int main()
{
//初始化互斥变量,初始化要传递给线程的数组
//HANDLE类型可以为(Event,Mutex,Process,Thread,Semaphore)数组
int Physical[5];
int i;
for (i = 0; i<5; i++)
{
Mutext[i] = CreateMutexA(NULL, false, NULL);//创建了一个一个匿名的互斥锁创建后
Physical[i]
死锁之五个哲学家就餐问题 C语言实现
最新推荐文章于 2024-10-01 00:00:00 发布