日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Linux基礎組件之死鎖檢測

作者:Long_xu 更新時間: 2022-09-25 編程語言

死鎖檢測

  • 死鎖產生原因
  • 構建一個死鎖
  • 使用hook檢測死鎖
    • dlsym()函數
    • pthread_self()函數
    • hook使用場景
    • 使用步驟
    • 示例代碼
  • 使用圖算法檢測死鎖
    • 圖的構建
    • 圖的使用
    • 示例代碼
  • 總結
  • 后言

死鎖產生原因

死鎖,是指多個線程或者進程在運行過程中因爭奪資源而造成的一種僵局,當進程或者線程處于這種僵持狀態,若無外力作用,它們將無法再向前推進。
如下圖所示,線程 A 想獲取線程 B 的鎖,線程 B 想獲取線程 C 的鎖,線程 C 想獲取線程 D 的鎖,線程 D 想獲取線程 A 的鎖,從而構建了一個資源獲取環。

如果有兩個及以上的CPU占用率達到100%時,極可能是程序進入死鎖狀態。

死鎖的存在是因為有資源獲取環的存在,所以只要能檢測出資源獲取環,就等同于檢測出死鎖的存在。

構建一個死鎖

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


pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;

void *thread_funcA(void *arg)
{
	pthread_mutex_lock(&mutex1);
	sleep(1);
	pthread_mutex_lock(&mutex2);

	printf("funcA --> \n");

	pthread_mutex_unlock(&mutex2);
	pthread_mutex_unlock(&mutex1);
}

void *thread_funcB(void *arg)
{
	pthread_mutex_lock(&mutex2);
	sleep(1);
	pthread_mutex_lock(&mutex3);

	printf("funcB --> \n");

	pthread_mutex_unlock(&mutex3);
	pthread_mutex_unlock(&mutex2);
}

void *thread_funcC(void *arg)
{
	pthread_mutex_lock(&mutex3);
	sleep(1);
	pthread_mutex_lock(&mutex4);

	printf("funcC --> \n");

	pthread_mutex_unlock(&mutex4);
	pthread_mutex_unlock(&mutex3);
}

void *thread_funcD(void *arg)
{
	pthread_mutex_lock(&mutex4);
	sleep(1);
	pthread_mutex_lock(&mutex1);

	printf("funcD --> \n");

	pthread_mutex_unlock(&mutex1);
	pthread_mutex_unlock(&mutex4);
}


int main()
{
	pthread_t tid[4] = { 0 };
	
	pthread_create(&tid[0], NULL, thread_funcA, NULL);
	pthread_create(&tid[1], NULL, thread_funcB, NULL);
	pthread_create(&tid[2], NULL, thread_funcC, NULL);
	pthread_create(&tid[3], NULL, thread_funcD, NULL);

	pthread_join(tid[0], NULL);
	pthread_join(tid[1], NULL);
	pthread_join(tid[2], NULL);
	pthread_join(tid[3], NULL);

	return 0;
}

使用hook檢測死鎖

dlsym()函數

獲取共享對象或可執行文件中符號的地址。
函數原型:

#include <dlfcn.h>

void *dlsym(void *handle, const char *symbol);

#define _GNU_SOURCE
#include <dlfcn.h>

void *dlvsym(void *handle, char *symbol, char *version);

// Link with -ldl.

描述:
函數dlsym()接受dlopen()返回的動態加載共享對象的“句柄”以及以空結尾的符號名,并返回該符號加載到內存中的地址。如果在指定對象或加載對象時dlopen()自動加載的任何共享對象中找不到該符號,dlsym()將返回NULL。(dlsym()執行的搜索是通過這些共享對象的依賴關系樹進行的廣度優先搜索。)

由于符號的值實際上可能是NULL(因此,dlsym()的NULL返回值不必指示錯誤),因此測試錯誤的正確方法是調用dlerror()以清除任何舊的錯誤條件,然后調用dlsym。

handle中可以指定兩個特殊的偽句柄:

代碼 含義
RTLD_DEFAULT 使用默認共享對象搜索順序查找所需符號的第一個匹配項。搜索將包括可執行文件及其依賴項中的全局符號,以及使用RTLD_GLOBAL標志動態加載的共享對象中的符號。
RTLD_NEXT 在當前對象之后,按搜索順序查找所需符號的下一個匹配項。這允許在另一個共享對象中為函數提供包裝,例如,預加載共享對象中的函數定義可以查找并調用另一共享對象中提供的“真實”函數(或者在預加載有多層的情況下,函數的“下一個”定義)。

函數dlvsym()的作用與dlsym()相同,但使用版本字符串作為附加參數。

返回值:
成功時,這些函數返回與符號關聯的地址。
失敗時,返回NULL;可以使用dlerror()診斷錯誤的原因。

pthread_self()函數

獲取調用線程的ID。
函數原型:

#include <pthread.h>

pthread_t pthread_self(void);

// Compile and link with -pthread.

