caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Threads & Fork
@ 2005-11-22 15:39 Jonathan Bryant
  2005-11-22 15:03 ` [Caml-list] " Oliver Bandel
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Jonathan Bryant @ 2005-11-22 15:39 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1365 bytes --]

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



[-- Attachment #2: threadTest.ml --]
[-- Type: text/plain, Size: 944 bytes --]

(**
 * 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-11-23  3:35 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-22 15:39 Threads & Fork Jonathan Bryant
2005-11-22 15:03 ` [Caml-list] " Oliver Bandel
2005-11-22 15:39 ` Florian Weimer
2005-11-22 15:50   ` Oliver Bandel
2005-11-22 16:01     ` David Teller
2005-11-22 17:32     ` Alexander S. Usov
2005-11-22 20:56       ` Oliver Bandel
2005-11-23  3:34     ` Matthew Hannigan
2005-11-22 16:01 ` Christophe Raffalli
2005-11-22 20:46 ` Oliver Bandel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).