* fork question... @ 2005-02-25 10:20 Micha 2005-02-25 11:02 ` [Caml-list] " Keith Wansbrough 0 siblings, 1 reply; 3+ messages in thread From: Micha @ 2005-02-25 10:20 UTC (permalink / raw) To: caml-list 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 :-) Michael let establish_server server_fun sockaddr = let domain = domain_of sockaddr in let sock = Unix.socket domain Unix.SOCK_STREAM 0 in Unix.bind sock sockaddr ; Unix.listen sock 3; while true do let (s, caller) = Unix.accept sock in match Unix.fork() with 0 -> if Unix.fork() <> 0 then exit 0 ; (* <<<< ???? *) let inchan = Unix.in_channel_of_descr s and outchan = Unix.out_channel_of_descr s in server_fun inchan outchan ; close_in inchan ; close_out outchan ; exit 0 | id -> Unix.close s; ignore(Unix.waitpid [] id) done ;; ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] fork question... 2005-02-25 10:20 fork question Micha @ 2005-02-25 11:02 ` Keith Wansbrough 2005-02-25 13:26 ` Christophe TROESTLER 0 siblings, 1 reply; 3+ messages in thread From: Keith Wansbrough @ 2005-02-25 11:02 UTC (permalink / raw) To: micha-1; +Cc: caml-list > 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). HTH. --KW 8-) ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] fork question... 2005-02-25 11:02 ` [Caml-list] " Keith Wansbrough @ 2005-02-25 13:26 ` Christophe TROESTLER 0 siblings, 0 replies; 3+ messages in thread From: Christophe TROESTLER @ 2005-02-25 13:26 UTC (permalink / raw) To: Keith.Wansbrough; +Cc: micha-1, caml-list On Fri, 25 Feb 2005, Keith Wansbrough <Keith.Wansbrough@cl.cam.ac.uk> wrote: > > It's a Unix question, not an OCaml one. The reason is to completely The following may is really nice (in French only unfortunately): "Principes et Programmation des Systèmes d'exploitation" http://cristal.inria.fr/~remy/poly/system/ Maybe -- if the authors agree -- some people may be interested to translate it to english ? Cheers, ChriS ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-02-25 13:28 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2005-02-25 10:20 fork question Micha 2005-02-25 11:02 ` [Caml-list] " Keith Wansbrough 2005-02-25 13:26 ` Christophe TROESTLER
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox