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

商城网站定制怎么做扁平化中文网站模板下载

商城网站定制怎么做,扁平化中文网站模板下载,女装商城网站建设,微盟是做什么的哲学家就餐问题 问题信号量实现发生死锁版限制人数版规定取筷顺序 条件变量实现 问题 在一个圆桌上坐着五位哲学家,每个哲学家面前有一个碗装有米饭的碗和一个筷子。哲学家的生活包括思考和进餐两个活动。当一个哲学家思考时,他不需要任何资源。当他饿了…

哲学家就餐问题

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

问题

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

信号量实现

发生死锁版

#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.yayakq.cn/news/120054/

相关文章:

  • 做汽车保养的网站湛江建设培训学校网站
  • 网站上传图片不成功关于苏宁易购网站建设的不足之处
  • asp 网站卡死个人网站如何在百度上做推广
  • 做网站需要公司东莞常平怎么样
  • 网站托管的好处有趣的网站设计
  • 图片网站制作建设银行app忘记登录密码
  • 广东省建网站公司深圳网站建设推荐
  • 专门做拼团的网站深圳正规装修公司
  • 吉林省建设厅网站专家梅陇做网站
  • 雅安市建设工程招投标网站四川建设网站项目招标
  • 宁波做公司网站公司中国小康建设网站
  • 做网站费用分摊入什么科目30天网站建设实录下载
  • 西安做网站的公司在哪开发企业网站的公司
  • 怀化建设网站合肥整站推广
  • 如何在电脑上打开自己做的网站延庆网站建设
  • 微信上浏览自己做的网站吗小红书推广方法
  • 安什么网站建设建站公司网站模板
  • 西樵网站设计网站建设策划模板下载
  • discuz网站编码萧山网站建设争锋网络
  • vps如果制作论坛网站哪家企业网站做的好
  • 宁波高端网站建设联系方式wordpress php5.5
  • 海南网站建设哪里好修改wordpress入口地址
  • 江西省网站开发企业电子商务网站开发
  • 国际网站平台有哪些公众平台微信登录
  • 电子商务网站html模板怎样做网络推广效果好视频
  • 服务周到的做网站大型网站建设建站模板
  • 哪些做网站的公司比较好济南的互联网公司
  • 哈尔滨专业建站免费咨询wordpress怎么弄登录
  • 金阊网站建设自己注册了个域名想做一个网站
  • 网站开发技术员网站建设部署与发布试题