• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 哲學家問題的算法

    發表于:2007-07-04來源:作者:點擊數: 標簽:
    在c/c++論壇里看到有網友貼出來,這里做一下備份,以后有時間嘗試一下能否用信號量與共享內存機制把程序編出來 #define N 5 #define LEFT (i-1)%N #define RIGHT (i+1)%N #define THINKING 0 #define HUNGRY 1 #define EATING 2 int state[N]; void philosopher
    在c/c++論壇里看到有網友貼出來,這里做一下備份,以后有時間嘗試一下能否用信號量與共享內存機制把程序編出來

    #define N 5
    #define LEFT (i-1)%N
    #define RIGHT (i+1)%N
    #define THINKING 0
    #define HUNGRY 1
    #define EATING 2
    int state[N];

    void philosopher(int i)
    {
       while(TRUE)
       {
           think();
           take_forks();
           eat();
           put_forks(i);
       }
    }


    void take_forks(int i)
    {
       down(&mutex);
       state[i]=HUNGRY;
       test(i);
       up(&mutex);
       down(&s[i]);
    }


    void put_forks(int i)
    {
       down(&mutex);
       state[i]=THINKING;
       test(LEFT);
       test(RIGHT);
       up(&mutex);
    }


    void test(i)
    {
       if(state[i]==HUNGRY && state[LEFT]!=EATING && state[RIGHT]!=EATING)
       {
           state[i]=EATING;
           up(&s[i]);
       }
    }


    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>