哲学家就餐问题

来源:互联网 发布:最近上映的网络电视剧 编辑:程序博客网 时间:2024/03/29 01:32

有5个哲学家同坐一桌用餐,但只有5只筷子均分于每个哲学家两侧,要求哲学家只能使用在自己座位两侧的筷子,编写程序不能让任何一位哲学家饿死。

(互斥锁实现)

#include <iostream>#include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <time.h>using namespace std;#define N 5int man[5] = { 0 };int food[1024] = { 0 };int n = 5;pthread_mutex_t mutex[5];pthread_t tid[5];void *thread_do(void *arg){    int *p = (int *)arg;    cout << "*p = " << *p << endl;    int count = 0;    int time = 0;    while(time < 100)    {        time++;        //cout << *p << "  count = " << count << endl;        //usleep(10000);        if ((pthread_mutex_trylock(mutex + *p)) == 0)        {            //count = 0;            //cout << *p << "  1111111111111111111111111111" << endl;            int cnt = 0;            if(*p == 0)//shou wei xiang jiao            {                while(1)                {                    if((pthread_mutex_trylock(mutex + N-1)) == 0)                    {                        //cout << *p << "  22222222222222222222222222" << endl;                        printf("*p = %d, using %d and %d dinner\n", *p, *p, N-1);                        man[*p]++;                        usleep(100000);                        //sleep(rand()%5);                        pthread_mutex_unlock(mutex + N-1);                        usleep(100000);                        //sleep(rand()%5);                        break;                    }                    //cout << *p << "  111111111222222222" << endl;                    cnt++;                    //sleep(rand()%5);                    usleep(10000);                    if(cnt >= 3)                    {                        //pthread_mutex_unlock(mutex + *p);                        //sleep(1);                        //sleep(rand()%5);                        break;                    }                }                pthread_mutex_unlock(mutex + *p);                cnt = 0;                usleep(10000);                //sleep(rand()%5);            }            else            {                while(1)                {                    if((pthread_mutex_trylock(mutex + *p-1)) == 0)                    {                        //cout << *p << "  22222222222222222222222222" << endl;                        printf("*p = %d, using %d and %d dinner\n", *p, *p, *p-1);                        man[*p]++;                        usleep(100000);                        //sleep(rand()%5);                        pthread_mutex_unlock(mutex + *p-1);                        usleep(100000);                        //sleep(rand()%5);                        break;                    }                    //cout << *p << "  111111111222222222" << endl;                    cnt++;                    usleep(10000);                    //sleep(rand()%5);                    if(cnt >= 3)                    {                        //pthread_mutex_unlock(mutex + *p);                        //sleep(1);                        //sleep(rand()%5);                        break;                    }                }                cnt = 0;                pthread_mutex_unlock(mutex + *p);                //sleep(rand()%5);                usleep(10000);            }        }        //else        {            count++;            usleep(10000);            //sleep(rand()%5);            if(count >= 5)            {                count = 0;                usleep(100000);                //sleep(rand()%5);            //  while()            }        }    }    printf("man[%d] : %d\n", *p, man[*p]);    return NULL;}int main(void){    int i = 0;    srand(time(NULL));    for(i = 0; i < 10; i++)        printf("Random number #%d: %d\n", i, rand()%5);    i = 0;    //sleep(5);    while(i < n)    {        pthread_mutex_init(mutex + i, NULL);        i++;    }    i = 0;    int num[N] = {0, 1, 2, 3, 4};    while(i < n)    {        pthread_create(tid + i, NULL, &thread_do, (void *)&num[i]);        i++;    }    i = 0;    cout << "execute finish" << endl;    while(i < n)    {        pthread_join(tid[i], NULL);        i++;    }    cout << "recycle finish" << endl;    i = 0;    while(i < n)    {        cout << "in destroy" << endl;        pthread_mutex_destroy(mutex + i);        i++;    }    cout << "destroy finish" << endl;    i = 0;    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~"  << endl;    while(i < n)    {        printf("man[%d] : %d\n", i, man[i]);        i++;    }    return 0;}


0 0
原创粉丝点击