說明:
函數的作用是返回調用線程的ID。這與創建此線程的pthread_create()調用中*thread中返回的值相同。

返回值:

此函數始終成功,返回調用線程的ID。

hook使用場景

(1)實現自己的協議棧,通過hook posix api。

使用步驟

(1)構建函數指針
(2)定義與目標函數一樣的類型

typedef int(*pthread_mutex_lock_t)(pthread_mutex_t *mutex);
typedef int(*pthread_mutex_unlock_t)(pthread_mutex_t *mutex);

pthread_mutex_lock_t	pthread_mutex_lock_f;
pthread_mutex_unlock_t	pthread_mutex_unlock_f;

(3)具體函數實現,函數名與目標函數名一致

int pthread_mutex_lock(pthread_mutex_t *mutex)
{
	pthread_t selfid = pthread_self();

	printf("pthread_mutex_lock: %ld, %p\n", selfid, mutex);
	// ...
	return 0;
}

int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
	pthread_t selfid = pthread_self();

	printf("pthread_mutex_unlock: %ld, %p\n", selfid, mutex);
	// ...
	return 0;
}

(4)調用dlsym()函數,即鉤子。

int init_hook()
{
	pthread_mutex_lock_f = dlsym(RTLD_NEXT, "pthread_mutex_lock");
	pthread_mutex_unlock_f = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
	// ...
	return 0;
}

示例代碼

#define _GNU_SOURCE
#include <dlfcn.h>


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


typedef int(*pthread_mutex_lock_t)(pthread_mutex_t *mutex);
typedef int(*pthread_mutex_unlock_t)(pthread_mutex_t *mutex);

pthread_mutex_lock_t	pthread_mutex_lock_f;
pthread_mutex_unlock_t	pthread_mutex_unlock_f;

int pthread_mutex_lock(pthread_mutex_t *mutex)
{
	pthread_t selfid = pthread_self();

	pthread_mutex_lock_f(mutex);
	printf("pthread_mutex_lock: %ld, %p\n", selfid, mutex);
	
	return 0;
}

int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
	pthread_t selfid = pthread_self();

	pthread_mutex_unlock_f(mutex);
	printf("pthread_mutex_unlock: %ld, %p\n", selfid, mutex);

	return 0;
}

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

#if 1 // debug

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;

void *thread_funcA(void *arg)
{
	pthread_mutex_lock(&mutex1);
	sleep(1);
	pthread_mutex_lock(&mutex2);

	printf("funcA --> \n");

	pthread_mutex_unlock(&mutex2);
	pthread_mutex_unlock(&mutex1);
}

void *thread_funcB(void *arg)
{
	pthread_mutex_lock(&mutex2);
	sleep(1);
	pthread_mutex_lock(&mutex3);

	printf("funcB --> \n");

	pthread_mutex_unlock(&mutex3);
	pthread_mutex_unlock(&mutex2);
}

void *thread_funcC(void *arg)
{
	pthread_mutex_lock(&mutex3);
	sleep(1);
	pthread_mutex_lock(&mutex4);

	printf("funcC --> \n");

	pthread_mutex_unlock(&mutex4);
	pthread_mutex_unlock(&mutex3);
}

void *thread_funcD(void *arg)
{
	pthread_mutex_lock(&mutex4);
	sleep(1);
	pthread_mutex_lock(&mutex1);

	printf("funcD --> \n");

	pthread_mutex_unlock(&mutex1);
	pthread_mutex_unlock(&mutex4);
}


int main()
{

	init_hook();

	pthread_t tid[4] = { 0 };
	
	pthread_create(&tid[0], NULL, thread_funcA, NULL);
	pthread_create(&tid[1], NULL, thread_funcB, NULL);
	pthread_create(&tid[2], NULL, thread_funcC, NULL);
	pthread_create(&tid[3], NULL, thread_funcD, NULL);

	pthread_join(tid[0], NULL);
	pthread_join(tid[1], NULL);
	pthread_join(tid[2], NULL);
	pthread_join(tid[3], NULL);

	return 0;
}

#endif

缺點:這種方式在少量鎖情況下還可以分析,在大量鎖使用的情況,分析過程極為困難。

使用圖算法檢測死鎖

死鎖檢測可以利用圖算法,檢測有向圖是否有環

圖的構建

(1)矩陣

指向 1 指向 2 指向 3 指向 …
節點 1
節點 2
節點 3
節點 …

(2)鄰接表
數據結構原理示意圖:

“圖”連接:

圖的使用

先新增節點再新增邊。
(1)每創建一個線程,新增一個節點;注意,不是線程創建的時候就要加節點(有些線程不會用到鎖),而是線程調用鎖(以互斥鎖為例,pthread_mutex_lock() )的時候才添加節點。
(2)線程加鎖(以互斥鎖為例,pthread_mutex_lock() )的時候,并且檢測到鎖已經占用,則新增一條邊。
(3)移除邊,調用鎖(以互斥鎖為例,pthread_mutex_lock() )前,如果此時鎖沒有被占用,并且該邊存在,則移除邊。
(4)移除節點是在解鎖之后。

