哲学家进餐c++|操作系统

发布于 2021-07-10  27 次阅读


这是哲学家进餐问题的c++程序,这里使用的进餐方式是只有当哲学家左右两边都有可用筷子时才可以进餐,如果只有一只左手筷子放下自己左手筷子。如果对其中函数有不理解的可以看我另一篇博客进程相关函数汇总解释,对照着看,可能方便你理解

#include <iostream> 
#include<Windows.h>
#include<time.h>
using namespace std;                    //命名空间 std 内定义的所有标识符都有效

const unsigned int PHILOSOPHER_NUM = 5; //哲学家数目 
const char THINKING=1;                    /*标记当前哲学家的状态,1 表示等待,2 表示得到饥饿,3 表示正在吃饭*/
const char HUNGRY = 2; 
const char DINING = 3; 

HANDLE hPhilosopher[5];                    //定义数组存放哲学家
HANDLE semaphore[PHILOSOPHER_NUM];        // semaphore 用来表示筷子是否可用 
HANDLE mutex;                            // Mutex 用来控制安全输出
DWORD WINAPI philosopherProc(LPVOID lpParameter) //返回 API 函数 philosopherProc 
{
    int myid;
    char idStr[128]; 
    char stateStr[128];
    char mystate; 
    int ret;
    unsigned int leftFork;  //左筷子
    unsigned int rightFork; //右筷子

    myid = int(lpParameter);
    _itoa(myid, idStr, 10);//把十进制整数转化为字符串并且返回指针
    WaitForSingleObject(mutex, INFINITE);
    cout << "philosopher " << myid << " begin......" << endl;
    ReleaseMutex(mutex);
    mystate = THINKING;     //初始状态为 THINKING
    leftFork = (myid) % PHILOSOPHER_NUM;
    rightFork = (myid + 1) % PHILOSOPHER_NUM;
    while (true)
    {
        switch (mystate)
        {
        case THINKING:
            mystate = HUNGRY;// 改变状态
            strcpy(stateStr, "HUNGRY");
            break;
        case HUNGRY:
            strcpy(stateStr, "HUNGRY");
            ret = WaitForSingleObject(semaphore[leftFork], 0); // 先检查左筷子是否可用
            if (ret == WAIT_OBJECT_0)//有信号返回
            {
                ret = WaitForSingleObject(semaphore[rightFork], 0); // 左筷子可用就拿起,再检查右筷子是否可用 
                if (ret == WAIT_OBJECT_0)
                {
                    mystate = DINING;                               //  右筷子可用,改变自己状态
                    strcpy(stateStr, "DINING");
                }
                else
                {
                    ReleaseSemaphore(semaphore[leftFork], 1, NULL);//如果右筷子不可用,就把左筷子放下
                }
            }
            break;
        case DINING:
            //吃完了把两根筷子放下
            ReleaseSemaphore(semaphore[leftFork], 1, NULL);
            ReleaseSemaphore(semaphore[rightFork], 1, NULL);
            mystate = THINKING;
            strcpy(stateStr, "THINKING");
            break;
        }
        //输出状态
        WaitForSingleObject(mutex, INFINITE);
        cout << "philosopher" << myid << "is:" << stateStr << endl;
        ReleaseMutex(mutex);
        int sleepTime;
        sleepTime = 1 + (int)(5.0 * rand() / (RAND_MAX + 1.0));
        Sleep(sleepTime * 10);
    }
}

int main() 
{
    int i;
    srand(time(0));
    mutex = CreateMutex(NULL, false, NULL);
    for (i = 0; i < PHILOSOPHER_NUM; i++)
    {
        semaphore[i] = CreateSemaphore(NULL, 1, 1, NULL);
        hPhilosopher[i] = CreateThread(NULL, 0, philosopherProc, LPVOID(i), CREATE_SUSPENDED, 0);//CREATE_SUSPENDED创建后挂起的进程
    }
    for (i = 0; i < PHILOSOPHER_NUM; i++)
    {
        ResumeThread(hPhilosopher[i]);
    }

    Sleep(2000); //程序运行时间
    return 0;
}