~ubuntu-branches/ubuntu/hardy/ocaml-doc/hardy

« back to all changes in this revision

Viewing changes to examples/camltktutorial/servers/bipipe.ml

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2007-09-08 01:49:22 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070908014922-lvihyehz0ndq7suu
Tags: 3.10-1
* New upstream release.
* Removed camlp4 documentation since it is not up-to-date.
* Updated to standards version 3.7.2, no changes needed.
* Updated my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(***********************************************************************)
2
 
(*                                                                     *)
3
 
(*                           Objective Caml                            *)
4
 
(*                                                                     *)
5
 
(*               Pierre Weis, projet Cristal, INRIA Rocquencourt       *)
6
 
(*                                                                     *)
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.                *)
10
 
(*                                                                     *)
11
 
(***********************************************************************)
12
 
 
13
 
(* $Id: Exp *)
14
 
 
15
 
(* The bipipe module, to launch two programs connected via stdin/stdout. *)
16
 
 
17
 
(* The shell version:
18
 
 
19
 
#!/bin/sh
20
 
# creates two fifos (or named pipes)
21
 
mknod p1top2 p
22
 
mknod p2top1 p
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 &
29
 
rm -f p1top2 p2top1
30
 
 
31
 
# Or better and simpler:
32
 
 
33
 
mknod fifo p
34
 
< fifo $1 | $2  > fifo &
35
 
rm -f fifo
36
 
 
37
 
#Generalisation to more than 2 processes:
38
 
 
39
 
mknod fifo p
40
 
L="$1"
41
 
shift
42
 
for CMD in $*; do L="$L | $CMD"; done
43
 
sh -c "< fifo $L > fifo &"
44
 
rm -f fifo
45
 
 
46
 
*)
47
 
 
48
 
(* The Caml version, using module Unix. *)
49
 
let connect_stdio proc (fdin1, fdout1) (fdin2, fdout2) =
50
 
  Unix.close fdout1;
51
 
  Unix.close fdin2;
52
 
  Unix.dup2 fdin1 Unix.stdin;
53
 
  Unix.close fdin1;
54
 
  Unix.dup2 fdout2 Unix.stdout;
55
 
  Unix.close fdout2;
56
 
  proc ();;
57
 
 
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;;
64
 
 
65
 
let launch prog () = Unix.execv prog [| prog |];;
66
 
 
67
 
let launch_connected_processes prog1 prog2 =
68
 
  connect_bi_directional (launch prog1) (launch prog2);;