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

深圳的网站建设网站建设网页设计做网站

深圳的网站建设,网站建设网页设计做网站,网站制作中搜索栏怎么做,上海装修公司排名87目录 一、概念 二、以原子的思想解决死锁 三、破环环路的思想解决死锁 四、使用管程来解决死锁 一、概念 问题描述#xff1a; 有五个哲学家#xff0c;他们的生活方式是交替地进行思考和进餐#xff0c;哲学家们共用一张圆桌#xff0c;分别坐在周围的五张椅子上…目录 一、概念 二、以原子的思想解决死锁 三、破环环路的思想解决死锁 四、使用管程来解决死锁 一、概念 问题描述 有五个哲学家他们的生活方式是交替地进行思考和进餐哲学家们共用一张圆桌分别坐在周围的五张椅子上在圆桌上有五个碗和五支筷子平时哲学家进行思考饥饿时便试图取其左、右最靠近他的筷子只有在他拿到两支筷子时才能进餐该哲学家进餐完毕后放下左右两只筷子又继续思考。 约束条件 (1)只有拿到两只筷子时哲学家才能吃饭。 (2)如果筷子已被别人拿走则必须等别人吃完之后才能拿到筷子。 (3)任一哲学家在自己未拿到两只筷子吃完饭前不会放下手中已经拿到的筷子。 筷子是临界资源一段时间只允许一位哲学家使用。为了表示互斥用一个互斥锁表示一只筷子五个互斥锁构成互斥锁数组。 进餐毕先放下他左边的筷子然后再放下右边的筷子。当五个哲学家同时去取他左边的筷子每人拿到一只筷子且不释放即五个哲学家只得无限等待下去引起死锁。 以下代码可能引起死锁 #include iostream #include thread #include mutex #include vector #include chronoconst int NUM_PHILOSOPHERS 5;std::vectorstd::mutex forks(NUM_PHILOSOPHERS);void philosopher(int id) {//1 a 1 a 1 a 1 a 1 aint left_fork id;int right_fork (id 1) % NUM_PHILOSOPHERS;while (true) {// 思考std::cout Philosopher id is thinking. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 尝试拿起筷子std::unique_lockstd::mutex left_lock(forks[left_fork]);std::unique_lockstd::mutex right_lock(forks[right_fork]);// 就餐std::cout Philosopher id is eating. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 放下筷子right_lock.unlock();left_lock.unlock();} }int main() {std::vectorstd::thread philosophers;for (int i 0; i NUM_PHILOSOPHERS; i) {philosophers.emplace_back(philosopher, i);}for (auto ph : philosophers) {ph.join();}return 0; } 二、以原子的思想解决死锁 原子操作指的是一组不可分割的操作这些操作要么全部执行成功要么全部不执行是一个整体不可再分。 若只拿到左筷子没有拿到右筷子则rollback释放所以的左筷子锁。 #include iostream #include thread #include mutex #include vector #include chronoconst int NUM_PHILOSOPHERS 5;std::vectorstd::mutex forks(NUM_PHILOSOPHERS); void philosopher(int id) {int left_fork id;int right_fork (id 1) % NUM_PHILOSOPHERS;while (true) {// 思考std::cout Philosopher id is thinking. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 尝试同时拿起两根筷子while (true) {// 尝试锁定左边的筷子std::unique_lockstd::mutex left_lock(forks[left_fork], std::try_to_lock);if (left_lock.owns_lock()) {// 尝试锁定右边的筷子std::unique_lockstd::mutex right_lock(forks[right_fork], std::try_to_lock);if (right_lock.owns_lock()) {// 成功锁定两根筷子开始就餐std::cout Philosopher id is eating. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 放下筷子自动释放锁break;} else {// 未能锁定右边的筷子释放左边的筷子left_lock.unlock();}}// 等待一段时间后重试std::this_thread::sleep_for(std::chrono::milliseconds(100));}} } int main() {std::vectorstd::thread philosophers;for (int i 0; i NUM_PHILOSOPHERS; i) {philosophers.emplace_back(philosopher, i);}for (auto ph : philosophers) {ph.join();}return 0; } 三、破环环路的思想解决死锁 奇数号哲学家先拿左边的筷子偶数号哲学家先拿右边的筷子可以破坏循环等待的条件从而避免死锁。这种方法的核心思想是打破哲学家之间的对称性使得不会所有哲学家同时持有左边的筷子并等待右边的筷子。 #include iostream #include thread #include mutex #include vector #include chronoconst int NUM_PHILOSOPHERS 5;std::vectorstd::mutex forks(NUM_PHILOSOPHERS); // 每根筷子用一个互斥锁表示void philosopher(int id) {int left_fork id;int right_fork (id 1) % NUM_PHILOSOPHERS;while (true) {// 思考std::cout Philosopher id is thinking. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 奇数号哲学家先拿左边的筷子偶数号哲学家先拿右边的筷子if (id % 2 1) {// 奇数号哲学家std::unique_lockstd::mutex left_lock(forks[left_fork]);std::unique_lockstd::mutex right_lock(forks[right_fork]);// 就餐std::cout Philosopher id is eating. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 放下筷子自动释放锁} else {// 偶数号哲学家std::unique_lockstd::mutex right_lock(forks[right_fork]);std::unique_lockstd::mutex left_lock(forks[left_fork]);// 就餐std::cout Philosopher id is eating. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 放下筷子自动释放锁}} }int main() {std::vectorstd::thread philosophers;// 创建哲学家线程for (int i 0; i NUM_PHILOSOPHERS; i) {philosophers.emplace_back(philosopher, i);}// 等待所有哲学家线程完成实际上永远不会完成for (auto ph : philosophers) {ph.join();}return 0; } 限制同时就餐的哲学家数量破坏环路可以确保至少有一位哲学家能够成功进餐从而避免死锁。这种方法的核心思想是 减少资源竞争确保系统中始终有可用的资源。  #include iostream #include thread #include mutex #include vector #include chrono #include semaphore.hconst int NUM_PHILOSOPHERS 5;std::vectorstd::mutex forks(NUM_PHILOSOPHERS); // 每根筷子用一个互斥锁表示 sem_t table; // 信号量限制同时就餐的哲学家数量void philosopher(int id) {int left_fork id;int right_fork (id 1) % NUM_PHILOSOPHERS;while (true) {// 思考std::cout Philosopher id is thinking. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 尝试进入就餐状态sem_wait(table);// 拿起左边的筷子std::unique_lockstd::mutex left_lock(forks[left_fork]);std::cout Philosopher id picked up left fork left_fork . std::endl;// 拿起右边的筷子std::unique_lockstd::mutex right_lock(forks[right_fork]);std::cout Philosopher id picked up right fork right_fork . std::endl;// 就餐std::cout Philosopher id is eating. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 放下右边的筷子right_lock.unlock();std::cout Philosopher id put down right fork right_fork . std::endl;// 放下左边的筷子left_lock.unlock();std::cout Philosopher id put down left fork left_fork . std::endl;// 离开就餐状态sem_post(table);} }int main() {// 初始化信号量最多允许 4 位哲学家同时就餐sem_init(table, 0, NUM_PHILOSOPHERS - 1);std::vectorstd::thread philosophers;// 创建哲学家线程for (int i 0; i NUM_PHILOSOPHERS; i) {philosophers.emplace_back(philosopher, i);}// 等待所有哲学家线程完成实际上永远不会完成for (auto ph : philosophers) {ph.join();}// 销毁信号量sem_destroy(table);return 0; } 四、使用管程来解决死锁 程是一种将共享资源及其操作封装在一起的同步机制它通过条件变量和互斥锁实现线程间的同步和互斥。 #include iostream #include thread #include mutex #include vector #include chrono #include condition_variableconst int NUM_PHILOSOPHERS 5;class DiningPhilosophers { private:std::vectorstd::mutex forks; // 每根筷子用一个互斥锁表示std::vectorstd::condition_variable conditions; // 每个哲学家的条件变量std::vectorbool isEating; // 记录哲学家是否正在就餐std::mutex monitorMutex; // 管程的互斥锁public:DiningPhilosophers() : forks(NUM_PHILOSOPHERS), conditions(NUM_PHILOSOPHERS), isEating(NUM_PHILOSOPHERS, false) {}// 哲学家请求筷子void requestForks(int id) {std::unique_lockstd::mutex lock(monitorMutex);int left_fork id;int right_fork (id 1) % NUM_PHILOSOPHERS;// 如果左右筷子被占用则等待while (isEating[left_fork] || isEating[right_fork]) {conditions[id].wait(lock);}// 拿起筷子forks[left_fork].lock();forks[right_fork].lock();isEating[id] true;std::cout Philosopher id picked up forks left_fork and right_fork . std::endl;}// 哲学家释放筷子void releaseForks(int id) {std::unique_lockstd::mutex lock(monitorMutex);int left_fork id;int right_fork (id 1) % NUM_PHILOSOPHERS;// 放下筷子forks[left_fork].unlock();forks[right_fork].unlock();isEating[id] false;std::cout Philosopher id put down forks left_fork and right_fork . std::endl;// 通知左右哲学家可以尝试拿筷子conditions[left_fork].notify_all();conditions[right_fork].notify_all();} };void philosopher(int id, DiningPhilosophers dining) {while (true) {// 思考std::cout Philosopher id is thinking. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 请求筷子dining.requestForks(id);// 就餐std::cout Philosopher id is eating. std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(1000));// 释放筷子dining.releaseForks(id);} }int main() {DiningPhilosophers dining;std::vectorstd::thread philosophers;// 创建哲学家线程for (int i 0; i NUM_PHILOSOPHERS; i) {philosophers.emplace_back(philosopher, i, std::ref(dining));}// 等待所有哲学家线程完成实际上永远不会完成for (auto ph : philosophers) {ph.join();}return 0; }
http://www.zqtcl.cn/news/998717/

