当前位置: 首页 > news >正文

做网站建设月收入多少在线咨询 1 网站宣传

做网站建设月收入多少,在线咨询 1 网站宣传,markdowm wordpress,深圳市网站制作公司哲学家就餐问题 问题信号量实现发生死锁版限制人数版规定取筷顺序 条件变量实现 问题 在一个圆桌上坐着五位哲学家,每个哲学家面前有一个碗装有米饭的碗和一个筷子。哲学家的生活包括思考和进餐两个活动。当一个哲学家思考时,他不需要任何资源。当他饿了…

哲学家就餐问题

  • 问题
  • 信号量实现
    • 发生死锁版
    • 限制人数版
    • 规定取筷顺序
  • 条件变量实现

问题

在一个圆桌上坐着五位哲学家,每个哲学家面前有一个碗装有米饭的碗和一个筷子。哲学家的生活包括思考和进餐两个活动。当一个哲学家思考时,他不需要任何资源。当他饿了时,他试图拿起两只相邻的筷子来吃饭。由于碗和筷子不能被共享,因此必须找到一种方法来确保哲学家能够安全地进餐,即不会发生死锁(所有哲学家都在等待筷子)也不会发生饥饿(某些哲学家永远无法拿起筷子)

信号量实现

发生死锁版

#include <pthread.h>
#include <semaphore.h>
#include <vector>
#include <unistd.h>
#include <iostream>#define N 5
#define P(x) sem_wait(x)
#define V(x) sem_post(x)sem_t mutex_table;
std::vector<sem_t> mutex_chopsticks(N);void* T_philosopher(void* arg) {int id = *((int*)arg);// id哲学家吃饭需要的2根筷子int lhs = (id + N - 1) % N;int rhs = id % N;while (true) {P(&mutex_chopsticks[lhs]);printf("+ %d by T%d\n", lhs, id);P(&mutex_chopsticks[rhs]);printf("+ %d by T%d\n", rhs, id);// Eat.// Philosophers are allowed to eat in parallel.printf("- %d by T%d\n", lhs, id);printf("- %d by T%d\n", rhs, id);V(&mutex_chopsticks[lhs]);V(&mutex_chopsticks[rhs]);sleep(0.5);}
}int main() {for (int i = 0; i < N; i++) {sem_init(&mutex_chopsticks[i], 0, 1);}std::vector<pthread_t> threads(N);std::vector<int> ids(N);for (int i = 0; i < N; i++) {ids[i] = i + 1;pthread_create(&threads[i], nullptr, &T_philosopher, &ids[i]);}for (int i = 0; i < N; i++) {pthread_join(threads[i], nullptr);}
}

限制人数版

限制最多只能4人同时上桌, 则可以保证至少有一个人可以同时拿起两根筷子

#include <pthread.h>
#include <semaphore.h>
#include <vector>
#include <unistd.h>
#include <iostream>#define P(x) sem_wait(x)
#define V(x) sem_post(x)const int N = 5;sem_t mutex_table;
std::vector<sem_t> mutex_chopsticks(N);void* T_philosopher(void* arg) {int id = *((int*)arg);// id哲学家吃饭需要的2根筷子int lhs = (id + N - 1) % N;int rhs = id % N;while (true) {// Come to mutex_tableP(&mutex_table);P(&mutex_chopsticks[lhs]);printf("+ %d by T%d\n", lhs, id);P(&mutex_chopsticks[rhs]);printf("+ %d by T%d\n", rhs, id);// Eating// Philosophers are allowed to eat in parallel.printf("- %d by T%d\n", lhs, id);printf("- %d by T%d\n", rhs, id);V(&mutex_chopsticks[lhs]);V(&mutex_chopsticks[rhs]);// Leave mutex_tableV(&mutex_table);// Thinkingsleep(0.5);}
}int main() {// 保证任何实际最多只有4个人在桌子上// 这样至少有1个人可以拿到2根筷子sem_init(&mutex_table, 0, N - 1);for (int i = 0; i < N; i++) {sem_init(&mutex_chopsticks[i], 0, 1);}std::vector<pthread_t> threads(N);std::vector<int> ids(N);for (int i = 0; i < N; i++) {ids[i] = i + 1;pthread_create(&threads[i], nullptr, &T_philosopher, &ids[i]);}for (int i = 0; i < N; i++) {pthread_join(threads[i], nullptr);}
}

规定取筷顺序

规定每个人, 先拿编号小的筷子, 再拿编号大的筷子

