Pipe question

Jonathan I. Kamens jik at athena.mit.edu
Wed Mar 13 19:43:13 AEST 1991


In article <9669 at exodus.Eng.Sun.COM>, pew at grieg.Eng.Sun.COM (John Pew) writes:
|> When the parent tries to read
|> from the filter it never returns from the read.  Any suggestions would
|> be appreciated.

  Put "(void) close(fd1)" directly after your write() to the child in the
parent.

  The filter never sees EOF, so (if it's using stdio) it never gets any input
to process and send back to you, because the text you have sent it isn't large
enough to fill stdio's input buffer, nor does it have a carriage return at the
end of it to force it through (if the filter were working on a line-by-line
basis).

  If you only use the filter once (i.e. you always open the filter, send some
amount of data to it, read the result and then close the pipe to the filter),
you can solve your problem simply by closing the pipe to the filter before
trying to read data.

  If you need the filter to stay around, then you'll have to figure out some
way to get it to do line-buffering, possibly by running it on a pty instead of
on a pipe.  Or you could use

    execlp("pty", "tr", "tr", "a-z", "A-Z", 0);

if you don't want to bother to learn how to use pty's in C, and if you've got
pty's installed on your system.  (Note to all of you who are reading this in
disgust -- I had to say it, or Dan would have! :-)

  And, of course, if it's doing line-buffering, then you'll have to make sure
that your input always ends in a newline.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list