#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h>
void sig_handler(int signum)
{
for (int n = 0, status = 0; waitpid(-1, &status, WNOHANG) > 0; n++);
/*!
* 假如在执行该中断处理程序时,stdout所持有的锁没有被释放,则会出现死锁。
*/
fprintf(stdout, "Captured signal[%d]\n", signum);
fprintf(stdout, "Signal[%d] handle finished!\n", signum);
}
static struct sigaction chld_signal = {
.sa_handler = sig_handler,
.sa_flags = SA_RESTART,
};
static void *thread(void *arg)
{
while (1) {
char buffer[1024] = {0};
snprintf(buffer, sizeof(buffer) - 1, "[%s:%d] [%ld] This is a running thread, for output data to stdout!\r\n", __FILE__, __LINE__, syscall(SYS_gettid));
size_t num = strlen(buffer);
/*!
* 在C库中会对stdout对象进行加锁,假如中断发生时该锁没有被释放,
* 从而去执行了中断处理程序sig_handler,在sig_handler中的fprintf又对stdout进行了加锁,
* 这样就出现连续2次加锁的情况,从而出现死锁。
*/
fwrite(buffer, sizeof(char), num, stdout);
fflush(stdout);
}
}
static void *thread2(void *arg)
{
while (1) {
/*! 产生中断信号 */
system("date &");
}
}
static void *thread3(void *arg)
{
while (1) {
usleep(10000);
/*! 产生中断信号 */
system("date &");
}
}
int main()
{
sigaction(SIGCHLD, &chld_signal, NULL);
for (int index = 0; index < 1; index++) {
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
}
for (int index = 0; index < 1; index++) {
pthread_t tid;
pthread_create(&tid, NULL, thread2, NULL);
}
for (int index = 0; index < 1; index++) {
pthread_t tid = -1;
pthread_create(&tid, NULL, thread3, NULL);
}
while (1) {
usleep(10000);
/*! 产生中断信号 */
system("date &");
}
return 0;
}
说明:
在用fwrite不断向标准输出(stdout)写数据的过程中,如果系统调用被中断信号所打断,则原先系统调用会停止运行,继而执行中断处理程序;假如在打断过程中已经对一个对象加了锁,而中断后该锁没有被释放,从而会造成死锁情况的发生(该例中的stdout就出现了死锁情况)。
分析
Thread 4 (LWP 27105):
#0 0x0000007fa87a73a4 in __lll_lock_wait_private (futex=0x7fa8819548 <_IO_stdfile_1_lock>) at ./lowlevellock.c:33
#1 0x0000007fa8716784 in _IO_vfprintf_internal (s=0x7fa88184f0 <_IO_2_1_stdout_>, format=0x400d30 "Captured signal[%d]\n", ap=...) at vfprintf.c:1325
#2 0x0000007fa871d978 in __fprintf (stream=<optimized out>, format=<optimized out>) at fprintf.c:32
#3 0x0000000000400a94 in sig_handler (signum=17) at test3.c:19
#4 <signal handler called>
#5 0x0000007fa8733044 in __GI__IO_fwrite (buf=0x7fa86d05e8, size=1, count=75, fp=0x7fa88184f0 <_IO_2_1_stdout_>) at iofwrite.c:37
#6 0x0000000000400b4c in thread (arg=0x0) at test3.c:41
#7 0x0000007fa88240e8 in start_thread () from ../rootfs/lib/libpthread.so.0
#8 0x0000007fa879a92c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78
Thread 3 (LWP 27107):
#0 0x0000007fa87a73d0 in __lll_lock_wait_private (futex=0x7fa88192c8 <lock>) at ./lowlevellock.c:30
#1 0x0000007fa870f394 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:157
#2 0x0000000000400b9c in thread3 (arg=0x0) at test3.c:59
#3 0x0000007fa88240e8 in start_thread () from ../rootfs/lib/libpthread.so.0
#4 0x0000007fa879a92c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78
Thread 2 (LWP 27106):
#0 0x0000007fa87a73d0 in __lll_lock_wait_private (futex=0x7fa8819548 <_IO_stdfile_1_lock>) at ./lowlevellock.c:30
#1 0x0000007fa8716784 in _IO_vfprintf_internal (s=0x7fa88184f0 <_IO_2_1_stdout_>, format=0x400d30 "Captured signal[%d]\n", ap=...) at vfprintf.c:1325
#2 0x0000007fa871d978 in __fprintf (stream=<optimized out>, format=<optimized out>) at fprintf.c:32
#3 0x0000000000400a94 in sig_handler (signum=17) at test3.c:19
#4 <signal handler called>
#5 0x0000007fa87031f4 in __sigprocmask (how=how@entry=2, set=<optimized out>, set@entry=0x7fa7ecf8b8, oset=oset@entry=0x0) at ../sysdeps/unix/sysv/linux/sigprocmask.c:54
#6 0x0000007fa870f3e4 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:161
#7 0x0000000000400b78 in thread2 (arg=0x0) at test3.c:50
#8 0x0000007fa88240e8 in start_thread () from ../rootfs/lib/libpthread.so.0
#9 0x0000007fa879a92c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78
Thread 1 (LWP 27104):
#0 0x0000007fa87a73a4 in __lll_lock_wait_private (futex=0x7fa88192c8 <lock>) at ./lowlevellock.c:33
#1 0x0000007fa870f394 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:157
#2 0x0000000000400c90 in main () at test3.c:85
(gdb) thread 4
[Switching to thread 4 (LWP 27105)]
#0 0x0000007fa87a73a4 in __lll_lock_wait_private (futex=0x7fa8819548 <_IO_stdfile_1_lock>) at ./lowlevellock.c:33
33 ./lowlevellock.c: No such file or directory.
(gdb) frame 5
#5 0x0000007fa8733044 in __GI__IO_fwrite (buf=0x7fa86d05e8, size=1, count=75, fp=0x7fa88184f0 <_IO_2_1_stdout_>) at iofwrite.c:37
37 iofwrite.c: No such file or directory.
(gdb) print -pretty -- *fp->_lock
$3 = {
lock = 2, // 表示被加了两次锁。
cnt = 0,
owner = 0x0
}
解决方案
- 第一种方案:将sig_handler中的标准输出注释掉
- 第二种方案:将sig_handler中的stdout替换为stderr