哲学家就餐问题
来源:互联网 发布:最近上映的网络电视剧 编辑:程序博客网 时间: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
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题 锁
- 哲学家就餐问题
- 哲学家就餐问题
- 关于哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 哲学家就餐问题
- 多线程--哲学家就餐问题
- 使用Spring Boot快速构建应用
- BerkelyDB学习笔记
- 就看到了就是看了就看了了看
- Brief算法分析(草稿)
- php上传图片到数据库与网页无法显示的问题
- 哲学家就餐问题
- Android SDK Android NDK Android Studio 官方下载地址
- Ubuntu下Android開發環境之二(JDK/Android SDK安裝)
- 猜数游戏
- Java注释(Annotation)详解
- 号房间卡刷卡费就好撒就看好就看
- 克拉还是看了哈时间看好就看
- C/C++堆栈指引(X86)
- 好的金卡和金卡和金卡好就看好看