2 * Copyright (C) 2014-2015 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 (* Simple forking parallel primitives. *)
24 (* Call _exit directly, ie. do not run OCaml atexit handlers. *)
25 external _exit : int -> 'a = "parallel_exit" "noalloc"
30 let rfd, wfd = pipe () in
39 let y = Printexc.catch f x in
40 (* Write the final value to the pipe. *)
41 let chan = out_channel_of_descr wfd in
54 (* Read all the output from the pipe. *)
55 let buf = Buffer.create 13 in
56 let bytes = Bytes.create 4096 in
58 let len = read rfd bytes 0 (Bytes.length bytes) in
60 Buffer.add_subbytes buf bytes 0 len;
65 let str = Buffer.to_bytes buf in
67 (* Wait for the subprocess. *)
68 match waitpid [] pid with
71 eprintf "mclu: subprocess pid %d died with exit status %d\n" pid i;
75 eprintf "mclu: subprocess pid %d died with signal %d\n" pid i;
79 eprintf "mclu: subprocess pid %d stopped with signal %d\n" pid i;
89 let iter f xs = ignore (map f xs)