-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadlock_success.c
More file actions
564 lines (368 loc) · 10.1 KB
/
Copy pathdeadlock_success.c
File metadata and controls
564 lines (368 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#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;
#define MAX 100
enum Type {PROCESS, RESOURCE};
// 资源信息 用于表示任务图中的一个节点(vertex)的相关信息。
struct source_type
{
uint64 id; // 拥有该资源的线程 id
enum Type type; // 顶点类型:线程 or 资源 a->b a线程申请b资源
uint64 lock_id; // 资源(锁) 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; // 资源(锁)的数量
};
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 ++;
}
}
//添加一条边 a->b
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);
}
//i到j是是否存在一条边 无返回0 有返回1
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;
}
//释放to 保留from a->b a->null
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);
}
//广度优先遍历 看是否存在回环
/*
1 ---> 2
| /
| /
v /
3 ---> 4
*/
int DFS(int idx) {
//1.访问
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;
//2.访问邻接表的边表
while (ver->next != NULL) {
DFS(search_vertex(ver->next->s)); //C:dfs(1) dfs(2) dfs(3)
k --;//回溯 (1 2 ) (1)
ver = ver->next; //取边表的下一个 1-2 -3 2-0 3-4 4-0
}
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; //使用不同的顶点 进行 dfs
}
}
//检查是否有死锁
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);
}
//遍历以互斥锁为key 找到返回idx 失败返回-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;
}
//遍历以互斥锁为key 找到返回idx 失败返回插入位置
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 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");
}
#if 1 //debug
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()
{
//开启死锁检测模块 start
init_hook();
start_check();
//开启死锁检测模块 end
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;
}
#endif