哲学家就餐问题C语言实现(涉及多线程、信号量等)

哲学家就餐问题C语言实现(涉及多线程、信号量等)

1、实验原理

  由Dijkstra提出并解决的哲学家进餐问题(The Dinning Philosophers Problem)是典型的同步问题。该问题是描述有五个哲学家共用一张圆桌,分别坐在周围的五张椅子上,在圆桌上有五个碗和五只筷子,他们的生活方式是交替地进行思考和进餐。平时,一个哲学家进行思考,饥饿时便试图取用其左右最靠近他的筷子,只有在他拿到两只筷子时才能进餐。进餐完毕,放下筷子继续思考。

2、实验内容

  显示出每个哲学家的工作状态,如吃饭,思考。连续运行30次以上都未出现死锁现象。

3、代码部分

3.1 重要代码展示

重要代码展示:
(1)初始化int型数组ID[NUM]标识线程ID号,用于后续传参;初始化信号量数组sem_chopsticks[NUM]模拟五根筷子;初始化信号量sem_eaters=4,用来控制同时可拿起筷子的最大人数。

int ID[NUM]={0,1,2,3,4};
sem_t sem_chopsticks[NUM];
sem_t sem_eaters;
sem_init(&sem_eaters,0,NUM-1)

(2)线程的例程处理函数,先等待信号量sem_eaters,再等待信号量sem_chopsticks[pthread_id],这两个信号量不为零说明此时同时拿起筷子的人数小于4,且该哲学家编号相同的筷子(记为左筷子)可以被拿起,所以拿起左子,然后等待右筷子sem_chopsticks[pthread_id+1]信号量,拿到右筷子后便可以进餐,随机释放上述三个信号量。

void philosopher(void * ptid){
    int pthread_id = *(int *)ptid%NUM;
    printf("%d philosopher is thinking...\n",(int)pthread_id);
    sem_wait(&sem_eaters);
    sem_wait(&sem_chopsticks[pthread_id]);
    printf("%d philosopher takes chopstick %d...\n",(int)pthread_id,(int)pthread_id);
    sem_wait(&sem_chopsticks[(pthread_id+1)%NUM]);
    printf("%d philosopher takes chopstick %d...\n",(int)pthread_id,((int)pthread_id+1)%NUM);
printf("%d philosopher is eating, %d philosopher had already dined.\n",(int)pthread_id,eaters_num);
    sem_post(&sem_chopsticks[(pthread_id+1)%NUM]);
    sem_post(&sem_chopsticks[pthread_id]);
    sem_post(&sem_eaters);
    eaters_num++;
    printf("%d philosopher had dined, by now %d philosopher had already dined.\n",(int)pthread_id,eaters_num);}

(3)循环创建5个线程模拟五位哲学家,线程例子程函数为philosopher,并传入参数ID[i]标识线程的ID。

