linux 捕获信号处理中遇到的死锁
tag: 信号 signal sigchld 死锁 堆栈
我们的程序需要捕获信号自己处理,所以尝试对1-32的信号处理(后面33-64的信号不处理)。
但是在调试代码时,发现一个线程死锁的问题。
程序目的:捕获信号,然后打印堆栈。
|
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
|
伪代码如下:设置捕获信号函数(){ //设置信号处理函数 sigact.sa_sigaction = TsSigHandler; // //这里捕获了很多信号,包括SIGCHLD:子进程结束,父进程会收到该信号 sigaction( SIGSEGV, &sigact, NULL ); .... sigaction( SIGCHLD, &sigact, NULL ); } 信号处理函数:TsSigHandler{ //调用打印堆栈函数 PrintStack();} 打印堆栈函数PrintStack{ //打印堆栈 backtrace(); backtrace_symbols(); //调用system函数执行一些命令 system("xxxxxx");} |
Thread 12 (Thread 0xf7dd2b90 (LWP 5770)):
以下是一个让我觉得奇怪的堆栈,奇怪之处:
1.死锁了:__lll_lock_wait_private
2.获得了2个信号:<signal handler called>,为什么不是一个一个信号处理
堆栈如下:
|
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
|
#0 0xffffe410 in __kernel_vsyscall ()#1 0x002a0783 in __lll_lock_wait_private () from /lib/libc.so.6#2 0x001f8448 in _L_lock_124 () from /lib/libc.so.6#3 0x001f7f8b in do_system () from /lib/libc.so.6#4 0x001f8412 in system () from /lib/libc.so.6#5 0x00317ead in system () from /lib/libpthread.so.0#6 0x080f95c1 in PrintStack() ()#7 0x080f9844 in TsSigHandler(int, siginfo*, void*) ()#8 <signal handler called>#9 0xffffe410 in __kernel_vsyscall ()#10 0x001eb1a9 in sigprocmask () from /lib/libc.so.6#11 0x001f8132 in do_system () from /lib/libc.so.6#12 0x001f8412 in system () from /lib/libc.so.6#13 0x00317ead in system () from /lib/libpthread.so.0#14 0x080f95c1 in PrintStack() ()#15 0x080f9844 in TsSigHandler(int, siginfo*, void*) ()#16 <signal handler called>#17 0x002338ec in memcpy () from /lib/libc.so.6#18 0x0804fa02 in boom ()#19 0x080dbd9c in RunCmd ()#20 0x080dbf12 in CmdParse ()#21 0x080dc705 in OspTeleDaemon ()#22 0x080f8817 in OspTaskTemplateFunc(void*) ()#23 0x0030f832 in start_thread () from /lib/libpthread.so.0#24 0x00293e0e in clone () from /lib/libc.so.6 |
#18 0x0804fa02 in boom ()
boom()是我写的一个制造崩溃的函数:
char *pBoom = NULL;
memcpy( pBoom, "aaaa", 100 );
#16 <signal handler called>
触发信号
#15 0x080f9844 in TsSigHandler(int, siginfo*, void*) ()
TsSigHandler是信号处理函数。通过以下代码设置:
struct sigaction sigact;
sigemptyset( &sigact.sa_mask );
sigact.sa_flags = SA_ONESHOT | SA_SIGINFO;
sigact.sa_sigaction = TsSigHandler;
信号触发后,由TsSigHandler函数处理
#14 0x080f95c1 in PrintStack() ()
TsSigHandler函数中调用PrintStack函数打印堆栈。
#13 0x00317ead in system () from /lib/libpthread.so.0
PrintStack函数中调用了system函数做一些额外的事情,例如执行gcore(事实证明,这种方法是有点问题的)。
#11 0x001f8132 in do_system () from /lib/libc.so.6
system调用了do_system
#10 0x001eb1a9 in sigprocmask () from /lib/libc.so.6
do_system调用sigprocmask
#8 <signal handler called>
关键来了:这是获取到了另外一个信号:SIGCHLD。
#7 0x080f9844 in TsSigHandler(int, siginfo*, void*) ()
又调用信号处理函数TsSigHandler
#3 0x001f7f8b in do_system () from /lib/libc.so.6
system调用do_system,调用流程和上面当然是一样的
#2 0x001f8448 in _L_lock_124 () from /lib/libc.so.6
#1 0x002a0783 in __lll_lock_wait_private () from /lib/libc.so.6
nice!锁住了。。
分析:
|
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
|
/* Execute LINE as a shell command, returning its status. */static intdo_system (const char *line){ int status, save; pid_t pid; struct sigaction sa;#ifndef _LIBC_REENTRANT struct sigaction intr, quit;#endif sigset_t omask; sa.sa_handler = SIG_IGN; sa.sa_flags = 0; __sigemptyset (&sa.sa_mask); DO_LOCK (); if (ADD_REF () == 0) { if (__sigaction (SIGINT, &sa, &intr) < 0) { (void) SUB_REF (); goto out; } if (__sigaction (SIGQUIT, &sa, &quit) < 0) { save = errno; (void) SUB_REF (); goto out_restore_sigint; } } DO_UNLOCK (); /* We reuse the bitmap in the 'sa' structure. */ __sigaddset (&sa.sa_mask, SIGCHLD); save = errno; if (__sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0) {#ifndef _LIBC if (errno == ENOSYS) __set_errno (save); else#endif { DO_LOCK (); if (SUB_REF () == 0) { save = errno; (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL); out_restore_sigint: (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL); __set_errno (save); } out: DO_UNLOCK (); return -1; } }#ifdef CLEANUP_HANDLER CLEANUP_HANDLER;#endif#ifdef FORK pid = FORK ();#else pid = __fork ();#endif if (pid == (pid_t) 0) { /* Child side. */ const char *new_argv[4]; new_argv[0] = SHELL_NAME; new_argv[1] = "-c"; new_argv[2] = line; new_argv[3] = NULL; /* Restore the signals. */ (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL); (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL); (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL); INIT_LOCK (); /* Exec the shell. */ (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ); _exit (127); } else if (pid < (pid_t) 0) /* The fork failed. */ status = -1; else /* Parent side. */ { /* Note the system() is a cancellation point. But since we call waitpid() which itself is a cancellation point we do not have to do anything here. */ if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid) status = -1; }#ifdef CLEANUP_HANDLER CLEANUP_RESET;#endif save = errno; DO_LOCK (); if ((SUB_REF () == 0 && (__sigaction (SIGINT, &intr, (struct sigaction *) NULL) | __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0) || __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0) {#ifndef _LIBC /* glibc cannot be used on systems without waitpid. */ if (errno == ENOSYS) __set_errno (save); else#endif status = -1; } DO_UNLOCK (); return status;} |
system()函数执行的大体过程是:fork()->exec()->waitpid(),
waitpid用于等待子进程执行完毕。
但是在子进程执行完毕时,会产生SIGCHLD信号,
而SIGCHLD信号会唤醒wait中的进程,这就是看到了2个信号的原因,
解决方法:
1.忽略SIGCHLD信号:其实这个信号一般情况下应该被忽略,除非你的程序需要对这种情况做非常特殊的处理
2.不要在这里调用system()
to do: 有空了记得补详细些
linux 捕获信号处理中遇到的死锁的更多相关文章
- Linux 多线程应用中如何编写安全的信号处理函数
http://blog.163.com/he_junwei/blog/static/1979376462014021105242552/ http://www.ibm.com/developerwor ...
- Linux 多线程应用中如何编写安全的信号处理函数【转】
转自:https://www.cnblogs.com/virusolf/p/4945642.html http://blog.163.com/he_junwei/blog/static/1979376 ...
- (4.7)怎么捕获和记录SQL Server中发生的死锁?
转自:https://blog.csdn.net/c_enhui/article/details/19498327 怎么捕获和记录SQL Server中发生的死锁? 关键词:死锁记录,死锁捕获 sql ...
- linux 多线程信号处理总结
linux 多线程信号总结(一) 1. 在多线程环境下,产生的信号是传递给整个进程的,一般而言,所有线程都有机会收到这个信号,进程在收到信号的的线程上下文执行信号处理函数,具体是哪个线程执行的难以获知 ...
- Linux驱动设备中的并发控制
一.基本概念 二.中断屏蔽 三.原子操作 四.自旋锁 五.信号量 六.互斥体 七.自旋锁与信号量的比较 Linux设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发的访问会导致竞态,即使 ...
- 基于innodb_print_all_deadlocks从errorlog中解析MySQL死锁日志
本文是说明如何获取死锁日志记录的,不是说明如何解决死锁问题的. MySQL的死锁可以通过show engine innodb status;来查看,但是show engine innodb statu ...
- JDK中ThreadDump诊断Java代码中的线程死锁问题
多线程的死锁..死锁不是死了而是线程互相等待... 在项目中可能就是在几十万行的代码中存在一个死锁的问题,如何发现这个问题并且解决这个问题. JavaJDK为我们提供了一个诊断工具叫做ThreadDu ...
- Linux内核调试方法总结之死锁问题分析
死锁问题分析 死锁就是多个进程(线程)因为等待别的进程已占有的自己所需要的资源而陷入阻塞的一种状态,死锁状态一旦形成,进程本身是解决不了的,需要外在的推动,才能解决,最重要的是死锁不仅仅影响进程业务, ...
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(3)
作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...
随机推荐
- NOIP1998 拼数
http://www.luogu.org/problem/show?pid=1012 题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,3 ...
- Digest [IAB_SRI_Online_Advertising_Effectiveness]
http://www.pwc.com/en_GX/gx/entertainment-media/pdf/IAB_SRI_Online_Advertising_Effectiveness_v3.pdf
- linux Apache和php配置
今天安装Apache httpd web服务器,安装过程分为三部分: (1)./configure (2)make (3)make install (需要root权限) 我的apache 安装在/ho ...
- NSBundle 的使用
NSBundle 读取图片 plist text NSBundle *mainbundle=[NSBundle mainBundle]; //使mainBundle 对象获取图片的路径 NSStrin ...
- XSS CSRF 攻击
XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery)定义: 跨网站脚本(Cross-site scripting ...
- Android IOS WebRTC 音视频开发总结(五五)-- 音视频通讯中的抗丢包与带宽自适应原理
本文主要分析webrtc中的抗丢包与带宽自适应原理,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help 文章内容主要来自中国电信北京研究院丁博士在 ...
- C++ STL 简单记录
1,STL提供三种类型的组件:容器.迭代器.算法. 容器: 顺序容器(vector.list.deque.string等)是一系列元素的有序集合: 关联容器(set.multiset.map.mult ...
- 高仿QQ的即时通讯应用带服务端软件安装
Android 基于xmpp协议,smack包,openfire服务端(在下面)的高仿QQ的即时通讯实现.实现了注册,登录,读取好友列表,搜索好友,添加分组,添加好友,删除好友,修改心情,两个客户端之 ...
- MySQL查询昨天、今天、7天、近30天、本月、上一月数据
文章同步发表在博主网站朗度云,传输门:http://www.wolfbe.com/detail/201608/291.html 在开发或者统计时,我们可能需要统计某个表的数据.比如:查看今天新增的 ...
- 【PHP】金额数字转换成大写形式
<?php /*将数字金额转成大写*/ function num_to_upper($num) { $d = array('零','壹','贰','叁','肆','伍','陆','柒','捌', ...