zoukankan      html  css  js  c++  java
  • Linux下模拟哲学家就餐,提供死锁和非死锁解法

    /**
     * every philosopher is in while loop:
     * thinking -> take_forks -> eating -> put_down_forks -> thingking
     * 死锁:修改N_ROOM为5,开启take_forks(int id)函数中的sleep(5);
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <unistd.h>
    
    #define N 5 // five philosopher
    #define T_EAT 5
    #define T_THINK 5
    #define N_ROOM  4  //同一时间只允许X人用餐
    #define left(phi_id) (phi_id+N-1)%N
    #define right(phi_id) (phi_id+1)%N
    
    enum { think , hungry , eat  }phi_state[N];
    sem_t chopstick[N];
    sem_t room;
    
    void thinking(int id){
        sleep(T_THINK);
        printf("philosopher[%d] is thinking....
    ", id);
    }
    
    void eating(int id){
        sleep(T_EAT);
        printf("philosopher[%d] is eating....
    ", id);
    }
    
    void take_forks(int id){
        //获取左右两边的筷子
        printf("philosopher[%d] is trying to get left[%d] chopstick.???????
    ", id, left(id));
        sem_wait(&chopstick[left(id)]);
        printf("philosopher[%d]  takes left[%d] chopstick.------
    ", id, left(id));
        //sleep(5);	//增加死锁几率
        printf("philosopher[%d] is trying to get right[%d] chopstick.??????
    ", id, id);
        sem_wait(&chopstick[id]);
        printf("philosopher[%d]  takes right[%d] chopstick.-----
    ", id, id);
    }
    
    void put_down_forks(int id){
        printf("philosopher[%d] put_down_forks...vvvvvv
    ", id);
        sem_post(&chopstick[left(id)]);
        sem_post(&chopstick[id]);
    }
    
    void* philosopher_work(void *arg){
        int id = *(int*)arg;
        printf("philosopher init [%d] 
    ", id);
        while(1){
            thinking(id);
            sem_wait(&room);
            take_forks(id);
            sem_post(&room);
            eating(id);
            put_down_forks(id);
        }
    }
    
    int main(){
        pthread_t phiTid[N];
        int i;
        int err;
        int *id=(int *)malloc(sizeof(int)*N);
    
        //initilize semaphore
        for (i = 0; i < N; i++)
        {
            if(sem_init(&chopstick[i], 0, 1) != 0)
            {
                printf("init forks error
    ");
            }
        }
    
        sem_init(&room, 0, N_ROOM);
    
        for(i=0; i < N; ++i){
            id[i] = i;
            err = pthread_create(&phiTid[i], NULL, philosopher_work, (void*)(&id[i])); //这种情况生成的thread id是0,1,2,3,4
            if (err != 0)
                printf("can't create process for reader
    ");
        }
        while(1);
        // delete the source of semaphore
        for (i = 0; i < N; i++)
        {
            err = sem_destroy(&chopstick[i]);
            if (err != 0)
            {
                printf("can't destory semaphore
    ");
            }
        }
        exit(0);
        return 0;
    }
    
  • 相关阅读:
    JavaScript 基本类型值-Undefined、Null、Boolean
    Git学习之路(6)- 分支操作
    Git学习之路(5)- 同步到远程仓库及多人协作问题
    setTimeout小总结
    Git学习之路(4)- 撤销操作、删除文件和恢复文件
    Git学习之路(3)-提交文件到三个区
    Git学习之路(2)-安装GIt和创建版本库
    Git学习之路(1)-Git简介
    两种常见挂载Jenkins slave节点的方法
    rabbitmq集群节点操作
  • 原文地址:https://www.cnblogs.com/CSE-kun/p/14020482.html
Copyright © 2011-2022 走看看