Creating processes

Ceri Davies ceri at submonkey.net
Fri Sep 29 12:55:49 BST 2006


--u+FGODhbLwgAeSOU
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Sep 29, 2006 at 10:05:32AM +0000, Michael Abbott wrote:
> Here is an interesting *nix problem: it's not FreeBSD specific, but I hop=
e=20
> it's interesting enough for all that.
>=20
> In the old days I was familiar with the Win32 CreateProcess() api, and I'=
m=20
> wondering how to emulate this reliably in the *nix world.  There is a=20
> mythology that the following is sufficient (error testing ignored for=20
> simplicity):
>=20
> 	if(fork() =3D=3D 0)  exect(path, argv, envp);
>=20
> I promise you, this is by no means enough!  Let me enumerate the problems=
=20
> that I have encountered so far: I'm wondering if there is a standard=20
> solution to this.

Good question; I'll throw in some some small comments and leave the
dirty work for someone else (partly because I don't know the answer and
partly because I'm supposed to be doing something else and the missus is
shooting me daggers).

> 1. First of all (mostly this is trivial), the newly exec'd process is a=
=20
> child of the creator, which can be a bit undesirable if I don't want to=
=20
> keep track of it: I'll have lots of zombie processes if I don't use the=
=20
> old double-fork trick:
>=20
> 	pid_t New =3D fork();
> 	if (New =3D=3D 0)
> 	    if (fork() =3D=3D 0)
> 	    {
> 	        ... do all the other stuff below then exec ...
> 	    }
> 	    else
> 	        _exit(0);
> 	else
> 	    waitpid(New, NULL, 0);
>=20
> Damn.  This is already looking bad, and I'm only just getting started.

On FreeBSD (>=3D 5.0) and Solaris you can ignore SIGCHLD to avoid zombies.
Yes, that doesn't help you :)

> 4. Signals?  I can't find out what exec is defined to do about signals=20
> (haven't done the necessary experiments yet), so I'd better restore the=
=20
> default signal handlers.  I haven't found a portable way to do this:
>=20
> 	for (int i =3D 0; i < 32; i ++)   // 32?!  What should this be?
> 	    signal(i, SIG_DFL);

NSIG from sys/signal.h.

> 7. What about open sockets?  Hopefully step (3) above when I closed all m=
y=20
> handles suffices (I'm a bit rusty on the socket API).

It does.

> Now what really freaks me out about this is that this seems to me to be a=
=20
> pretty standard problem: I want to fire off a "top level" process from=20
> within my application (in my original application I needed to kill and=20
> restart a daemon that's rather fussy about how it's started), and yet the=
=20
> work required between fork() and exec() seems both lengthy and rather=20
> unbounded.

Agreed.

Ceri
--=20
That must be wonderful!  I don't understand it at all.
                                                  -- Moliere

--u+FGODhbLwgAeSOU
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFHQnFocfcwTS3JF8RAuq1AKCkpeT3BNIsu+moRMkCpM5Ey7iBnQCeJ1Ji
igYg+CE3MLyTuCnTd0Ak6EU=
=nFZz
-----END PGP SIGNATURE-----

--u+FGODhbLwgAeSOU--




More information about the Ukfreebsd mailing list