fork and pthreads
Sébastien Roret
sroret@eprocess.fr
Fri Sep 30 10:23:00 GMT 2005
Hi,
I'm having some problems using fork() with threads.
I'm using a debian linux on Intel, gdb 6.3, glibc 2.3.3 and 2.3.5
If I call forks before using threads it's ok, but not the other way.
The problem occurs only with gdb.
See below the testcase and output I got.
My question : is it a bug, or did I missed something ?
Thanks for any ideas.
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
void* start_routine(void *arg)
{
printf("start_routine\n");
}
int main()
{
pid_t pid;
pthread_t thread;
int status;
printf("pid=%d\n", getpid());
pthread_create(&thread, NULL, &start_routine, NULL);
pthread_join(thread, NULL);
perror("join");
pid = fork();
printf("fork pid = %d\n", pid);
if( pid == -1 )
{
perror( "fork" );
abort();
}
else if( pid == 0 )
{
printf("pid = %d\n", pid);
sleep(1);
return 0;
}
else
{
printf("waiting child pid = %d\n", pid);
printf("wait=%d\n", wait( &status ));
printf("status=%d\n", status );
return 0;
}
}
Then :
$ gcc pthread_fork.c -o pthread_fork -lpthread
$ gdb ./pthread_fork
GNU gdb 6.3-debian
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-linux"...Using host libthread_db library
"/lib/tls/libthread_db.so.1".
(gdb) r
Starting program: /tmp/pthread_fork
[Thread debugging using libthread_db enabled]
[New Thread 1075145376 (LWP 10278)]
pid=10278
[New Thread 1083534256 (LWP 10283)]
start_routine
[Thread 1083534256 (LWP 10283) exited]
join: Success
ptrace: No such process.
thread_db_get_info: cannot get thread info: generic error
(gdb) fork pid = 0
pid = 0
fork pid = 10284
waiting child pid = 10284
More information about the Gdb
mailing list