pthread_t philosopher_threads[NUM];
        sem_signal_init();
        for ( i= 0; i < NUM; i++) {
            printf("%d times\n",i);
            if (pthread_create(&philosopher_threads[i], NULL, (void *)&philosopher,&ID[i]) != 0){
                perror("oops:pthread_create error!");
                exit(1);
            }

3.2 源码

#include <unistd.h>
#include "pthread.h"
#include "stdio.h"
#include "stdlib.h"
#include "semaphore.h"

#define NUM 5
int ID[NUM]={0,1,2,3,4};
sem_t sem_chopsticks[NUM];
sem_t sem_eaters;
int eaters_num=0;
void sem_signal_init(){
    int i;
    for (i=0; i<NUM; i++){
        if (sem_init(&sem_chopsticks[i],0,1) == -1){
            perror("oops:em_init error!");
            exit(1);
        }
    }
    if (sem_init(&sem_eaters,0,NUM-1) == -1){
        perror("oops:em_init error!");
        exit(1);
    }
}

void philosopher(void * ptid){
    int pthread_id = *(int *)ptid%NUM;
    printf("%d philosopher is thinking...\n",(int)pthread_id);
    sem_wait(&sem_eaters);
    sem_wait(&sem_chopsticks[pthread_id]);
    printf("%d philosopher takes chopstick %d...\n",(int)pthread_id,(int)pthread_id);
    sem_wait(&sem_chopsticks[(pthread_id+1)%NUM]);
    printf("%d philosopher takes chopstick %d...\n",(int)pthread_id,((int)pthread_id+1)%NUM);
    printf("%d philosopher is eating, %d philosopher had already dined.\n",(int)pthread_id,eaters_num);
    sem_post(&sem_chopsticks[(pthread_id+1)%NUM]);
    sem_post(&sem_chopsticks[pthread_id]);
    sem_post(&sem_eaters);
    eaters_num++;
    printf("%d philosopher had dined, by now %d philosopher had already dined.\n",(int)pthread_id,eaters_num);

}

int main(){
    int i,l,j,k;
    for (l = 0; l < 1000; ++l) {
        printf("**********************%d times try ******************************",l+1);
        pthread_t philosopher_threads[NUM];
        sem_signal_init();
        for ( i= 0; i < NUM; i++) {
            printf("%d times\n",i);
            if (pthread_create(&philosopher_threads[i], NULL, (void *)&philosopher,&ID[i]) != 0){
                perror("oops:pthread_create error!");
                exit(1);
            }
        }

        for ( j = 0; j < NUM; j++) {
            pthread_join(philosopher_threads[j], NULL);
        }
        sem_destroy(&sem_eaters);
        for (k = 0; k < NUM ; ++k) {
            sem_destroy(&sem_chopsticks[k]);
        }
        eaters_num = 0;
//        sleep(2);
    }

    return 0;
}

3.3 运行结果展示

  本程序用五个线程模拟五位哲学家,用五个sem_t型信号量模拟五根筷子(临界资源),解题的思想是:每次让每位哲学家优先拿起自己左边的筷子,并限制最多有四个人同时拿起筷子,从而避免了死锁情况,该程序模拟五个哲学家进餐一次的情况,并重复模拟了1000次以上来检验是否出现死锁情况,经检验程序运行正常,没有出现死锁状况,上述图片仅展示了第1次和第1000次的情况。

在这里插入图片描述

哲学家就餐问题是一个经典的并发问题,可以通过使用互斥锁和条件变量来解决。下面是一个使用C语言编写的哲学家就餐问题代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #define NUM_PHILOSOPHERS 5 #define LEFT_FORK (id + NUM_PHILOSOPHERS - 1) % NUM_PHILOSOPHERS #define RIGHT_FORK (id + 1) % NUM_PHILOSOPHERS pthread_mutex_t forks[NUM_PHILOSOPHERS]; pthread_cond_t cond_vars[NUM_PHILOSOPHERS]; void* philosopher(void* arg) { int id = *(int*)arg; int num_eaten = 0; while (num_eaten < 3) { // thinking printf("Philosopher %d is thinking\n", id); // pick up forks pthread_mutex_lock(&forks[LEFT_FORK]); pthread_mutex_lock(&forks[RIGHT_FORK]); // eating printf("Philosopher %d is eating\n", id); num_eaten++; // put down forks pthread_mutex_unlock(&forks[LEFT_FORK]); pthread_mutex_unlock(&forks[RIGHT_FORK]); // wait before thinking again usleep(1000000); } printf("Philosopher %d is done eating\n", id); return NULL; } int main() { pthread_t threads[NUM_PHILOSOPHERS]; int ids[NUM_PHILOSOPHERS]; for (int i = 0; i < NUM_PHILOSOPHERS; i++) { pthread_mutex_init(&forks[i], NULL); pthread_cond_init(&cond_vars[i], NULL); ids[i] = i; } for (int i = 0; i < NUM_PHILOSOPHERS; i++) { pthread_create(&threads[i], NULL, philosopher, &ids[i]); } for (int i = 0; i < NUM_PHILOSOPHERS; i++) { pthread_join(threads[i], NULL); } for (int i = 0; i < NUM_PHILOSOPHERS; i++) { pthread_mutex_destroy(&forks[i]); pthread_cond_destroy(&cond_vars[i]); } return 0; } ``` 在这个代码中,每个哲学家是一个线程,通过互斥锁来控制哲学家拿起和放下叉子的操作。当一个哲学家要拿起叉子时,他会先尝试拿起左边的叉子,然后再尝试拿起右边的叉子。如果叉子已经被其他哲学家拿走了,那么这个哲学家就会等待,直到叉子可用。 当一个哲学家拿到两个叉子后,他就可以开始吃饭了。在吃饭的过程中,其他哲学家不能拿走他手上的叉子,因为这会导致死锁。因此,每个哲学家在拿起和放下叉子的时候都要先获得两个叉子的锁,然后再释放锁。 这个代码中的 usleep(1000000) 是为了让哲学家在吃完一顿饭后稍微停顿一下再开始思考。如果没有这个 usleep,那么所有的哲学家都会在很短的时间内吃完三顿饭,这样就不容易观察到死锁的情况了。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那由塔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值