Hi,
I have a problem executing a fork() and exec() from a threaded
application. I'm including a minimal test case below that demonstrates
the problem. I must be missing something. If I don't create the thread
before forking, the exec works fine. If I do create the thread, the exec
doesn't exec, and it doesn't return with an error either -- simply
nothing happens. Trying to debug this with gdb by attaching to the child
process before the exec (I put a while loop there so that I can catch
it) doesn't work -- GDB gives internal errors.
Also, an interesting thing is that if I do the exec from the father
process instead of the child, it does work. But then of course I lose
the thread that I started.
Any idea whatsoever? Thanks in advance.
Below is the code.
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
void *thread(void* arg)
{
while(1)
sleep(1);
return NULL;
}
int main()
{
pthread_t tid;
int rv;
if(rv = pthread_create(&tid, NULL, thread, NULL)) {
printf("Can't create thread: %d\n", rv);
exit(1);
}
switch(fork()) {
case -1:
perror("fork");
exit(1);
break;
case 0:
printf("Sweet child in time: %d\n", getpid());
execlp("echo", "echo", "you'll see the light", NULL);
perror("execlp");
exit(1);
break;
default:
printf("Father\n");
pause();
break;
}
return 0;
}
--
Alex Shnitman <[EMAIL PROTECTED]>
http://www.hectic.net/ UIN 188956
PGP 0xEC5D619D / E1 F2 7B 6C A0 31 80 28 63 B8 02 BA 65 C7 8B BA
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]