相关文章:

  • 广州网站建设网页设计贵阳网站建设宏思锐达
  • 洪栾单页网站建设象山县城乡和住房建设局网站
  • 网站留言发送到邮箱潍坊商城网站建设
  • 四川省的住房和城乡建设厅网站首页产品设计是冷门专业吗
  • 北仑建设银行网站网站设计 导航条
  • 如何做网站宣传片单位做网站费用怎么记账
  • 西安网站建设现状购物app开发
  • 2019年做网站还有前景吗手机制作表格教程
  • 校园网站html模板南昌网站建设优化
  • 网站的建立目的来宾网站优化
  • 建设国家游戏网站网站建设规范方案
  • 做网站价位wordpress tag 列表
  • 网站建设 李奥贝纳百度软文推广公司
  • 网站建设流程平台企业微信开发者文档
  • 唐山建设网站的网站青海网站建设企业
  • 北京企业建站系统模板网站建设公司专业网站科技开发
  • 工商注册在哪个网站手机浏览器网站开发
  • 建设电影网站的目的各个国家的google网站
  • centos 网站搭建中国互联网协会调解中心
  • 手机端视频网站模板下载做单页网站需要做什么的
  • 太原网站建设外包中国做乱的小说网站
  • 青海做网站哪家好旅游网站的功能及建设
  • 百度网站优化工具汉川网页设计
  • 网站标签优化怎么做可以看图片的地图什么软件
  • 品牌网站建设9小蝌蚪9wordpress会务网站模版
  • 免费推广网站入口202网页与网站建设
  • 武夷山市网站建设网站标签制作
  • 广州网站定制开发方案河南省新闻发布会直播
  • 普陀网站建设哪家便宜网站建设辶金手指排名十五
  • 网站怎么做百度百科租房网站开发视频教程