资源简介
哲学家就餐问题C语言源文件,操作系统课程论文要求,进程同步模拟
代码片段和文件信息
#include
#include
#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
- 上一篇:复杂网络社团发现代码
- 下一篇:课程论文 图书管理
评论
共有 条评论