On 11 Jul 2003, Alex Shnitman wrote:

> 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. IfI 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.
>

Here's the output from the program on my machine:

Father
Sweet child in time: 8826
you'll see the light

It seems the exec is happening. I'm using kernel 2.4.21-0.18mdk with
glibc-2.3.1-10mdk on a Mandrake 9.1 system running on a P3 Machine.

Regards,

        Shlomi Fish

> 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 2863 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]
>



----------------------------------------------------------------------
Shlomi Fish        [EMAIL PROTECTED]
Home Page:         http://t2.technion.ac.il/~shlomif/

There's no point in keeping an idea to yourself since there's a 10 to 1
chance that somebody already has it and will share it before you.


=================================================================
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]

Reply via email to