信号量实现哲学家问题(C)

信号量实现哲学家问题©

编号法(打破环路等待条件)

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

#define N 5
sem_t chops[N];


void *dine(void *args){
    int i = (int)args;  //哲学家编号0-4
    while(1){
        //哲学家思考
        //对于偶数编号的哲学家,先拿左边筷子,后拿右边筷子
        if(i % 2 == 0){                    
            sem_wait(&chops[i]);         
            sem_wait(&chops[(i + 1) % N]);   
        }
        //对于奇数编号的哲学家,先拿右边筷子,后拿左边筷子
        else{
            sem_wait(&chops[(i + 1) % N]);
            sem_wait(&chops[i]);
        }
        //哲学家用餐中
        printf("%d 号哲学家用餐中...\n",i);
        //用餐完毕后放下筷子
        sem_post(&chops[i]);
        sem_post(&chops[(i + 1) % N]);
        sleep(1);
    }

}

int main(){
    pthread_t philosopher[N];
    for(int i = 0; i < N; i++){
        pthread_create(&philosopher[i],NULL,dine, (void*)i);
        sem_init(&chops[i],0,1);
    }

    for(int i = 0; i < N; i++){
        pthread_join(philosopher[i],NULL);
    }

    for(int i = 0; i < N; i++){
        sem_destroy(&chops[i]);
    }
}

只允许最多4个哲学家入场,避免死锁

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

#define N 5 //哲学家个数
#define LEFT i    // i 的左边叉子
#define RIGHT (i + 1) % N       // i 的右边叉子

sem_t chop[N];   //每个筷子信号量为1
sem_t room; //只允许最多4个哲学家入场,打破死锁


//功能:要么拿到两把叉子,要么被阻塞起来
void take_chops(int i){
    sem_wait(&chop[LEFT]);
    sem_wait(&chop[RIGHT]);
}

//用餐完毕,通知左邻右舍
void put_chops(int i){
    sem_post(&chop[LEFT]);
    sem_post(&chop[RIGHT]);
}

//哲学家主代码
void process(void *args){
    int i = (int)args;
    while(1){
        sem_wait(&room);
        //think();
        take_chops(i);
        printf("%d 号哲学家用餐中\n",i);
        //eat()
        put_chops(i);
        sem_post(&room);
        sleep(1);
    }
}

int main(){
    pthread_t philosopher[N];
    sem_init(&room,0,4);
    for(int i = 0; i < N; i++){
        pthread_create(&philosopher[i],NULL,process,(void*)i);
        sem_init(&chop[i],0,1);
    }

    for(int i = 0; i < N; i++){
        pthread_join(philosopher[i],NULL);
    }

    sem_destroy(&room);
    for(int i = 0; i < N; i++){
        sem_destroy(&chop[i]);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用信号量解决哲学家进餐问题的 C 语言实现: ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #define N 5 // 哲学家数量 #define LEFT (i + N - 1) % N // 左边的哲学家 #define RIGHT (i + 1) % N // 右边的哲学家 sem_t chopstick[N]; // 餐叉信号量 sem_t mutex; // 互斥信号量 void *philosopher(void *arg) { int i = *((int *) arg); while (1) { // 思考 printf("Philosopher %d is thinking\n", i); sleep(rand() % 5); // 获取两个相邻的餐叉 printf("Philosopher %d is hungry\n", i); sem_wait(&mutex); sem_wait(&chopstick[LEFT]); sem_wait(&chopstick[RIGHT]); sem_post(&mutex); // 就餐 printf("Philosopher %d is eating\n", i); sleep(rand() % 5); // 释放两个餐叉 sem_post(&chopstick[LEFT]); sem_post(&chopstick[RIGHT]); } } int main() { pthread_t tid[N]; int i; // 初始化信号量 sem_init(&mutex, 0, 1); for (i = 0; i < N; i++) { sem_init(&chopstick[i], 0, 1); } // 创建哲学家线程 for (i = 0; i < N; i++) { pthread_create(&tid[i], NULL, philosopher, &i); } // 等待哲学家线程结束 for (i = 0; i < N; i++) { pthread_join(tid[i], NULL); } // 销毁信号量 sem_destroy(&mutex); for (i = 0; i < N; i++) { sem_destroy(&chopstick[i]); } return 0; } ``` 在上面的代码中,我们创建了 N 个哲学家线程,每个哲学家线程都循环执行思考、就餐和释放餐叉的操作。在获取餐叉时,我们使用了互斥信号量 mutex 来保证同一时间只有一个哲学家能够获取餐叉,避免死锁和饥饿等问题。同时,每个哲学家都有两个相邻的餐叉,分别为左边的餐叉和右边的餐叉,我们使用了 chopstick 数组来表示这些餐叉的信号量。在获取餐叉时,先获取互斥信号量 mutex,然后再获取左边和右边的餐叉信号量,这样就能保证每个哲学家都能安全地获取到两个相邻的餐叉,避免死锁和饥饿等问题。在就餐完成后,释放餐叉时,也需要先释放互斥信号量 mutex,然后再释放左边和右边的餐叉信号量。最后,我们在主函数中创建哲学家线程,等待哲学家线程结束,然后销毁信号量

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值