死锁与死锁检测


一、死锁是什么

比如说以下情况,线程a占用资源1,线程b占用资源2,线程b需要资源1,线程a需要资源2,就会产生死锁
在这里插入图片描述

在死锁的解决方案中,log和gdb调试可以用于线程数量少的情况
线程比较多的情况就不能利用log和gdb去解决
再比如三个线程
在这里插入图片描述

线程A想要线程B的资源,线程B想要线程C的资源,线程C想要线程A的资源,这样就构成了一个有向图的环路
在这里插入图片描述再比如,下图所示,线程A想获取线程B的锁,线程B想获取线程C的锁,线程C想获取线程D的锁,线程D想获取线程A的锁,从而构建了一个资源获取环。
在这里插入图片描述

综上
死锁,是指多个线程或者进程在运行过程中因争夺资源而造成的一种僵局,当进程或者线程处于这种僵持状态,若无外力作用,它们将无法再向前推进。
在这里我们可以把死锁问题,转换成有向图的环路问题

二、死锁的检测

死锁的检测原理
资源获取环可以采用图来存储,使用有向图来存储。线程 A 获取线程 B 已占用的锁,则为线程 A 指向线程 B。如何为线程 B 已占用的锁?运行过程线程 B 获取成功的锁。检测的原理采用另一个线程定时对图进程检测是否有环的存在。
我们可以把死锁的检测问题分为以下的部分
在这里插入图片描述
1、死锁的构建

#define _GNU_SOURCE
#include <dlfcn.h>

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#include <stdlib.h>
#include <stdint.h>
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");
}

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() {
	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);
}

这就是死锁的运行情况,打不出每个线程最后那一条的打印语句,只打出了第一句
在这里插入图片描述

cpu的占用率来看,看不出什么变化,用的mutex的锁是这样的,如果换成spin_lock就会出现cpu跑满的情况
在这里插入图片描述死锁的问题转为有向图的检测
2、问题,如何做到锁被哪个线程占有,如何通过线程关系,构建有向图
我们使用hook,可以看出哪个线程和id绑在一起

#define _GNU_SOURCE//加了这个宏,会把下面那一个头文件的一些功能开启
#include <dlfcn.h>

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#include <stdlib.h>
#include <stdint.h>
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;
//我们实现自己的lock与unlock函数,我们怎么调用库里边的lock函数呢,我们使用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;


int pthread_mutex_lock(pthread_mutex_t *mutex)
{
	printf("pthread_mutex_lock selfid %ld, mutex: %p\n", pthread_self(), mutex);
	pthread_mutex_lock_f(mutex);
}
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
	printf("pthread_mutex_unlock\n");
	pthread_mutex_unlock_f(mutex);
}

static int init_hook() {
	pthread_mutex_lock_f = dlsym(RTLD_NEXT, "pthread_mutex_lock");//调用这个库函数之前,就把这个库函数的指针给劫持下来,赋给另一个函数指针进行调用,就可以解决因为重载库函数而不能使用原来库函数的问题了
	pthread_mutex_unlock_f = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
}
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");
}

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() {
	init_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);
}

我们就可以看到相应的线程id与锁的id,这就是利用hook后的结果
在这里插入图片描述
3、我们来构建有向图
图的判环操作,用dfs

#define _GNU_SOURCE//加了这个宏,会把下面那一个头文件的一些功能开启
#include <dlfcn.h>

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#include <stdlib.h>
#include <stdint.h>


#define MAX		100
enum Type {PROCESS, RESOURCE};
struct source_type {
	uint64 id;
	enum Type type;
	uint64 lock_id;
	int degress;
};

struct vertex {
	struct source_type s;
	struct vertex *next;
};
//
struct task_graph {
	struct vertex list[MAX]; // <selfid , thid>
	int num;
	struct source_type locklist[MAX]; // thid = get_threadid_from_mutex(mutex);锁的id
	int lockidx;
	pthread_mutex_t mutex;
};

struct task_graph *tg = NULL;
int path[MAX+1];
int visited[MAX]; //节点是否被访问
int k = 0;
int deadlock = 0;

struct vertex *create_vertex(struct source_type type) {
	struct vertex *tex = (struct vertex *)malloc(sizeof(struct vertex ));
	tex->s = type;
	tex->next = NULL;
	return tex;
}

