pthread+exec question

Andreas Bauer baueran at
Sun Feb 10 19:24:53 EST 2002

On Sun, Feb 10, 2002 at 11:41:27AM +1100, Eyal Lebedinsky wrote:
> I am trying to undersnatd the interaction between threads and
> exec(). The attached program demonstrates how the exec() behaves
> differently when issued from inside a thread (./p) or directly
> from main (./p x). In the former case the shell proceeds when
> the exec is issued rather than wait for the completion of the
> program.
> The problem is that the exec'ed program takes over the pid of
> the thread rather than of main(), and the original process
> exits, letting my shell proceed rather than wait.

If I recall correctly, that's a limitation of pthreads under Linux.  A
process that opens a thread can only wait for this particular thread, not
for any further opened threads:

  Thread 1 -(opens)-> Thread 2 -(opens)-> Thread 3

Thread 1 (your program here) can not see or wait for Thread 3.  So after
you've opened up Thread 2 and send it to the background, Thread 1
finished all the work and you return to shell.  Thread 2 is the only
process that may wait for Thread 3 to terminate.  Remember in Linux a
process and a thread are the same thing really.

> How do I prevent this? I have a need to exec() from inside a
> thread and not release a wait()ing parent at that point.

By using a different concept for your program.  It's been a while since I
used pthreads, but that's all I can think of right now.  (Someone correct
me please, in case.)

> I looked into process groups and got no solution either.

See if you can buy or borrow Butenhof's "Programming with POSIX Threads".
It's a nice introduction to pthreads.

Hope this helped a bit, though I am no pthreads expert :-)


Andreas Bauer, baueran at,
"Should've bought a Holden, Frank!" -- Ozzie in Bad Taste

More information about the linux mailing list