linux下的哲学家问题...

艾味奶昔Elon 2013-12-11 07:50:50
问题:
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.


...全文
302 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
艾味奶昔Elon 2013-12-16
  • 打赏
  • 举报
回复
引用 2 楼 u013168863 的回复:
这个循环貌似没有结束啊,ctrl+z。
抱歉..好几天都没上网了。 ctrl+c,ctrl+z都不行。抓狂了,就直接把终端关掉,重头再运行。 最后的解决办法: 再开一个终端,ps -a,再kill掉那五个哲学家子进程。 现在的问题是输出不对..不知道问题出在哪儿T T
艾味奶昔Elon 2013-12-16
  • 打赏
  • 举报
回复
引用 1 楼 turingo 的回复:
CTRL+C一定会停止的。
抱歉..好几天都没上网了。 的确是不行的。无论是ctrl+c或者是ctrl+z都不行。 最后的解决方法: 打开另一个终端,ps -a 查看当前所有进程,接着kill掉在执行那五个子进程.. 再一问:输出结果不是预期中的结果。问题出在哪儿了?互斥变量应该没用错。难道是printf函数或者是临界区没设置对?
sanqianluweiyang 2013-12-15
  • 打赏
  • 举报
回复
这个循环貌似没有结束啊,ctrl+z。
图灵狗 2013-12-11
  • 打赏
  • 举报
回复
CTRL+C一定会停止的。
引用 楼主 u011408943 的回复:
问题: 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.

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