哲学家用餐问题 求助 急!!!
发布网友
发布时间:2023-07-14 08:35
我来回答
共1个回答
热心网友
时间:2024-11-25 13:16
#include <pthread.h>
#include <stdio.h>
#define N 5
#define LEFT (i+N-1)%N
#define RIGHT (i+1)%N
#define THINK_TIME 3
#define EAT_TIME 2
enum { THINKING, HUNGRY, EATING } state[N];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER, s[N];
void test(int i)
{
if (state[i] == HUNGRY
&& state[LEFT] != EATING
&& state[RIGHT] != EATING)
{
state[i] = EATING;
pthread_mutex_unlock(&s[i]);
}
}
void take_forks(int i)
{
pthread_mutex_lock(&mutex);
state[i] = HUNGRY;
test(i);
pthread_mutex_unlock(&mutex);
pthread_mutex_lock(&s[i]);
}
void put_forks(int i)
{
pthread_mutex_lock(&mutex);
state[i] = THINKING;
test(LEFT);
test(RIGHT);
pthread_mutex_unlock(&mutex);
}
void think(int i)
{
printf("philosopher %d is thinking...\n", i);
sleep(THINK_TIME);
}
void eat(int i)
{
printf("philosopher %d is eating...\n", i);
sleep(EAT_TIME);
}
void* phi(void* vargp)
{
int i = *(int*)vargp;
while (1)
{
think(i);
take_forks(i);
eat(i);
put_forks(i);
}
return NULL;
}
int main()
{
int i;
pthread_t tid[N];
for (i = 0; i < N; i++)
pthread_create(&tid[i], NULL, phi, (void*)(&i));
for (i = 0; i < N; i++)
pthread_join(tid[i], NULL);
return 0;
}追问能说明一下一些重要的变量定义吗
就注释一下
谢谢了
追答其实哲学家就餐就是一个这样的逻辑,只有当某个哲学家左边和右边的筷子都空闲就可以就餐,一旦遇到有一个或者两个都不空闲,就继续等待,