caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jonathan Bryant <jtbryant@valdosta.edu>
To: caml-list@yquem.inria.fr
Subject: Threads & Fork
Date: Tue, 22 Nov 2005 10:39:41 -0500	[thread overview]
Message-ID: <1132673981.13170.17.camel@starlight> (raw)

[-- 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

             reply	other threads:[~2005-11-22 14:38 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-22 15:39 Jonathan Bryant [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1132673981.13170.17.camel@starlight \
    --to=jtbryant@valdosta.edu \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).