int search_vertex(struct source_type type) {
	int i = 0;
	for (i = 0;i < tg->num;i ++) {
		if (tg->list[i].s.type == type.type && tg->list[i].s.id == type.id) {
			return i;
		}
	}
	return -1;
}

void add_vertex(struct source_type type) {
	if (search_vertex(type) == -1) {
		tg->list[tg->num].s = type;
		tg->list[tg->num].next = NULL;
		tg->num ++;
	}
}

int add_edge(struct source_type from, struct source_type to) {
	add_vertex(from);
	add_vertex(to);
	struct vertex *v = &(tg->list[search_vertex(from)]);
	while (v->next != NULL) {
		v = v->next;
	}
	v->next = create_vertex(to);
}


int verify_edge(struct source_type i, struct source_type j) {
	if (tg->num == 0) return 0;
	int idx = search_vertex(i);
	if (idx == -1) {
		return 0;
	}
	struct vertex *v = &(tg->list[idx]);
	while (v != NULL) {
		if (v->s.id == j.id) return 1;
		v = v->next;		
	}
	return 0;
}

int remove_edge(struct source_type from, struct source_type to) {
	int idxi = search_vertex(from);
	int idxj = search_vertex(to);
	if (idxi != -1 && idxj != -1) {
		struct vertex *v = &tg->list[idxi];
		struct vertex *remove;
		while (v->next != NULL) {
			if (v->next->s.id == to.id) {
				remove = v->next;
				v->next = v->next->next;
				free(remove);
				break;
			}
			v = v->next;
		}
	}
}

void print_deadlock(void) {
	int i = 0;
	printf("deadlock : ");
	for (i = 0;i < k-1;i ++) {
		printf("%ld --> ", tg->list[path[i]].s.id);
	}
	printf("%ld\n", tg->list[path[i]].s.id);
}

int DFS(int idx) {
	struct vertex *ver = &tg->list[idx];
	if (visited[idx] == 1) {
		path[k++] = idx;
		print_deadlock();
		deadlock = 1;
		return 0;
	}
	visited[idx] = 1;
	path[k++] = idx;
	while (ver->next != NULL) {
		DFS(search_vertex(ver->next->s));
		k --;
		ver = ver->next;
	}
	return 1;
}

int search_for_cycle(int idx) {//判环操作
	struct vertex *ver = &tg->list[idx];
	visited[idx] = 1;
	k = 0;
	path[k++] = idx;
	while (ver->next != NULL) {
		int i = 0;
		for (i = 0;i < tg->num;i ++) {
			if (i == idx) continue;
			visited[i] = 0;
		}
		for (i = 1;i <= MAX;i ++) {
			path[i] = -1;
		}
		k = 1;
		DFS(search_vertex(ver->next->s));
		ver = ver->next;
	}
}
int main() {
	tg = (struct task_graph*)malloc(sizeof(struct task_graph));
	tg->num = 0;/多少个节点的数量
	
	struct source_type v1;
	v1.id = 1;
	add_vertex(v1);

	struct source_type v2;
	v2.id = 2;
	add_vertex(v2);

	struct source_type v3;
	v3.id = 3;
	add_vertex(v3);

	struct source_type v4;
	v4.id = 4;
	add_vertex(v4);//构造4个节点

	add_edge(v1, v2);
	add_edge(v2, v3);
	add_edge(v1, v3);

	add_edge(v3, v4);
	add_edge(v4, v1);//构造有向边
	search_for_cycle(search_vertex(v1));//从v1节点开始判环
}

