【发布时间】:2018-04-04 06:24:54
【问题描述】:
我遇到了一个非常烦人的问题: 我有一个程序在开始时创建一个线程,该线程将在其执行期间启动其他东西(fork() 紧接着是 execve())。
这是我的程序到达(我认为)死锁时两个线程的 bt:
线程 2 (LWP 8839):
#0 0x00007ffff6cdf736 in __libc_fork () at ../sysdeps/nptl/fork.c:125
#1 0x00007ffff6c8f8c0 in _IO_new_proc_open (fp=fp@entry=0x7ffff00031d0, command=command@entry=0x7ffff6c26e20 "ps -u brejon | grep \"cvc\"
#2 0x00007ffff6c8fbcc in _IO_new_popen (command=0x7ffff6c26e20 "ps -u user | grep \"cvc\" | wc -l", mode=0x42c7fd "r") at iopopen.c:296
#3-4 ...
#5 0x00007ffff74d9434 in start_thread (arg=0x7ffff6c27700) at pthread_create.c:333
#6 0x00007ffff6d0fcfd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
线程 1 (LWP 8835):
#0 __lll_lock_wait_private () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95
#1 0x00007ffff6ca0ad9 in malloc_atfork (sz=140737337120848, caller=) at arena.c:179
#2 0x00007ffff6c8d875 in __GI__IO_file_doallocate (fp=0x17a72230) at filedoalloc.c:127
#3 0x00007ffff6c9a964 in __GI__IO_doallocbuf (fp=fp@entry=0x17a72230) at genops.c:398
#4 0x00007ffff6c99de8 in _IO_new_file_overflow (f=0x17a72230, ch=-1) at fileops.c:820
#5 0x00007ffff6c98f8a in _IO_new_file_xsputn (f=0x17a72230, data=0x17a16420, n=682) at fileops.c:1331
#6 0x00007ffff6c6fcb2 in _IO_vfprintf_internal (s=0x17a72230, format=, ap=ap@entry=0x7fffffffcf18) at vfprintf.c:1632
#7 0x00007ffff6c76a97 in __fprintf (stream=, format=) at fprintf.c:32
#8-11 ...
#12 0x000000000042706e in main (argc=3, argv=0x7fffffffd698, envp=0x7fffffffd6b8) at mains/ignore/.c:146
glibc-2.17 和 glibc-2.23 都永远停留在这里
欢迎任何帮助:'D
编辑:
这是一个最小的例子:
1 #include <stdlib.h>
2 #include <pthread.h>
3 #include <unistd.h>
4
5 void * thread_handler(void * args)
6 {
7 char * argv[] = { "/usr/bin/ls" };
8 char * newargv[] = { "/usr/bin/ls", NULL };
9 char * newenviron[] = { NULL };
10 while (1) {
11 if (vfork() == 0) {
12 execve(argv[0], newargv, newenviron);
13 }
14 }
15
16 return 0;
17 }
18
19 int main(void)
20 {
21 pthread_t thread;
22 pthread_create(&thread, NULL, thread_handler, NULL);
23
24 int * dummy_alloc;
25
26 while (1) {
27 dummy_alloc = malloc(sizeof(int));
28 free(dummy_alloc);
29 }
30
31 return 0;
32 }
环境: user:deadlock$ cat /etc/redhat-release
Scientific Linux 7.3 版(氮气)
用户:死锁$ ldd --version
ldd (GNU libc) 2.17
编辑 2: rpm包版本为:glibc-2.17-196.el7.x86_64
我无法使用 rpm 包获取行号。这是使用随发行版提供的 glibc 的 BT: 已使用 debuginfo 解决。
(gdb) 线程应用所有 bt
线程 2(线程 0x7ffff77fb700 (LWP 59753)):
#0 vfork () at ../sysdeps/unix/sysv/linux/x86_64/vfork.S:44
#1 0x000000000040074e in thread_handler (args=0x0) at deadlock.c:11
#2 0x00007ffff7bc6e25 in start_thread (arg=0x7ffff77fb700) at pthread_create.c:308
#3 0x00007ffff78f434d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:113
线程 1(线程 0x7ffff7fba740 (LWP 59746)):
#0 0x00007ffff7878226 in _int_free (av=0x7ffff7bb8760 , p=0x602240, have_lock=0) at malloc.c:3927
#1 0x00000000004007aa in main () at deadlock.c:28
【问题讨论】:
-
请贴出代码。
-
嗨,在多线程程序中调用 fork 会导致问题,当在线程中调用时,它会复制整个地址空间,但不会克隆任何线程,除了调用它的线程。这将导致永久资源锁定。此行为在 POSIX 中定义。
-
您的原始代码使用
fork,但您的示例使用vfork,因此不具有代表性。你的环境是什么?我想知道__libc_fork究竟是哪个锁被阻塞(即,第 125 行是什么?)。只有两个线程吗?任何使用 C++ 流的东西?或致电fflush (NULL)?多年来,我们确实修复了这方面的一些错误。 -
我读到在多线程程序中调用 fork 只会复制子进程中的调用线程,我完全可以接受这种行为。正如您在最小示例中看到的那样,我没有在 fork() 和 execve() 之间调用任何函数。