Date: Sat, 16 Jan 2016 04:35:58 +0000 From: "Montgomery-Smith, Stephen" <stephen@missouri.edu> To: "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org> Subject: Re: How to send EOF to the popen(3) pipe? Message-ID: <5699C8AB.7070006@missouri.edu> In-Reply-To: <5699BAC9.3060407@rawbw.com> References: <5699BAC9.3060407@rawbw.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On 01/15/2016 09:36 PM, Yuri wrote: > Is there a way to send the EOF to popen(3) pipe? >=20 > Imagine the situation when the child process works in a stream fashion, > processes objects one by one, and stops on EOF from stdin. One has to be > able to send EOF to get to the end of the last processed object. > Otherwise reading from the descriptor will generally block. >=20 Maybe I am displaying my ignorance. But wouldn't you do this by invoking the function pclose? > Linux man page says that popen is unidirectional on Linux. But FreeBSD > supports bi-directional popen. My memory of using this was that this could gridlock because of buffering. Suppose process A popens a process B. A sends a message to B. But the end of the message often never arrives unless A also does a fflush. So then B just sits there waiting. If A does do a fflush, then B knows to reply back. But B also has to do a fflush after it has replied. If B happens to be a piece of code you haven't written yourself, there is nothing you can do to force it to do a fflush. If you run B in a terminal, you won't notice this behavior, because the stdio package automatically does a fflush at the end of every new line if it is writing out to a terminal. You can get around this by using pseudo-terminals to connect process A to process B, but I don't think popen is this sophisticated. But you could probably do this using a script called unbuffer: http://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe This is included in the lang/expect port, but for some reason this script is not in /usr/local/bin but in /usr/local/share/expect But now I see what your original problem was. When you open a bidirectional popen, you really need to have two separate pclose command - one for each direction. You want to close the input stream to the program before you close the output stream to the program. Yes, I think un unsophisticated bidirectional popen is fairly useless. I think that is why Linux never bothered to implement it. Look at the first answer to this question: http://stackoverflow.com/questions/6171552/popen-simultaneous-read-and-writ= e
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5699C8AB.7070006>