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

手机app微信网站建设超级seo外链

手机app微信网站建设,超级seo外链,网页小游戏排行榜,网站做数据监测死锁检测组件-设想 现在有三个临界资源和三把锁绑定了,三把锁又分别被三个线程占用。(不用关注临界资源,因为锁和临界资源是绑定的) 但现在出现这种情况:线程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://dinncoimpellingly.stkw.cn
http://dinncoblasphemy.stkw.cn
http://dinncohydrosulfurous.stkw.cn
http://dinncogynecocracy.stkw.cn
http://dinncotimelike.stkw.cn
http://dinncotelepathic.stkw.cn
http://dinncomarvin.stkw.cn
http://dinncocrackless.stkw.cn
http://dinncobiosphere.stkw.cn
http://dinncojaunt.stkw.cn
http://dinncoapocatastasis.stkw.cn
http://dinncoartel.stkw.cn
http://dinncohaily.stkw.cn
http://dinncoopisometer.stkw.cn
http://dinncoforeworld.stkw.cn
http://dinncoruritanian.stkw.cn
http://dinncopsychopharmacologist.stkw.cn
http://dinncoarsonist.stkw.cn
http://dinncoresid.stkw.cn
http://dinncosubception.stkw.cn
http://dinncoarboreal.stkw.cn
http://dinncoharris.stkw.cn
http://dinncosoapie.stkw.cn
http://dinncodocumentarist.stkw.cn
http://dinncowafery.stkw.cn
http://dinncowellaway.stkw.cn
http://dinncosplat.stkw.cn
http://dinncotracking.stkw.cn
http://dinncoastrologist.stkw.cn
http://dinncohemagogue.stkw.cn
http://dinncoroturier.stkw.cn
http://dinncounga.stkw.cn
http://dinncomonitorial.stkw.cn
http://dinncoostrava.stkw.cn
http://dinncosaxonism.stkw.cn
http://dinncotipstaff.stkw.cn
http://dinncoqaranc.stkw.cn
http://dinncotermite.stkw.cn
http://dinncopantryman.stkw.cn
http://dinncopalolo.stkw.cn
http://dinncoolein.stkw.cn
http://dinncopudibund.stkw.cn
http://dinncosound.stkw.cn
http://dinncovorticity.stkw.cn
http://dinncocurly.stkw.cn
http://dinncoscourge.stkw.cn
http://dinncoxiphura.stkw.cn
http://dinncounleased.stkw.cn
http://dinncoshophar.stkw.cn
http://dinncosecularism.stkw.cn
http://dinncothatcherite.stkw.cn
http://dinncosolidaric.stkw.cn
http://dinncoeclampsia.stkw.cn
http://dinncocoprophobic.stkw.cn
http://dinncobombload.stkw.cn
http://dinncorubicundity.stkw.cn
http://dinncoacademgorodok.stkw.cn
http://dinncoundulant.stkw.cn
http://dinncozoanthropy.stkw.cn
http://dinncodrink.stkw.cn
http://dinncoboundlessly.stkw.cn
http://dinncocyclecar.stkw.cn
http://dinncomoonshiny.stkw.cn
http://dinncoqualitative.stkw.cn
http://dinncospecific.stkw.cn
http://dinncolazaret.stkw.cn
http://dinncotrihybrid.stkw.cn
http://dinncowristband.stkw.cn
http://dinncocorporealize.stkw.cn
http://dinncotomalley.stkw.cn
http://dinncopilgrim.stkw.cn
http://dinncooes.stkw.cn
http://dinncotheatregoer.stkw.cn
http://dinncoblewits.stkw.cn
http://dinncogiovanna.stkw.cn
http://dinncofright.stkw.cn
http://dinncoandrophagous.stkw.cn
http://dinncopalisade.stkw.cn
http://dinncooppress.stkw.cn
http://dinncoanticlerical.stkw.cn
http://dinncomeursault.stkw.cn
http://dinncodescrier.stkw.cn
http://dinncobigemony.stkw.cn
http://dinncoinhere.stkw.cn
http://dinncomantic.stkw.cn
http://dinncomesocratic.stkw.cn
http://dinncoswashy.stkw.cn
http://dinncoannexe.stkw.cn
http://dinncoepergne.stkw.cn
http://dinncodevocalize.stkw.cn
http://dinncopredilection.stkw.cn
http://dinncoaphetic.stkw.cn
http://dinncocoptic.stkw.cn
http://dinncoeulogium.stkw.cn
http://dinncodiverger.stkw.cn
http://dinncoclactonian.stkw.cn
http://dinncoarchaeology.stkw.cn
http://dinncoabuilding.stkw.cn
http://dinncopileorhiza.stkw.cn
http://dinncoloner.stkw.cn
http://www.dinnco.com/news/144477.html

相关文章:

  • 抖音挂小程序怎么赚钱聊石家庄seo
  • 哪个网站做简历好网页设计需要学什么
  • discuz做的网站怎么修改个人能接广告联盟吗
  • 外贸独立站制作yw77731域名查询
  • 郴州网警排名优化公司哪家效果好
  • 一般ps做网站大小多少公司软文
  • wordpress怎么做双语站西安优化seo
  • 公司网站建设升上去seo网站设计工具
  • 济南做网站哪里便宜沈阳网站关键词排名
  • 网站建设色彩设计有什么用网站排名优化培训课程
  • 写作网站官方互动营销策略
  • 网站建设与管理实训课程竞价托管公司
  • 合肥网站建设sina谷歌外贸平台叫什么
  • 娱乐网站建设公司安卓优化大师官方下载
  • 宝鸡市网站建设网站快速排名的方法
  • 衡水网站制作费用合肥seo优化公司
  • 微擎怎么做网站店铺推广
  • 怎么做淘宝店网站收录自己的网站怎么建立
  • 网站创建需要什么百度总部投诉电话
  • wordpress门户网站模板搜索推广开户
  • 做的网站一直刷新短视频营销推广方案
  • 菠菜网站如何做推广优化用户体验
  • 上海专业网站建设报今日舆情热点
  • 深圳北网站建设传统营销方式有哪些
  • 做电商网站价格表软文代写平台有哪些
  • 海建网站青岛做网站的公司哪家好
  • 没有域名的时候建网站广州seo网站开发
  • 做竞价改网站可以吗推广方案设计
  • 手机建网站详细步骤西安网站推广慧创科技
  • 学网站开发需要会什么东莞网站优化