diff options
author | graehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-07-16 05:05:57 +0000 |
---|---|---|
committer | graehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-07-16 05:05:57 +0000 |
commit | 3afbbcdfaa7f0c30d4c6a1fd374a7d4c0986818c (patch) | |
tree | b78948d7eb63a9b72a6eaf5bb15ae13b302099e3 /vest | |
parent | d5d7820830c2a2beda775a674f39fc8506f8a8f6 (diff) |
line_mediator default works for parallel via sentserver - directly cross stdin/stdout of the two commands
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@284 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'vest')
-rwxr-xr-x | vest/line_mediator.pl | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/vest/line_mediator.pl b/vest/line_mediator.pl index 5ae13aa4..f3c6dbf1 100755 --- a/vest/line_mediator.pl +++ b/vest/line_mediator.pl @@ -7,6 +7,8 @@ use strict; use IPC::Open2; +use POSIX qw(pipe dup2 STDIN_FILENO STDOUT_FILENO); +#use IO::Handle; $,=' '; my $quiet=$ENV{QUIET}; sub info { @@ -14,24 +16,53 @@ sub info { } my @c1; -do { - push @c1,shift -} while $c1[$#c1] ne '--'; +if (scalar @ARGV) { + do { + push @c1,shift + } while $c1[$#c1] ne '--'; +} pop @c1; my @c2=@ARGV; -(scalar @c1 && scalar @c2) || die "usage: $0 cmd1 args -- cmd2 args; hooks up two processes, 2nd of which has one line of output per line of input, expected by the first, which starts off the communication. crosses stdin/stderr of cmd1 and cmd2 line by line (both must flush on newline and output. cmd1 initiates the conversation (sends the first line). QUIET env var suppresses debugging output."; +(scalar @c1 && scalar @c2) || die "usage: $0 cmd1 args -- cmd2 args; hooks up two processes, 2nd of which has one line of output per line of input, expected by the first, which starts off the communication. crosses stdin/stderr of cmd1 and cmd2 line by line (both must flush on newline and output. cmd1 initiates the conversation (sends the first line). QUIET=1 env var suppresses debugging output. default: attempts to cross stdin/stdout of c1 and c2 directly (via two unidirectional posix pipes created before fork). env SERIAL=1: (no parallelism possible) but lines exchanged are logged unless QUIET."; + info("1 cmd:",@c1,"\n"); info("2 cmd:",@c2,"\n"); -my ($R1,$W1,$R2,$W2); -my $c1p=open2($R1,$W1,@c1); # Open2 R W backward from Open3. -my $c2p=open2($R2,$W2,@c2); -select $W2; -$|=1; -while(<$R1>) { - info("1:",$_); - print $_; - $_=<$R2>; - last unless defined $_; - info("2:",$_); - print $W1 $_; + +if ($ENV{SERIAL}) { + my ($R1,$W1,$R2,$W2); + my $c1p=open2($R1,$W1,@c1); # Open2 R W backward from Open3. + my $c2p=open2($R2,$W2,@c2); + select $W2; + $|=1; + while(<$R1>) { + info("1:",$_); + print $_; + $_=<$R2>; + last unless defined $_; + info("2:",$_); + print $W1 $_; + } +} else { + my @rw1=POSIX::pipe(); + my @rw2=POSIX::pipe(); + my $pid=undef; + $SIG{CHLD} = sub { wait }; +# close STDIN; +# close STDOUT; + while (not defined ($pid=fork())) { + sleep 1; + } +# info(STDOUT_FILENO,STDIN_FILENO,"\n"); + POSIX::close(STDOUT_FILENO); + POSIX::close(STDIN_FILENO); + if ($pid) { + POSIX::dup2($rw1[1],STDOUT_FILENO); + POSIX::dup2($rw2[0],STDIN_FILENO); + exec @c1; + } else { + POSIX::dup2($rw2[1],STDOUT_FILENO); + POSIX::dup2($rw1[0],STDIN_FILENO); + exec @c2; + } + while (wait()!=-1) {} } |