<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=GB2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-text-html"> <font face="΢ÈíÑźÚ">I've found that a
forked child runs into dead lock in a multithreaded application.<br>
The test code is:<br>
<br>
[root@localhost]# cat a.c<br>
#include <stdio.h><br>
#include <stdlib.h><br>
#include <pthread.h><br>
#include "include/jemalloc/jemalloc.h"<br>
<br>
void *worker(void *arg) {<br>
int j;<br>
char *p;<br>
<br>
while (1) {<br>
for (j=1; j<5000; ++j) {<br>
p = je_malloc(j);<br>
je_free(p);<br>
}<br>
}<br>
return NULL;<br>
}<br>
<br>
int main(int argc, char **argv) {<br>
int pid;<br>
int j;<br>
char *p;<br>
pthread_t tid;<br>
int n = 20;<br>
<br>
if (argc > 1)<br>
n = atoi(argv[1]);<br>
<br>
for (j=0; j<10; ++j)<br>
pthread_create(&tid, NULL, worker, NULL);<br>
<br>
int i = 0;<br>
while (i++<n) {<br>
if ((pid = fork()) == 0) {<br>
/* child */<br>
fprintf(stderr, "child %d\n", i);<br>
int cnt;<br>
for (cnt=0; cnt<100; ++cnt) {<br>
for (j=1; j<5000; ++j) {<br>
p = je_malloc(j);<br>
je_free(p);<br>
}<br>
}<br>
exit(0);<br>
}<br>
usleep(10000);<br>
}<br>
<br>
sleep(1);<br>
while (n--) {<br>
fprintf(stderr, "%d children running...\n", n+1);<br>
pid = wait(NULL);<br>
}<br>
return 0;<br>
}<br>
<br>
1. jemalloc is configured using:<br>
# ./configure CFLAGS=-std=gnu99 -Wall -pipe -g3 -O2
-funroll-loops --with-jemalloc-prefix=je_ --enable-cc-silence
&& make lib/libjemalloc.a<br>
<br>
2. compile the test code using:<br>
# gcc -g -ggdb -o 3.0.0 a.c lib/libjemalloc.a -lpthread -ldl<br>
<br>
3. run the test:<br>
</font><font face="΢ÈíÑźÚ">[root@localhost]# ./3.0.0 4<br>
child 1<br>
child 2<br>
child 3<br>
child 4<br>
4 children running...<br>
3 children running...<br>
2 children running...<br>
^C<br>
</font><font face="΢ÈíÑźÚ">[root@localhost]#<br>
<br>
4. Two children are blocked, backtrace:<br>
first:<br>
(gdb) bt<br>
#0 malloc_init_hard () at src/jemalloc.c:626<br>
#1 0x0000000000402f75 in malloc_init (size=1) at
src/jemalloc.c:282<br>
#2 je_malloc (size=1) at src/jemalloc.c:804<br>
#3 0x00000000004011f3 in main (argc=2, argv=0x7fffa509d888) at
a.c:40<br>
<br>
second:<br>
(gdb) bt<br>
#0 0x0000003e4ea0d594 in __lll_lock_wait () from
/lib64/libpthread.so.0<br>
#1 0x0000003e4ea08e8a in _L_lock_1034 () from
/lib64/libpthread.so.0<br>
#2 0x0000003e4ea08d4c in pthread_mutex_lock () from
/lib64/libpthread.so.0<br>
#3 0x000000000040148f in malloc_mutex_lock () at
include/jemalloc/internal/mutex.h:77<br>
#4 malloc_init_hard () at src/jemalloc.c:611<br>
#5 0x0000000000402f75 in malloc_init (size=1) at
src/jemalloc.c:282<br>
#6 je_malloc (size=1) at src/jemalloc.c:804<br>
#7 0x00000000004011f3 in main (argc=2, argv=0x7fffa509d888) at
a.c:40<br>
<br>
</font><font face="΢ÈíÑźÚ">I've tested the code against
jemalloc-2.2.5 and jemalloc-3.0.0, they shows the same result.<br>
The default libc malloc does not have this issue.<br>
<br>
System info:<br>
CentOS-5.4 x86_64,<br>
<br>
Regards,<br>
tfengjun<br>
<br>
</font> </div>
</body>
</html>