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

宁波哪里做网站的网站系统功能描述

宁波哪里做网站的,网站系统功能描述,做网站走啥科目,惠州网站建设方案外包哲学家就餐问题 问题信号量实现发生死锁版限制人数版规定取筷顺序 条件变量实现 问题 在一个圆桌上坐着五位哲学家#xff0c;每个哲学家面前有一个碗装有米饭的碗和一个筷子。哲学家的生活包括思考和进餐两个活动。当一个哲学家思考时#xff0c;他不需要任何资源。当他饿了… 哲学家就餐问题 问题信号量实现发生死锁版限制人数版规定取筷顺序 条件变量实现 问题 在一个圆桌上坐着五位哲学家每个哲学家面前有一个碗装有米饭的碗和一个筷子。哲学家的生活包括思考和进餐两个活动。当一个哲学家思考时他不需要任何资源。当他饿了时他试图拿起两只相邻的筷子来吃饭。由于碗和筷子不能被共享因此必须找到一种方法来确保哲学家能够安全地进餐即不会发生死锁所有哲学家都在等待筷子也不会发生饥饿某些哲学家永远无法拿起筷子 信号量实现 发生死锁版 #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::vectorsem_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::vectorpthread_t threads(N);std::vectorint 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::vectorsem_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::vectorpthread_t threads(N);std::vectorint 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::vectorsem_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::vectorpthread_t threads(N);std::vectorint 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::vectorint 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::vectorpthread_t threads(N);std::vectorint 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.dnsts.com.cn/news/61612.html

相关文章:

  • 网站制作详细流程qq空间关闭申请网站
  • 北京网站设计服务app手机软件开发
  • 网站不能添加图片大学网站栏目建设通知
  • icp备案网站管理员有负责吗网址生成短链接
  • 网站建设与优化推广方案模板希爱力
  • 找做金融的网站闵行郑州阳网站建设
  • 无锡网站制作哪家便宜个人域名备案流程详细
  • 12380网站建设打算爱站网查询
  • 百度右边相关网站网站运营需要哪些人员
  • 网站的链接建设wordpress 空行
  • 江门专业网站建设价格个人注册公司的步骤
  • 怎么确定电商网站建设的目标做网站不赚钱了
  • 广西腾达建设集团有限公司网站现代网络营销的方式
  • 上海个人建站模板wordpress 招聘模块
  • 寻找锦州网站建设百度做网站的费用
  • 重庆网站推广运营深圳给企业做网站
  • 网站建设公司的客户cpa个人网站怎么做
  • 绍兴柯桥区城乡建设局网站网站关键词 公司
  • 查询建设规范的网站餐饮vi设计公司
  • 360做网站多少钱一年申请网站网站
  • 图片生成链接的网站安监局特种作业证全国联网
  • 小企业网站建设价格网页制作与网站建设实战大全
  • 网站开发 云智互联自学做网站的书
  • 512m内存做网站邵阳网站开发公司推荐
  • 企业网站的建设报价免费ppt模板下载 知乎
  • 课外辅导东莞网站建设技术支持全案品牌设计公司
  • 做了网站应该如何推广青岛最新疫苗接种
  • 中文企业网站html模板做网站发布
  • PHP开源网站开发系统wordpress 内容置顶
  • 网站建设评价指标网站开发好后版权归谁