哲学家吃饭问题 代码实现如何 避免线程死锁

问题描述:
    假设有5个哲学家,他们花费一生中的时光思考和吃饭。这些哲学家共用一个圆桌,每个哲学家都有一把椅子。在桌子中央是一碗通心面,在桌子上放着5只筷子。当一个哲学家思考时,他与其他同事不交互。时而,哲学家会感到饥饿,并试图拿起与他相近的两只筷子(他与邻近左、右之间的筷子)。一只筷子一次只能被一个哲学家拿起。显然,他不能从其他哲学家手里拿走筷子。当一个饥饿的哲学家同时有两只筷子时,他就可以吃饭。当吃完后,他会放下两只筷子,并再次开始思考。

代码实现:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <pthread.h>

/** 线程的处理函数,会有死锁 */
void* ReadyEatDeadLock(void* arg);

/** 线程的处理函数,不会有死锁 */
void* ReadyEat(void* arg);


/** 线程锁 ,定义6把锁*/
pthread_mutex_t mutix[6] ;

/** 线程的处理函数,不会有死锁 */
void* ReadyEat(void* arg)
{
    int nLeft = 0,nRight = 0;
    int nArg = (int)arg;
    //printf("%d\n",nArg);
    switch (nArg)
    {
        case 1: 
        {
            nLeft = 5;
            nRight = 1;
            break;
        }
        case 2: 
        {
            nLeft = 1;
            nRight = 2;
            break;
        }
        case 3: 
        {
            nLeft = 2;
            nRight = 3;
            break;
        }
        case 4: 
        {
            nLeft = 3;
            nRight = 4;
            break;
        }
        case 5: 
        {
            nLeft = 4;
            nRight = 5;
            break;
        }
        default:
        {
            printf("The parameter is wrong\n");
            break;
        }
    }
    while(1)
    {
        /** 思考一秒钟 */
        sleep(1);

        /** 拿起左边的筷子,如果正被锁住则阻塞 */
        pthread_mutex_lock(&mutix[nLeft]);

        /** 如果右手边的筷子被上锁 */
        if(EBUSY == pthread_mutex_trylock(&mutix[nRight]))
        {
            /** 放下左手上的筷子 */
            pthread_mutex_unlock(&mutix[nLeft]);
            continue;
        }

        printf("The %d man is eating  1 秒钟\n",nArg);
        sleep(1);

        /** 放下左手上的筷子 */
        pthread_mutex_unlock(&mutix[nLeft]);
        /** 放下右手上的筷子 */
        pthread_mutex_unlock(&mutix[nRight]);
    }
}

int main()
{
    pthread_t tid[6];
    int nIndex = 0;
    int nRet = 0;
    char szPthreadFun[10] = {"ReadyEat"};


    /** 初始化线程锁 */
    for(nIndex = 1; nIndex < 6; nIndex++)
    {
        pthread_mutex_init(&mutix[nIndex],NULL);
    }

    /** 创建5个线程*/
    for(nIndex = 1; nIndex < 6; nIndex++)
    {
        nRet = pthread_create(&tid[nIndex],NULL,ReadyEatDeadLock,(void*)nIndex);
        if(0 != nRet)
        {
            printf("create %d pthread failed\n",nIndex);
            return -1;
        }
    }

    while(1)
    {
        /** 进程不退出,确保线程正在运行 */
    }
    return 0;
}


/** 线程的处理函数,会有死锁 */
void* ReadyEatDeadLock(void* arg)
{
    int nLeft = 0,nRight = 0;
    int nArg = (int)arg;
    //printf("%d\n",nArg);
    switch (nArg)
    {
        case 1: 
        {
            nLeft = 5;
            nRight = 1;
            break;
        }
        case 2: 
        {
            nLeft = 1;
            nRight = 2;
            break;
        }
        case 3: 
        {
            nLeft = 2;
            nRight = 3;
            break;
        }
        case 4: 
        {
            nLeft = 3;
            nRight = 4;
            break;
        }
        case 5: 
        {
            nLeft = 4;
            nRight = 5;
            break;
        }
        default:
        {
            printf("The parameter is wrong\n");
            break;
        }
    }
    while(1)
    {
        /** 思考一秒钟 */
        usleep(1);

        /** 拿起左边的筷子,如果正被锁住则阻塞 */
        pthread_mutex_lock(&mutix[nLeft]);

        /** 拿起右边的筷子,如果正被锁住则阻塞 */
        pthread_mutex_lock(&mutix[nRight]);

        printf("The %d man is eating  1 秒钟\n",nArg);
        usleep(1);

        /** 放下左手上的筷子 */
        pthread_mutex_unlock(&mutix[nLeft]);
        /** 放下右手上的筷子 */
        pthread_mutex_unlock(&mutix[nRight]);
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dmfrm

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

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

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

打赏作者

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

抵扣说明:

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

余额充值