1
(***********************************************************************)
5
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
7
(* Copyright 2001, 2004 Institut National de Recherche en Informatique *)
8
(* et en Automatique. All rights reserved. This file is distributed *)
9
(* under the terms of the Q Public License version 1.0. *)
11
(***********************************************************************)
15
(* The bipipe module, to launch two programs connected via stdin/stdout. *)
20
# creates two fifos (or named pipes)
23
# Launch the first argument reading from one fifo and writing to the other one.
24
$1 < p2top1 > p1top2 &
25
# Launch the second argument WRITING to one fifo and reading from
26
# the other one (order of file descriptor opening is relevant here,
27
# otherwise we get a deadlock).
28
$2 > p2top1 < p1top2 &
31
# Or better and simpler:
34
< fifo $1 | $2 > fifo &
37
#Generalisation to more than 2 processes:
42
for CMD in $*; do L="$L | $CMD"; done
43
sh -c "< fifo $L > fifo &"
48
(* The Caml version, using module Unix. *)
49
let connect_stdio proc (fdin1, fdout1) (fdin2, fdout2) =
52
Unix.dup2 fdin1 Unix.stdin;
54
Unix.dup2 fdout2 Unix.stdout;
58
let connect_bi_directional proc1 proc2 =
59
let p1 = Unix.pipe () in
60
let p2 = Unix.pipe () in
61
match Unix.fork () with
62
| 0 -> connect_stdio proc2 p1 p2
63
| _ -> connect_stdio proc1 p2 p1;;
65
let launch prog () = Unix.execv prog [| prog |];;
67
let launch_connected_processes prog1 prog2 =
68
connect_bi_directional (launch prog1) (launch prog2);;