在 Dining Philosophers 上使用 Pthreads 显示死锁
Showing deadlock, using Pthreads, on Dining Philosophers
我们有一项任务是展示哲学家就餐问题的僵局。我们已经编写了所有代码并且代码可以编译,但是当 运行 代码时,其中一位哲学家最终吃掉了。那么这是否意味着实际上并没有发生死锁?
#include <pthread.h>
#include <stdio.h>
#define number_philo 5
pthread_mutex_t cutlery[number_philo];
void* philosopher (void* number)
{
int my_num = *((int*)number);
while (1)
{
printf("\n Philosopher %d is thinking.\n",my_num);
pthread_mutex_lock (&cutlery[my_num]);
printf("\nPhilosopher %d has left cutlery. \n",my_num);
sleep(3);
pthread_mutex_lock (&cutlery[(my_num + 1) % number_philo]);
printf("\nPhilosopher %d has right cutlery. \n",my_num);
printf("\n Philosopher %d eating.\n", my_num);
printf("\n Philosopher %d done.\n", my_num);
pthread_mutex_unlock (&cutlery[(my_num + 1) % number_philo]);
pthread_mutex_unlock (&cutlery[my_num]);
printf("\nPhilosopher %d no longer has cutlery.\n", my_num);
}
return NULL;
}
int main ()
{
int i;
pthread_t phils[number_philo];
void* return_val;
for (i = 0; i < number_philo; i++)
pthread_mutex_init (&cutlery[i], NULL);
for (i = 0; i < number_philo; i++)
pthread_create (&phils[i], NULL, philosopher, &i);
for (i = 0; i < number_philo; i++)
pthread_join (phils[i], &return_val);
return 0;
}
这是输出:output
问题出在这里:
pthread_create (&phils[i], NULL, philosopher, &i);
您正在将指向同一变量 i
的指针传递给每个线程,而这些线程(和主线程)都在以一种不正当的方式访问 i
。你会得到多个哲学家使用相同的数字和一些根本没有使用的数字。
你需要给每个哲学家它自己的变量来读取,或者等待它读取 i
,然后再循环并改变 i
。前者的例子:
int phil_num[number_philo];
/* ... */
for (i = 0; i < number_philo; i++)
{
phil_num[i] = i;
pthread_create (&phils[i], NULL, philosopher, &phil_num[i]);
}
我们有一项任务是展示哲学家就餐问题的僵局。我们已经编写了所有代码并且代码可以编译,但是当 运行 代码时,其中一位哲学家最终吃掉了。那么这是否意味着实际上并没有发生死锁?
#include <pthread.h>
#include <stdio.h>
#define number_philo 5
pthread_mutex_t cutlery[number_philo];
void* philosopher (void* number)
{
int my_num = *((int*)number);
while (1)
{
printf("\n Philosopher %d is thinking.\n",my_num);
pthread_mutex_lock (&cutlery[my_num]);
printf("\nPhilosopher %d has left cutlery. \n",my_num);
sleep(3);
pthread_mutex_lock (&cutlery[(my_num + 1) % number_philo]);
printf("\nPhilosopher %d has right cutlery. \n",my_num);
printf("\n Philosopher %d eating.\n", my_num);
printf("\n Philosopher %d done.\n", my_num);
pthread_mutex_unlock (&cutlery[(my_num + 1) % number_philo]);
pthread_mutex_unlock (&cutlery[my_num]);
printf("\nPhilosopher %d no longer has cutlery.\n", my_num);
}
return NULL;
}
int main ()
{
int i;
pthread_t phils[number_philo];
void* return_val;
for (i = 0; i < number_philo; i++)
pthread_mutex_init (&cutlery[i], NULL);
for (i = 0; i < number_philo; i++)
pthread_create (&phils[i], NULL, philosopher, &i);
for (i = 0; i < number_philo; i++)
pthread_join (phils[i], &return_val);
return 0;
}
这是输出:output
问题出在这里:
pthread_create (&phils[i], NULL, philosopher, &i);
您正在将指向同一变量 i
的指针传递给每个线程,而这些线程(和主线程)都在以一种不正当的方式访问 i
。你会得到多个哲学家使用相同的数字和一些根本没有使用的数字。
你需要给每个哲学家它自己的变量来读取,或者等待它读取 i
,然后再循环并改变 i
。前者的例子:
int phil_num[number_philo];
/* ... */
for (i = 0; i < number_philo; i++)
{
phil_num[i] = i;
pthread_create (&phils[i], NULL, philosopher, &phil_num[i]);
}