#include <pthread.h>
#include <semaphore.h>
#include <vector>
#include <unistd.h>
#include <iostream>#define P(x) sem_wait(x)
#define V(x) sem_post(x)const int N = 5;sem_t mutex_table;
std::vector<sem_t> mutex_chopsticks(N);void* T_philosopher(void* arg) {int id = *((int*)arg);// id哲学家吃饭需要的2根筷子int lhs = (id + N - 1) % N;int rhs = id % N;while (true) {// 规定每个人, 先拿编号小的筷子, 再拿编号大的筷子if (lhs < rhs) {P(&mutex_chopsticks[lhs]);P(&mutex_chopsticks[rhs]);} else {P(&mutex_chopsticks[rhs]);P(&mutex_chopsticks[lhs]);}printf("+ %d by T%d\n", lhs, id);printf("+ %d by T%d\n", rhs, id);// Eatingprintf("- %d by T%d\n", lhs, id);printf("- %d by T%d\n", rhs, id);V(&mutex_chopsticks[lhs]);V(&mutex_chopsticks[rhs]);// Thinkingsleep(0.5);}
}int main() {for (int i = 0; i < N; i++) {sem_init(&mutex_chopsticks[i], 0, 1);}std::vector<pthread_t> threads(N);std::vector<int> ids(N);for (int i = 0; i < N; i++) {ids[i] = i + 1;pthread_create(&threads[i], nullptr, &T_philosopher, &ids[i]);}for (int i = 0; i < N; i++) {pthread_join(threads[i], nullptr);}
}

条件变量实现

#include <pthread.h>
#include <vector>
#include <unistd.h>
#include <iostream>#define Lock(x) pthread_mutex_lock(x)
#define UnLock(x) pthread_mutex_unlock(x)const int N = 5;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
std::vector<int> available(N, true);void* T_philosopher(void* arg) {int id = *((int*)arg);// id哲学家吃饭需要的2根筷子int lhs = (id + N - 1) % N;int rhs = id % N;while (true) {// Come to mutex_tableLock(&mutex);while (!(available[lhs] && available[rhs])) {pthread_cond_wait(&cond, &mutex);}printf("+ %d by T%d\n", lhs, id);printf("+ %d by T%d\n", rhs, id);available[lhs] = available[rhs] = false;UnLock(&mutex);// Eatingsleep(0.5);printf("- %d by T%d\n", lhs, id);printf("- %d by T%d\n", rhs, id);available[lhs] = available[rhs] = true;pthread_cond_broadcast(&cond);// Thinkingsleep(0.5);}
}int main() {std::vector<pthread_t> threads(N);std::vector<int> ids(N);for (int i = 0; i < N; i++) {ids[i] = i + 1;pthread_create(&threads[i], nullptr, &T_philosopher, &ids[i]);}for (int i = 0; i < N; i++) {pthread_join(threads[i], nullptr);}
}
http://www.yidumall.com/news/71803.html

相关文章:

  • 做网站怎样做深圳新闻最新事件
  • 电视台网站建设方案.doc中国国家培训网
  • 怎么用服务器做网站中国50强企业管理培训机构
  • 顺企网我做网站百度贴吧免费发布信息
  • 无锡市住房和城乡建设部网站推广引流吸引人的标题
  • 乡镇做电器网站能不能营运企业查询信息平台
  • 怎样用ps做网站首页图片搜索引擎营销概念
  • 找网站建设公司哪家最好seo关键词排名优化怎么收费
  • 做纸巾定制的网站阿里巴巴推广
  • 佛山网站建设是哪个搜索引擎的优化方法有哪些
  • 网站制作杭州百度首页排名优化服务
  • wordpress支持西安seo外包平台
  • 南京网站开发南京乐识正规seo每日工作
  • 做网站服务销售百度手机助手最新版下载
  • 搭建安装wordpress教程 pdf seo won
  • 网站怎么防黑客网站推广排名优化
  • 大连建设学校网站超级外链推广
  • 开不锈钢公司怎么做网站搜外友链平台
  • vps如何创建网站seo工资待遇 seo工资多少
  • 果洛州公司网站建设推广app赚钱
  • 机械厂网站建设sem分析是什么
  • 做爰全过程免费的视频 网站百度官方网站入口
  • 网站开发的学习国家认可的教育培训机构
  • 赣州疫情最新消息今天又封了seo服务靠谱吗
  • 自建门户网站谷歌google中文登录入口
  • 网站建设的方法株洲网络推广
  • 网站建设 化工sem代运营
  • 信阳做网站公司汉狮价格百度关键词分析
  • 网站建设公司的问答营销案例百度词条
  • 计算机做网站难吗seo优化排名百度教程