For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2005 > Two-way pipe on an awk process









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Two-way pipe on an awk process
Aaron S. Hawley

2005-01-07, 3:56 pm

[This started as a call for help, but now is for discussion.]

Using two-way pipes in Gawk with the separate program being an Awk program
(Gawk process) one will get buffering problems. The result is that
currently Gawk needs pseydo-ttys (ptys) for two-communication to another
Awk program.

Here's a simple example involving an Awk version of cat(1) called cat.awk,
which is used in a simple two-way i/o example script called 2way-io.awk,
which really just functions as a simple unix cat(1), also.

$ cat cat.awk
{
print $0
}

$ cat 2way-io.awk
BEGIN {
prog = "gawk -f ./cat.awk"; ## pseudo-ttys not needed when /bin/cat
PROCINFO[prog, "pty"] = 1; ## use pseydo-ttys
}

{
print $0 |& prog
prog |& getline result;
print result;
}

END {
close(prog);
}

Usage

$ gawk -f cat.awk
> foo

-| foo

$ gawk -f 2way-io.awk
> foo

-| foo

Here's the documentation I read on Two-way pipes in the GNU Awk User
Manual:
<http://www.gnu.org/software/gawk/ma...ay-I_002fO.html>

/a
John DuBois

2005-01-07, 8:55 pm

In article <Pine.A41.4.58.0501071121001.106090@elk.uvm.edu>,
Aaron S. Hawley <Aaron.Hawley@uvm.edu> wrote:
>[This started as a call for help, but now is for discussion.]
>
>Using two-way pipes in Gawk with the separate program being an Awk program
>(Gawk process) one will get buffering problems. The result is that
>currently Gawk needs pseydo-ttys (ptys) for two-communication to another
>Awk program.


You can generalize this to "using pipes with any program that uses stdio-style
buffering with no way to control that buffering is liable to result in
buffering problems". A gawk program, at least, can eliminate its problems by
fflush()ing prints to pipes. If you add "fflush()" after the print in cat.awk,
it'll work without needing the pty in 2way-io.awk. In many other awks you can
also flush the standard output, though it is clumsier - print to "/dev/stdout",
and then close("/dev/stdout") after each print:

{
print $0 > "/dev/stdout"
close("/dev/stdout")
}

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com