深圳的网站建设,网站建设网页设计做网站,网站制作中搜索栏怎么做,上海装修公司排名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;
}