Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Capturing STDOUT without subshells or file I/O



On Mon, Sep 17, 2018 at 2:01 PM, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> On Mon, Sep 17, 2018 at 10:46 PM, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
>>
>> If you could call pipe(2), you'd be able to do
>> .
>>     printf foo >&$w
>>     read bar <&$r
>> .
>> without forking (where $r,$w were returned by pipe(2)).
>
> That's just a deadlock-by-design.

To expand upon that a bit, the pipe created by the system has a
limited (and generally indeterminate) amount of buffer space, so if
the print side ever sends more than that amount of data at once, it
will block forever, and the read side will never get a chance to
execute.  This is WHY (printf '%.2f' "3.4" | read var) creates a
subshell on the left, so that the read and write are simultaneous and
independent.

You could safely do it if you only wrote and read one byte at a time,
but you're not going to pull that off with printf and formatting.

If zsh exposed a way to make the writes non-blocking, you could do it
without deadlock but with possible data loss, but that's not going to
help you with zsh 5.1 and earlier.



Messages sorted by: Reverse Date, Date, Thread, Author