问题:
1、怎么互斥变量没起作用?
2、执行后,程序一直在跑,ctrl+c不起作用
代码:
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<semaphore.h>
#include<stdio.h>
#define NUM 5 //哲学家人数
#define LEFT (ph_num-1)%NUM //左邻
#define RIGHT (ph_num+1)%NUM //右邻
typedef enum{thinking,hungry,eating}STATE_TYPE; //哲学家的状态:思考,饥饿,进食
sem_t ph[NUM]; //每个哲学家有个信号量,初值为0
sem_t mutex; //信号量,保护临界区
STATE_TYPE state[NUM]; //定义整型数组
//测试操作
void test(int ph_num)
{
if((state[ph_num]==hungry)&&(state[LEFT]!=eating)&&(state[RIGHT]!=eating))
{
state[ph_num]=eating; //左右邻都不处于eating状态,则将state[ph_num]状态置eating
sem_post(&ph[ph_num]); //V操作,将信号量-1
}
}
//取叉操作
void get_forks(int ph_num)
{
sem_wait(&mutex); //P操作,信号量-1,保护临界区
state[ph_num]=hungry; //将state[ph_num]状态置为hungry
printf("Philosopher %d is hungry.\n",ph_num+1);
test(ph_num); //测试函数
sem_post(&mutex); //V操作,信号量+1
sem_wait(&ph[ph_num]); //第ph_num位哲学家信号量-1
}
//放叉操作
void put_forks(int ph_num)
{
printf("Philosopher %d is full and put down chopsticks.\n",ph_num+1);
sem_wait(&mutex); //P操作,信号量-1,保护临界区
state[ph_num]=thinking; //将state[ph_num]状态置为hungry
printf("Philosopher %d is thinking\n",ph_num+1);
test(LEFT); //检验左邻是否有waiting,若无,则继续;若有,则wake
test(RIGHT); //检验右邻...
sem_post(&mutex);
}
//吃操作
void eat(int ph_num)
{
printf("Philosopher %d is eating...\n",ph_num+1);
sleep(2);
}
//哲学家操作
void *philosopher(int ph_num)
{
while(1)
{
printf("**********************************\n");
printf("Dining...\n");
get_forks(ph_num); //取叉
eat(ph_num); //eating状态
put_forks(ph_num); //进食完毕,放叉
}
}
//主函数
void main()
{
int i;
pid_t pid;
sem_init(&mutex,0,1); //初始化信号量为1,且信号量在进程间共享
for(i=0;i<NUM;i++)
{
sem_init(&ph[i],0,0); //初始化ph[i]的值为0,即哲学家都在thinking
printf("Philosopher %d is thinking.\n",i+1);
}
pid_t p0 = fork();
if( p0 == 0 )
{
philosopher(0);
return ; //若此处没有return 0 p1 进程也会执行 pid_t p2=fork()语句
}
pid_t p1 = fork();
if( p1 == 0 )
{
philosopher(1);
return ;
}
pid_t p2 = fork();
if( p2 == 0 )
{
philosopher(2);
return ;
}
pid_t p3 = fork();
if( p3 == 0 )
{
philosopher(3);
return ;
}
pid_t p4 = fork();
if( p4 == 0 )
{
philosopher(4);
return ;
}
}
linux中执行结果:
Philosopher 1 is thinking.
Philosopher 2 is thinking.
Philosopher 3 is thinking.
Philosopher 4 is thinking.
Philosopher 5 is thinking.
**********************************
Dining...
Philosopher 1 is hungry.
Philosopher 1 is eating...
**********************************
Dining...
Philosopher 2 is hungry.
Philosopher 2 is eating...
**********************************
Dining...
Philosopher 3 is hungry.
Philosopher 3 is eating...
**********************************
Dining...
Philosopher 4 is hungry.
Philosopher 4 is eating...
**********************************
Dining...
Philosopher 5 is hungry.
Philosopher 5 is eating...
zncj@zncj-desktop:~$ Philosopher 1 is full and put down chopsticks.
Philosopher 1 is thinking
**********************************
Dining...
Philosopher 1 is hungry.
Philosopher 1 is eating...
Philosopher 2 is full and put down chopsticks.
Philosopher 2 is thinking
**********************************
Philosopher 3 is full and put down chopsticks.
Dining...
Philosopher 3 is thinking
Philosopher 2 is hungry.