zoukankan      html  css  js  c++  java
  • Linux环境下实现哲学家就餐问题

    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    #include <pthread.h>
    #include <errno.h>
    #include <math.h>
    //筷子作为mutex
    pthread_mutex_t chopstick[6] ;//定义以筷子为锁的数组
    void *eat_think(void *arg)
    {
        char phi = *(char *)arg; //将任意类型的指针*arg转化为*char类型
        int left,right; //左右筷子的编号
        switch (phi){
            case 'A':
                left = 5;
                right = 1;
                break;
            case 'B':
                left = 1;
                right = 2;
                break;
            case 'C':
                left = 2;
                right = 3;
                break;
            case 'D':
                left = 3;
                right = 4;
                break;
            case 'E':
                left = 4;
                right = 5;
                break;
        }
    
        int i;
        for(;;){
            usleep(3); //思考,将进程挂起一段时间
            pthread_mutex_lock(&chopstick[left]); //拿起左手的筷子,锁定互斥量,加锁
            printf("Philosopher %c fetches chopstick %d
    ", phi, left);输出哲学家拿起了左手边的筷子
            if (pthread_mutex_trylock(&chopstick[right]) == EBUSY){ //判断右手的筷子是否有人用, 再试一次获得对互斥量的锁定(非阻塞)    
                pthread_mutex_unlock(&chopstick[left]); //如果右边筷子被拿走放下左手的筷子,解锁互斥量,解锁
                continue;//如果此哲学家没能吃饭,验证下一个哲学家是否能吃饭,即跳出本次循环进行下次循环
            }
            
        //    pthread_mutex_lock(&chopstick[right]); //拿起右手的筷子,如果想观察死锁,把上一句if注释掉,再把这一句的注释去掉
            printf("Philosopher %c fetches chopstick %d
    ", phi, right); //输出此哲学家又拿起了右手边的跨子
            printf("Philosopher %c is eating.
    ",phi);//输出此次的哲学家拿起啦一双筷子在吃饭
            usleep(3); //吃饭,把进程挂起一段时间 
            pthread_mutex_unlock(&chopstick[left]); //放下左手的筷子
            printf("Philosopher %c release chopstick %d
    ", phi, left);
            pthread_mutex_unlock(&chopstick[right]); //放下右手的筷子
            printf("Philosopher %c release chopstick %d
    ", phi, right);
    
        }
    }
    int main(){
        pthread_t A,B,C,D,E; //5个哲学家
    
        int i;
        for (i = 0; i < 5; i++)
        pthread_mutex_init(&chopstick[i],NULL);//初始化默认互斥锁属性的互斥锁数组chopstick[i],默认属性为快速互斥锁
        pthread_create(&A,NULL, eat_think, "A");////创建并跳转到线程函数创建并跳转到参数为指向线程标识符的指针为 A 线程函数eat_think,A是运行函数的参数
        pthread_create(&B,NULL, eat_think, "B");
        pthread_create(&C,NULL, eat_think, "C");
        pthread_create(&D,NULL, eat_think, "D");
        pthread_create(&E,NULL, eat_think, "E");
    
        pthread_join(A,NULL);//等待线程标识符为 A 的eat_think线程函数结束
        pthread_join(B,NULL);
        pthread_join(C,NULL);
        pthread_join(D,NULL);
        pthread_join(E,NULL);
        return 0;
    }    
  • 相关阅读:
    jenkins+ant+jmeter实现自动化集成(详解)
    Android ADB命令 adb devices 出现error:protocol fault (no status)
    使用jmeter做接口测试
    估计新版本所需要的案例数量
    《将博客搬至CSDN》
    HDFS NN refreshNodes操作的可用性和效率的改进
    visudo: /etc/sudoers 忙,请稍后重试解决办法
    Java try与finally对返回值的影响
    Java中基本类型和包装类比较的问题
    Java中ArrayList去重方法
  • 原文地址:https://www.cnblogs.com/wenjieyatou/p/5392517.html
Copyright © 2011-2022 走看看