From owner-freebsd-hackers@freebsd.org Sat Jan 16 04:37:11 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id AA239A6D794 for ; Sat, 16 Jan 2016 04:37:11 +0000 (UTC) (envelope-from stephen@missouri.edu) Received: from um-nip4-missouri-out.um.umsystem.edu (um-nip4-missouri-out.um.umsystem.edu [198.209.49.177]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (Client CN "um-tip1.um.umsystem.edu", Issuer "InCommon RSA Server CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 481131C13 for ; Sat, 16 Jan 2016 04:37:10 +0000 (UTC) (envelope-from stephen@missouri.edu) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2CRCAB+yJlW/8aeoM9egzpSLEIFiFC1HCSFawKBLzwQAQEBAQEBAX8LhDQBAQEDAXgGCwIBCBgJFg8JAwIBAgEgJQIEDQgBAYgPCA69MgGDAAwBIItUhFWEaAWXGQGFRoVdhBgWNIclhTSKbINxOSuECoZhAYEHAQEB X-IPAS-Result: A2CRCAB+yJlW/8aeoM9egzpSLEIFiFC1HCSFawKBLzwQAQEBAQEBAX8LhDQBAQEDAXgGCwIBCBgJFg8JAwIBAgEgJQIEDQgBAYgPCA69MgGDAAwBIItUhFWEaAWXGQGFRoVdhBgWNIclhTSKbINxOSuECoZhAYEHAQEB Received: from um-tcas2.um.umsystem.edu ([207.160.158.198]) by um-nip4-exch-relay.um.umsystem.edu with ESMTP; 15 Jan 2016 22:36:00 -0600 Received: from UM-MBX-N02.um.umsystem.edu ([169.254.5.100]) by UM-TCAS2.um.umsystem.edu ([207.160.158.198]) with mapi id 14.03.0266.001; Fri, 15 Jan 2016 22:35:59 -0600 From: "Montgomery-Smith, Stephen" To: "freebsd-hackers@freebsd.org" Subject: Re: How to send EOF to the popen(3) pipe? Thread-Topic: How to send EOF to the popen(3) pipe? Thread-Index: AQHRUBBE+GmWWEt160m/VZ+mWUx+kJ798zuA Date: Sat, 16 Jan 2016 04:35:58 +0000 Message-ID: <5699C8AB.7070006@missouri.edu> References: <5699BAC9.3060407@rawbw.com> In-Reply-To: <5699BAC9.3060407@rawbw.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: user-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 x-originating-ip: [207.160.158.194] Content-Type: text/plain; charset="Windows-1252" Content-ID: <3DFE87A541E68B4CB727932DF854D0F6@missouri.edu> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Jan 2016 04:37:11 -0000 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