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

建设积分兑换官方网站wordpress会员内容

建设积分兑换官方网站,wordpress会员内容,公司logo效果图,手机网站开发 .net哲学家就餐问题 问题信号量实现发生死锁版限制人数版规定取筷顺序 条件变量实现 问题 在一个圆桌上坐着五位哲学家#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.w-s-a.com/news/778609/

相关文章:

  • 宠物发布网站模板wordpress中文免费电商模板
  • 济南做网站创意服装品牌策划公司
  • 本地电脑做视频网站 外网连接不上软件商城源码
  • 足球直播网站怎么做crm系统介绍
  • 株洲网站建设联系方式东莞凤岗网站制作
  • 小纯洁网站开发如何注册域名
  • 网上做试卷的网站如何把刚做的网站被百度抓取到
  • 滕州网站建wordpress用户中心按钮不弹出
  • 清远新闻最新消息福建seo搜索引擎优化
  • 凡客建站网微信网站怎么做的
  • 网站建设费怎么写会计科目行业网站建设公司
  • 网站里的友情链接网站建设个人简历的网页
  • 佛山自助建站软件湖南seo优化推荐
  • 免费微信微网站模板下载不了优化人员配置
  • wordpress 导航网站主题画流程图的网站
  • 皮卡剧网站怎样做排名网
  • 网站开发 兼职哪个网站是做安全教育
  • 商品展示类网站怎么用群晖nas做网站
  • 长腿蜘蛛wordpresssem优化推广
  • 中国铁路建设监理协会官方网站深圳福田区怎么样
  • 互联网网站开发发展wordpress文章自定义栏目
  • 众筹网站平台建设工信部网站备案系统
  • 网站301重定向代码wordpress 加子目录
  • 淄博网站制作优化推广asp做学生信息网站
  • 海口招商建设有限公司网站淮安哪有专业做网站的公司
  • 喀什哪有做网站的国内正规seo网络推广
  • 网站设计初步规划公司网页打不开是什么原因
  • 深圳企业网站建设推广服务php做的商城网站设计论文
  • 韩雪冬网站手机网站开发 宽度
  • 奉贤专业做网站新手怎么做企业网站