建立这样一个图,一共两个环
在这里插入图片描述
这里搜索出来一共两个环
在这里插入图片描述
图的算法我们有了
然后我们咋判断死锁,我们怎么用锁构建一个图`
我们就用beforelock,afterlock,afterunlock三个原语操作,来构建有向图的关系


int search_lock(uint64 mutex) {
	int idx = 0;
	for (idx = 0;idx < tg->lockidx;idx ++) {//遍历锁的总数
		if (mutex == tg->locklist[idx].lock_id) {//锁是否被占用
			return idx;
		}
	}
	return -1;
}

int search_empty_lock() {
	int idx = 0;
	for (idx = 0;idx < tg->lockidx;idx ++) {
		if (0 == tg->locklist[idx].lock_id) {
			return idx;
		}
	}
	return tg->lockidx;
}

void beforelock(uint64 threadid, uint64 mutex) {//第一个参数是线程id,第二个是锁
//这个锁是否被线程占用了,如果占用了,就把锁对应那个线程,就把当前的锁指向当前那个线程指向锁占用的那个线程,就加入一条边
	int idx = 0; //lockidx  sum
	for (idx = 0;idx < tg->lockidx;idx ++) { // 遍历锁的总数
		    //self             thid
		if (mutex == tg->locklist[idx].lock_id) {//锁是否被占用
			//self --> thid
			struct source_type from;
			from.id = threadid;//线程id
			add_vertex(from);//构建一条边
			struct source_type to;
			to.id = tg->locklist[idx].id;
			add_vertex(to);
			if (!verify_edge(from, to)) {//先判断这条边是否存在
				add_edge(from, to);
			}
		}
	}
}
//int inc(*)
void afterlock(uint64 threadid, uint64 mutex) {
//
	int idx = 0;
	if (-1 == (idx = search_lock(mutex))) { //是否加锁
		int emp_idx = search_empty_lock(mutex);
		tg->locklist[emp_idx].id = threadid;
		tg->locklist[emp_idx].lock_id = mutex;
		tg->lockidx ++; //atomic
	} else {//以前加过锁
		struct source_type from;
		from.id = threadid;
		add_vertex(from);
		struct source_type to;
		to.id = tg->locklist[idx].id;
		add_vertex(to);
		if (!verify_edge(from, to)) {
			remove_edge(from, to);//移除这条边
		}
		tg->locklist[idx].id = threadid;
	}
}


void afterunlock(uint64 threadid, uint64 mutex) {//释放所有
	int idx = search_lock(mutex);
	if (tg->locklist[idx].degress == 0) {
		tg->locklist[idx].id = 0;
		tg->locklist[idx].lock_id = 0;
	}
}

三、死锁检测的实现

#define _GNU_SOURCE
#include <dlfcn.h>

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

#include <stdlib.h>
#include <stdint.h>

#include <unistd.h>

#define THREAD_NUM      10

typedef unsigned long int uint64;

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;


#if 1 // graph
#define MAX		100
enum Type {PROCESS, RESOURCE};
struct source_type {
	uint64 id;
	enum Type type;
	uint64 lock_id;
	int degress;
};

struct vertex {
	struct source_type s;
	struct vertex *next;
};
struct task_graph {
	struct vertex list[MAX];
	int num;
	struct source_type locklist[MAX];
	int lockidx;
	pthread_mutex_t mutex;
};

struct task_graph *tg = NULL;
int path[MAX+1];
int visited[MAX];
int k = 0;
int deadlock = 0;

struct vertex *create_vertex(struct source_type type) {
	struct vertex *tex = (struct vertex *)malloc(sizeof(struct vertex ));
	tex->s = type;
	tex->next = NULL;
	return tex;
}
int search_vertex(struct source_type type) {
	int i = 0;
	for (i = 0;i < tg->num;i ++) {
		if (tg->list[i].s.type == type.type && tg->list[i].s.id == type.id) {
			return i;
		}
	}
	return -1;
}
void add_vertex(struct source_type type) {
	if (search_vertex(type) == -1) {
		tg->list[tg->num].s = type;
		tg->list[tg->num].next = NULL;
		tg->num ++;
	}
}

int add_edge(struct source_type from, struct source_type to) {
	add_vertex(from);
	add_vertex(to);
	struct vertex *v = &(tg->list[search_vertex(from)]);
	while (v->next != NULL) {
		v = v->next;
	}
	v->next = create_vertex(to);
}
int verify_edge(struct source_type i, struct source_type j) {
	if (tg->num == 0) return 0;
	int idx = search_vertex(i);
	if (idx == -1) {
		return 0;
	}
	struct vertex *v = &(tg->list[idx]);
	while (v != NULL) {
		if (v->s.id == j.id) return 1;
		v = v->next;
	}
	return 0;
}
int remove_edge(struct source_type from, struct source_type to) {
	int idxi = search_vertex(from);
	int idxj = search_vertex(to);
	if (idxi != -1 && idxj != -1) {
		struct vertex *v = &tg->list[idxi];
		struct vertex *remove;
		while (v->next != NULL) {
			if (v->next->s.id == to.id) {
				remove = v->next;
				v->next = v->next->next;
				free(remove);
				break;
			}
			v = v->next;
		}
	}
}
void print_deadlock(void) {
	int i = 0;
	printf("deadlock : ");
	for (i = 0;i < k-1;i ++) {
		printf("%ld --> ", tg->list[path[i]].s.id);
	}
	printf("%ld\n", tg->list[path[i]].s.id);
}

int DFS(int idx) {

	struct vertex *ver = &tg->list[idx];
	if (visited[idx] == 1) {
		path[k++] = idx;
		print_deadlock();
		deadlock = 1;
		return 0;
	}
	visited[idx] = 1;
	path[k++] = idx;
	while (ver->next != NULL) {
		DFS(search_vertex(ver->next->s));
		k --;	
		ver = ver->next;
	}
	return 1;
}
int search_for_cycle(int idx) {
	struct vertex *ver = &tg->list[idx];
	visited[idx] = 1;
	k = 0;
	path[k++] = idx;
	while (ver->next != NULL) {
		int i = 0;
		for (i = 0;i < tg->num;i ++) {
			if (i == idx) continue;		
			visited[i] = 0;
		}
		for (i = 1;i <= MAX;i ++) {
			path[i] = -1;
		}
		k = 1;
		DFS(search_vertex(ver->next->s));
		ver = ver->next;
	}
}
#endif
void check_dead_lock(void) {
	int i = 0;
	deadlock = 0;
	for (i = 0;i < tg->num;i ++) {
		if (deadlock == 1) break;
		search_for_cycle(i);
	}
	if (deadlock == 0) {
		printf("no deadlock\n");
	}
}
static void *thread_routine(void *args) {
	while (1) {
		sleep(5);
		check_dead_lock();
	}
}
void start_check(void) {
	tg = (struct task_graph*)malloc(sizeof(struct task_graph));
	tg->num = 0;
	tg->lockidx = 0;
	pthread_t tid;
	pthread_create(&tid, NULL, thread_routine, NULL);
}
#if 1
int search_lock(uint64 lock) {
	int i = 0;
	for (i = 0;i < tg->lockidx;i ++) {
		if (tg->locklist[i].lock_id == lock) {
			return i;
		}
	}
	return -1;
}

int search_empty_lock(uint64 lock) {
	int i = 0;
	for (i = 0;i < tg->lockidx;i ++) {
		if (tg->locklist[i].lock_id == 0) {
			return i;
		}
	}
	return tg->lockidx;
}
#endif
int inc(int *value, int add) {
	int old;
	__asm__ volatile(
		"lock;xaddl %2, %1;"
		: "=a"(old)
		: "m"(*value), "a" (add)
		: "cc", "memory"
	);
	return old;
}
void print_locklist(void) {
	int i = 0;
	printf("print_locklist: \n");
	printf("---------------------\n");
	for (i = 0;i < tg->lockidx;i ++) {
		printf("threadid : %ld, lockid: %ld\n", tg->locklist[i].id, tg->locklist[i].lock_id);
	}
	printf("---------------------\n\n\n");
}

void lock_before(uint64 thread_id, uint64 lockaddr) {
	int idx = 0;
	// list<threadid, toThreadid>

	for(idx = 0;idx < tg->lockidx;idx ++) {
		if ((tg->locklist[idx].lock_id == lockaddr)) {

			struct source_type from;
			from.id = thread_id;
			from.type = PROCESS;
			add_vertex(from);

			struct source_type to;
			to.id = tg->locklist[idx].id;
			tg->locklist[idx].degress++;
			to.type = PROCESS;
			add_vertex(to);
			
			if (!verify_edge(from, to)) {
				add_edge(from, to); // 
			}
		}
	}
}

void lock_after(uint64 thread_id, uint64 lockaddr) {

	int idx = 0;
	if (-1 == (idx = search_lock(lockaddr))) {  // lock list opera 
		int eidx = search_empty_lock(lockaddr);
		tg->locklist[eidx].id = thread_id;
		tg->locklist[eidx].lock_id = lockaddr;
		inc(&tg->lockidx, 1);
	} else {
		struct source_type from;
		from.id = thread_id;
		from.type = PROCESS;
		struct source_type to;
		to.id = tg->locklist[idx].id;
		tg->locklist[idx].degress --;
		to.type = PROCESS;
		if (verify_edge(from, to))
			remove_edge(from, to);
		tg->locklist[idx].id = thread_id;
	}
}

void unlock_after(uint64 thread_id, uint64 lockaddr) {
	int idx = search_lock(lockaddr);
	if (tg->locklist[idx].degress == 0) {
		tg->locklist[idx].id = 0;
		tg->locklist[idx].lock_id = 0;
		//inc(&tg->lockidx, -1);
	}	
}

int pthread_mutex_lock(pthread_mutex_t *mutex) {
    pthread_t selfid = pthread_self(); 
	lock_before(selfid, (uint64)mutex);
    pthread_mutex_lock_f(mutex);
	lock_after(selfid, (uint64)mutex);
}

int pthread_mutex_unlock(pthread_mutex_t *mutex) {
	pthread_t selfid = pthread_self();
    pthread_mutex_unlock_f(mutex);
	unlock_after(selfid, (uint64)mutex);
}

static int init_hook() {
    pthread_mutex_lock_f = dlsym(RTLD_NEXT, "pthread_mutex_lock");
    pthread_mutex_unlock_f = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
}

pthread_mutex_t mutex_1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_4 = PTHREAD_MUTEX_INITIALIZER;

void *thread_rountine_1(void *args)
{
	pthread_t selfid = pthread_self(); //
	printf("thread_routine 1 : %ld \n", selfid);
    pthread_mutex_lock(&mutex_1);
    sleep(1);
    pthread_mutex_lock(&mutex_2);
    pthread_mutex_unlock(&mutex_2);
    pthread_mutex_unlock(&mutex_1);
    return (void *)(0);
}

void *thread_rountine_2(void *args)
{
	pthread_t selfid = pthread_self(); //
	printf("thread_routine 2 : %ld \n", selfid);
    pthread_mutex_lock(&mutex_2);
    sleep(1);
    pthread_mutex_lock(&mutex_3);
    pthread_mutex_unlock(&mutex_3);
    pthread_mutex_unlock(&mutex_2);
    return (void *)(0);
}

void *thread_rountine_3(void *args)
{
	pthread_t selfid = pthread_self(); //
	printf("thread_routine 3 : %ld \n", selfid);
    pthread_mutex_lock(&mutex_3);
    sleep(1);
    pthread_mutex_lock(&mutex_4);
    pthread_mutex_unlock(&mutex_4);
    pthread_mutex_unlock(&mutex_3);
    return (void *)(0);
}

void *thread_rountine_4(void *args)
{
	pthread_t selfid = pthread_self(); //
	printf("thread_routine 4 : %ld \n", selfid);
    pthread_mutex_lock(&mutex_4);
    sleep(1);
    pthread_mutex_lock(&mutex_1);
    pthread_mutex_unlock(&mutex_1);
    pthread_mutex_unlock(&mutex_4);
    return (void *)(0);
}

int main()
{
    init_hook();
	start_check();
	printf("start_check\n");
    pthread_t tid1, tid2, tid3, tid4;
    pthread_create(&tid1, NULL, thread_rountine_1, NULL);
    pthread_create(&tid2, NULL, thread_rountine_2, NULL);
    pthread_create(&tid3, NULL, thread_rountine_3, NULL);
    pthread_create(&tid4, NULL, thread_rountine_4, NULL);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    pthread_join(tid4, NULL);

    return 0;
}

运行效果,判断出一个环,则产生了死锁
在这里插入图片描述

启动线程检测的时候,我们如何去预防死锁呢,我们在lock_before这个函数里边,加一条边的时候,就可以去判断是否有环,如果有环则把这条边删去

void lock_before(uint64 thread_id, uint64 lockaddr) {
	int idx = 0;
	// list<threadid, toThreadid>

	for(idx = 0;idx < tg->lockidx;idx ++) {
		if ((tg->locklist[idx].lock_id == lockaddr)) {

			struct source_type from;
			from.id = thread_id;
			from.type = PROCESS;
			add_vertex(from);

			struct source_type to;
			to.id = tg->locklist[idx].id;
			tg->locklist[idx].degress++;
			to.type = PROCESS;
			add_vertex(to);
			
			if (!verify_edge(from, to)) {
				add_edge(from, to); // 
				if(search_for_cycle(idx)){//判断是否有环
					remove_edge(from, to);//如果有环则把这条边删去,就可以预防死锁
				}
			}
		}
	}
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值