From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 1BDE1BB9C for ; Tue, 22 Nov 2005 15:38:29 +0100 (CET) Received: from mailx.valdosta.edu (mailx.valdosta.edu [168.18.130.251]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jAMEcSJA005359 for ; Tue, 22 Nov 2005 15:38:28 +0100 Received: from blazemail.valdosta.edu (blazemail.valdosta.edu [168.18.130.208]) by mailx.valdosta.edu (8.13.4/8.13.4) with ESMTP id jAMEcORU002733 for ; Tue, 22 Nov 2005 09:38:25 -0500 (EST) (envelope-from jtbryant@valdosta.edu) Received: from starlight.valdosta.edu (starlight.valdosta.edu [168.18.148.146]) by blazemail.valdosta.edu (iPlanet Messaging Server 5.2 HotFix 2.04 (built Feb 8 2005)) with ESMTP id <0IQD00LTP200GC@blazemail.valdosta.edu> for caml-list@yquem.inria.fr; Tue, 22 Nov 2005 09:38:24 -0500 (EST) Date: Tue, 22 Nov 2005 10:39:41 -0500 From: Jonathan Bryant Subject: Threads & Fork To: caml-list@yquem.inria.fr Reply-To: jtbryant@valdosta.edu Message-id: <1132673981.13170.17.camel@starlight> MIME-version: 1.0 X-Mailer: Ximian Evolution 1.4.5 (1.4.5-17) Content-type: multipart/mixed; boundary="Boundary_(ID_Vup0SQ7qXL9Pwe5Hfrb6+w)" X-PMX-Version: 5.0.2.153301, Antispam-Engine: 2.0.3.2, Antispam-Data: 2005.11.22.11 X-Miltered: at nez-perce with ID 43832D64.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; threads:01 threads:01 descriptors:01 intern:01 ocamlopt:01 -threads:01 threadtest:01 cmxa:01 cmxa:01 threadtest:01 printf:01 printf:01 stdout:01 stdout:01 waitpid:01 X-Attachments: name="threadTest.ml" name="threadTest.ml" X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 --Boundary_(ID_Vup0SQ7qXL9Pwe5Hfrb6+w) Content-type: text/plain Content-transfer-encoding: 7BIT I'm confused as to why the attached code hangs. My understanding of Unix.fork () is that it completely clones the current process, which in my understanding, clones the processes's threads as well. Apparently, though, that is not the case, because I can't join the thread in both the parent and the child. Are threads created external to the process? I've played around with the source code and I've found that I can only join the thread in the parent process (i.e., if I remove the Thread.join from the parent process, it still hangs). Can someone explain this behavior? I'm needing to create several multi-threaded worker processes, and it would be much easier if I could simply create one, have it wait for a signal, and, on that signal, fork a clone of itself. Also, I'm needing to fork a process from in the middle of a multi-threaded application, so I assumed that my new process needed to join all of the threads and close all of the file descriptors. Is this not the case? -- --Jonathan Bryant jtbryant@valdosta.edu Student Intern Unix System Operations VSU Information Technology "Das Leben ohne Music ist einfach ein Irrtum, eine Strapaze, ein" Exil." ("Life without music is simply an error, a pain, an exile.") --Frederich Nietzsche "The three cardinal values of a programmer are laziness, impatience, and hubris." --Perl Man Page --Boundary_(ID_Vup0SQ7qXL9Pwe5Hfrb6+w) Content-type: text/plain; name=threadTest.ml; charset= Content-transfer-encoding: 7BIT Content-disposition: attachment; filename=threadTest.ml (** * A simple thread + fork test. Compile as: * ocamlopt.opt -threads -o threadTest unix.cmxa threads.cmxa threadTest.ml *) let () = (* Create a thread to join *) let t = Thread.create (fun x -> ()) () in Printf.printf "Thread created.\n"; flush stdout; (* Fork a new process *) match Unix.fork () with | 0 -> (* Join the thread in the child process and exit *) Printf.printf "Child process created.\n"; flush stdout; Thread.join t; Printf.printf "Child joined thread.\n"; flush stdout; exit 0 | _ as pid -> (* Join the thread and the child in the parent process and exit *) Printf.printf "Parent process continuing.\n"; flush stdout; Thread.join t; Printf.printf "Parent joined thread.\nWaiting on child.\n"; flush stdout; let _ = Unix.waitpid [] pid in Printf.printf "Child joined. Exiting.\n"; exit 0 --Boundary_(ID_Vup0SQ7qXL9Pwe5Hfrb6+w)--