English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
fork question...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-02-25 (11:04)
From: Keith Wansbrough <Keith.Wansbrough@c...>
Subject: Re: [Caml-list] fork question...
> hi,
> below is a code piece of the ocaml-book. What I don't understand is why is 
> there the second fork() ? Maybe that's a Unix question, but I saw it the 
> first time in the ocaml book :-)

It's a Unix question, not an OCaml one.  The reason is to completely
dissolve the connection between the parent process and the grandchild.
Normally, the parent is responsible for cleaning up after the child -
in particular, wait()ing on it to obtain its return status.  In
situations like this, where establish_server doesn't want to wait for
server_fun to complete, you do the following:

* fork()
* in parent, wait for child to exit
* in child, fork() again
* in grandchild, do your work
* in child, exit.  This means the grandchild is orphaned, and "init"
  (PID 1) adopts it and takes responsibility for cleaning up after it.
* in parent, clean up after child
* in parent, continue

If you don't do this, your process table fills up with zombie
processes (processes that have completed, but haven't yet been
wait()ed for.

See any standard Unix text (eg Stevens, Unix Network Programming).


--KW 8-)