固安建设局网站,创建网站怎么赚钱的,网站网站制作服务,黄页88登录死锁检测组件-设想
现在有三个临界资源和三把锁绑定了#xff0c;三把锁又分别被三个线程占用。#xff08;不用关注临界资源#xff0c;因为锁和临界资源是绑定的#xff09; 但现在出现这种情况#xff1a;线程1去申请获取锁2#xff0c;线程2申请获取锁3#xff0c;…死锁检测组件-设想
现在有三个临界资源和三把锁绑定了三把锁又分别被三个线程占用。不用关注临界资源因为锁和临界资源是绑定的 但现在出现这种情况线程1去申请获取锁2线程2申请获取锁3线程3申请获取锁1这样就会造成死锁 死锁问题可转换为有向图的环路检测
死锁的构建
有四个线程4把锁以下代码一定会产生死锁 pthread_mutex_t mtx1 PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mtx2 PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mtx3 PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mtx4 PTHREAD_MUTEX_INITIALIZER;void *thread_routine_a(void *arg) {printf(thread_routine a \n);pthread_mutex_lock(mtx1);sleep(1); pthread_mutex_lock(mtx2);pthread_mutex_unlock(mtx2);pthread_mutex_unlock(mtx1);printf(thread_routine a exit\n);}void *thread_routine_b(void *arg) {printf(thread_routine b \n);pthread_mutex_lock(mtx2);sleep(1);pthread_mutex_lock(mtx3);pthread_mutex_unlock(mtx3);pthread_mutex_unlock(mtx2);printf(thread_routine b exit \n);
// -----pthread_mutex_lock(mtx1);
}void *thread_routine_c(void *arg) {printf(thread_routine c \n);pthread_mutex_lock(mtx3);sleep(1);pthread_mutex_lock(mtx4);pthread_mutex_unlock(mtx4);pthread_mutex_unlock(mtx3);printf(thread_routine c exit \n);
}void *thread_routine_d(void *arg) {printf(thread_routine d \n);pthread_mutex_lock(mtx4);sleep(1);pthread_mutex_lock(mtx1);pthread_mutex_unlock(mtx1);pthread_mutex_unlock(mtx4);printf(thread_routine d exit \n);
}int main() {
#if 1init_hook();pthread_t tid1, tid2, tid3, tid4;pthread_create(tid1, NULL, thread_routine_a, NULL);pthread_create(tid2, NULL, thread_routine_b, NULL);pthread_create(tid3, NULL, thread_routine_c, NULL);pthread_create(tid4, NULL, thread_routine_d, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_join(tid3, NULL);pthread_join(tid4, NULL);
}这里产生了线程a 线程b 线程c 线程d 》 线程a的这样一个环路
但是我们不知道哪把锁被哪个线程占用了没法构建有向图也就无法得知是否产生了这样一个环路
这时可以用hook调自己写的 pthread_mutex_lock将线程和锁的映射关系保存起来
pthread的hook
有点像装饰器模式
// 函数指针
typedef int (*pthread_mutex_lock_t)(pthread_mutex_t *mutex);
pthread_mutex_lock_t pthread_mutex_lock_f;typedef int (*pthread_mutex_unlock_t)(pthread_mutex_t *mutex);
pthread_mutex_unlock_t pthread_mutex_unlock_f;static int init_hook() {// RTLD_NEXT可以理解为代码段在这里面找pthread_mutex_lock函数名把地址返回// 所以pthread_mutex_lock_f就是静态或动态库里的pthread_mutex_lock锁函数 pthread_mutex_lock_f dlsym(RTLD_NEXT, pthread_mutex_lock); pthread_mutex_unlock_f dlsym(RTLD_NEXT, pthread_mutex_unlock);
}int pthread_mutex_lock(pthread_mutex_t *mutex) {printf(pthread_mutex_lock selfid %ld, mutex: %p\n, pthread_self(), mutex);beforelock(pthread_self(), mutex);pthread_mutex_lock_f(mutex); // 用钩子的好处给系统函数命一个别名afterlock(pthread_self(), mutex);}int pthread_mutex_unlock(pthread_mutex_t *mutex) {printf(pthread_mutex_unlock\n);pthread_mutex_unlock_f(mutex);afterunlock(pthread_self(), mutex);}
这样在加锁的时候就能知道锁id(mutex)和线程id的对应关系
图的构建
通过邻接表实现有向图如线程1等待线程2释放锁则将线程id2挂到线程1后面 而如何知道线程1申请的锁被线程2占用了呢设置一个 mutex 和 thread的映射列表结构体数组通过mutex返回threadid。
如线程1对mutex1加锁将mutex1threadid1加入locklist中表示mutex1被线程1占用了
图的邻接表生成当前线程根据要申请的mutex a找到占用这把锁的线程id A通过映射列表将改线程追加到自己的后面
通过邻接表检测环路DFS检测过的标为1再遇到一个1就表示死锁了 那我们怎么把这个图构建起来 通过三个原语操作 beforelock、afterlock、afterrunlock 以后要用到再说吧
至此死锁检测组件设计的大致思路为通过hook保存muitex和threadid的映射关系根据这个映射关系生成线程之间的有向图邻接表再利用dfs检测图的环路