三個原語操作:
(1)加鎖之前的操作,lock_before();
(2)加鎖之后的操作,lock_after();
(3)解鎖之后的操作,unlock_after();

示例代碼

#define _GNU_SOURCE
#include <dlfcn.h>

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

#define ENABLE_GRAPH	1

#if ENABLE_GRAPH

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

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

#include <unistd.h>


typedef unsigned long int uint64;


#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





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;

}


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 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)) { // 如果鎖已存在

#if 1

			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); // 則加邊
			}
#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;

			add_edge(from, to); // 加邊

#endif

		}
	}
}

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 {

		/* 當A調用lock時,B已占用,就建立了邊,
		當B釋放鎖并且A搶到鎖,需要把之前加的邊移除*/

		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) {//用戶數為0才刪除節點
		tg->locklist[idx].id = 0;
		tg->locklist[idx].lock_id = 0;
		//inc(&tg->lockidx, -1);
	}

}

/********************* 檢測死鎖 start ***************************/

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);

}

/********************* 檢測死鎖 end ***************************/


/********************* hook start ***************************/

typedef int(*pthread_mutex_lock_t)(pthread_mutex_t *mutex);
typedef int(*pthread_mutex_unlock_t)(pthread_mutex_t *mutex);

pthread_mutex_lock_t	pthread_mutex_lock_f;
pthread_mutex_unlock_t	pthread_mutex_unlock_f;

int pthread_mutex_lock(pthread_mutex_t *mutex)
{
	pthread_t selfid = pthread_self();

#if ENABLE_GRAPH

	lock_before(selfid, (uint64)mutex);

	pthread_mutex_lock_f(mutex);
	lock_after(selfid, (uint64)mutex);

#else

	pthread_mutex_lock_f(mutex);

#endif

	printf("pthread_mutex_lock: %ld, %p\n", selfid, mutex);
	
	return 0;
}

int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
	pthread_t selfid = pthread_self();

	pthread_mutex_unlock_f(mutex);

#if ENABLE_GRAPH
	unlock_after(selfid, (uint64)mutex);
#endif

	printf("pthread_mutex_unlock: %ld, %p\n", selfid, mutex);

	return 0;
}

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

/********************* hook end ***************************/

#if 1 // debug

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;

void *thread_funcA(void *arg)
{
	pthread_mutex_lock(&mutex1);
	sleep(1);
	pthread_mutex_lock(&mutex2);

	printf("funcA --> \n");

	pthread_mutex_unlock(&mutex2);
	pthread_mutex_unlock(&mutex1);
}

void *thread_funcB(void *arg)
{
	pthread_mutex_lock(&mutex2);
	sleep(1);
	pthread_mutex_lock(&mutex3);

	printf("funcB --> \n");

	pthread_mutex_unlock(&mutex3);
	pthread_mutex_unlock(&mutex2);
}

void *thread_funcC(void *arg)
{
	pthread_mutex_lock(&mutex3);
	sleep(1);
	pthread_mutex_lock(&mutex4);

	printf("funcC --> \n");

	pthread_mutex_unlock(&mutex4);
	pthread_mutex_unlock(&mutex3);
}

void *thread_funcD(void *arg)
{
	pthread_mutex_lock(&mutex4);
	sleep(1);
	pthread_mutex_lock(&mutex1);

	printf("funcD --> \n");

	pthread_mutex_unlock(&mutex1);
	pthread_mutex_unlock(&mutex4);
}


int main()
{

	init_hook();

#if ENABLE_GRAPH

	start_check();
	printf("start check dead lock.\n");
#endif

	pthread_t tid[4] = { 0 };
	
	pthread_create(&tid[0], NULL, thread_funcA, NULL);
	pthread_create(&tid[1], NULL, thread_funcB, NULL);
	pthread_create(&tid[2], NULL, thread_funcC, NULL);
	pthread_create(&tid[3], NULL, thread_funcD, NULL);

	pthread_join(tid[0], NULL);
	pthread_join(tid[1], NULL);
	pthread_join(tid[2], NULL);
	pthread_join(tid[3], NULL);

	return 0;
}

#endif

總結

死鎖的產生是因為多線程之間存在交叉申請鎖的情況,因爭奪資源而造成的一種僵局。
hook使用:
(1)定義與目標函數一樣的類型;
(2)具體函數實現,函數名與目標函數名一致;
(3)調用dlsym()函數,初始化hook。

死鎖檢測可以使用圖算法,通過檢測有向圖是否有環判斷是否有死鎖。

后言

本專欄知識點是通過<零聲教育>的系統學習,進行梳理總結寫下文章,對c/c++linux系統提升感興趣的讀者,可以點擊鏈接,詳細查看詳細的服務:C/C++服務器

原文鏈接:https://blog.csdn.net/Long_xu/article/details/127028231

欄目分類
最近更新