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

长沙市建设网站平台的公司搜一搜站长工具

长沙市建设网站平台的公司,搜一搜站长工具,免费个人主页网站,内贸b2b平台有哪些死锁检测组件-设想 现在有三个临界资源和三把锁绑定了,三把锁又分别被三个线程占用。(不用关注临界资源,因为锁和临界资源是绑定的) 但现在出现这种情况:线程1去申请获取锁2,线程2申请获取锁3,…

死锁检测组件-设想

现在有三个临界资源和三把锁绑定了,三把锁又分别被三个线程占用。(不用关注临界资源,因为锁和临界资源是绑定的)

20230217203523

但现在出现这种情况:线程1去申请获取锁2,线程2申请获取锁3,线程3申请获取锁1,这样就会造成死锁:

20230215224714

死锁问题,可转换为有向图的环路检测

死锁的构建

有四个线程,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后面

20230216225923

而如何知道线程1申请的锁被线程2占用了呢:设置一个 mutex 和 thread的映射列表(结构体数组),通过mutex返回threadid。

如线程1对mutex1加锁,将(mutex1,threadid1)加入locklist中,表示mutex1被线程1占用了

图的邻接表生成:当前线程根据要申请的mutex a找到占用这把锁的线程id A(通过映射列表),将改线程追加到自己的后面

通过邻接表检测环路:DFS,检测过的标为1,再遇到一个1就表示死锁了

20230216230719

那我们怎么把这个图构建起来: 通过三个原语操作
beforelock、afterlock、afterrunlock 以后要用到再说吧

至此,死锁检测组件设计的大致思路为:通过hook保存muitex和threadid的映射关系,根据这个映射关系生成线程之间的有向图(邻接表),再利用dfs检测图的环路

http://www.ds6.com.cn/news/53217.html

相关文章:

  • 大型网站服务器配置网站seo排名优化工具在线
  • ai做图标教程网站营销伎巧第一季
  • 张掖哪家公司做网站搜索引擎营销案例
  • wordpress 验证码访问关键词seo优化软件
  • 个人网站可以做音乐下载网网站收录工具
  • 如何进行网站的资源建设电商运营推广怎么做
  • 需要郑州网站建设国际财经新闻
  • 闵行区建设管理委员会 网站制作网页设计公司
  • 商城网站制作需要多少费用网络营销五种方法
  • 兄弟连php网站开发百度搜索引擎原理
  • 百度上开个网站怎么做完美日记网络营销策划书
  • 网站流量好难做it培训机构哪个好一点
  • 旅行社网站模板如何优化企业网站
  • wordpress显示分类目录举例说明seo
  • 哈尔滨营销型网站建设公司网站内容管理系统
  • 贵州省住房城乡建设网站搜狗官方网站
  • 哪些网站做的海报比较高大上快速推广
  • 龙岗企业网站设计公司上海seo排名
  • 简洁网站布局哈尔滨百度网站快速优化
  • 河间专业做网站电话什么软件比百度搜索好
  • 网站策划书撰写流程军事新闻头条
  • 现货黄金什么网站可以做直播重庆森林百度云
  • 厦门做外贸网站网络营销推广方式案例
  • 做视频网站弹窗济宁百度推广公司有几家
  • 做软件去哪个网站无锡seo公司哪家好
  • 恩做网站动态页面好源码时代培训机构官网
  • 英文网站建设官网云南网络推广服务
  • 科技网站模板免费下载天津seo培训
  • 哈尔滨 网站建设外国搜索引擎登录入口
  • 网站代码怎么打开淘宝指数官网入口