Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous pipe in command line

Tags:

linux

bash

Say I have two commands: foo and bar.

I want to execute both such that the output of foo is "redirected" to the input of bar. NOT only that though, I also want each byte written to foo's output stream to be directly/instantly written to bar's input stream.

If I use the command:

foo | bar

then the foo must terminate/exit before bar starts reading its input stream. I want bar to start reading its input stream in the same time the foo writes its output stream.

This answer here, if I understand correctly, states that there is a limit in the number of bytes buffered by the pipe and when they are filled then the pipe is "flushed" to the input stream of bar Maybe this limit can be reduced to '1' somehow?

like image 908
gthanop Avatar asked Oct 25 '17 18:10

gthanop


1 Answers

You wrote:

foo must terminate/exit before bar starts reading its input stream. I want bar to start reading its input stream in the same time the foo writes its output stream.

The assumption is not true. foo is not required to terminate before it's output gets passed to bar. True is that the glibc (assuming that both foo and bar are linked to it) performs output buffering. Unless the output of foo goes to a terminal, the output gets written to a buffer and this buffer gets flushed when it reaches a certain size. If the output of foo is so small that the buffer size won't get reached, then yes, the buffer will get flushed once foo exits.

You can adjust the buffer size using the stdbuf command. The following command won't buffer foo's output:

stdbuf -o0 foo | bar
like image 118
hek2mgl Avatar answered Nov 16 '22 10:11

hek2mgl