pthread+exec question

Eyal Lebedinsky eyal at
Mon Feb 11 21:37:34 EST 2002

Damien Elmes wrote:
> if you want to ensure that you keep the main pid of the process (and thus avoid
> falling through), the simplest way i can see of doing this is to refactor your
> program to check for a flag that's set when the child thread ends, and roll the
> exec behavior into the main one. this may not be a very elegant way to go about
> it, but threads and process control (such as signals) have never been
> particularly elegant.

Yes, this was the clear solution from the start, but I thought I could
it by doing something right. No such luck.

I am now convinced that this "hack" is actually the only way to do it. I
am lucky in that:
	- I am in control of all the threads (including main)
	- my need is to exec from a first generation thread
So I can (actually, I did this and it works) do it. If any of the above
was not the case then I would be in real trouble.

My approach was to write an exec_delay() function that saves the <path,
envp> in a safe place and then does a pthread_exit(), which falls into
pthread_join() which notices the saved data and executes the exec()
The original exec() was already in a wrapper so the implementation was
straight forward. One benefit is that now the main program does have an
opportunity to cleanup/shutdown before doing the exec(), and the very
exit() is what the exec() should replace.

If nothing else, I now know more about fork/exec/wait interactions with
POSIX threads that I ever wanted to know.

Thanks everyone.

Eyal Lebedinsky (eyal at <>

More information about the linux mailing list