Fork and Execl

Ken Latham latham at bsdpkh.UUCP
Sat Feb 22 08:12:08 AEST 1986


> I've got a question ...
>....but where does the execution of the child process continue from ?
> In other words, if I wanted to put an execl() in the child to overwrite
> the child, where would it go ?

A common phrase used is:

......PARENT

if ( (childid = fork()) == 0)
{
	DO execl();
}
if ( childid != -1 )
	werror=wait(&status);  /* status being a status structure */

REST OF PARENT ....;


( Yes, I know it's not standard indentation ..... :-) )

You get two complete copies of the process executing from the same point
in process with the same data, stack ... well everything EXCEPT the return
code from fork().  The parent gets the PID of the kid, the kid gets 0.
( other restrictions may apply see fork(2), void where prohibited)
the execl() will never be executed if the fork() call fails.

If you are using OS9 however you get a NEW process as a child ...
never mind I'm getting carried away.

Be careful the above will NOT work if the CHILD doesn't do an exec()
of some kind ( or exit inside the if ).  The child would execute
what was inside the if ... drop out .... execute the parent code...
etc.  Although you may WANT to do this some times!


			Ken Latham, AT&T-IS (via AGS Inc.), Orlando , FL

			uucp: ihnp4!bsdpkh!latham



More information about the Comp.lang.c mailing list