Reputation: 8131
Here is shell commands sequence with comments
#cd `mktemp -d`
mkfifo i
exec 3<>i # keep pipe open
bash <i & # run bash interpreter in background, use -x option to see what and when is executed
echo 'echo X' >i # writes X immediately
echo 'if echo -n A; then' >i # waits for input till end of if-statement
echo 'echo B; fi' >i # writes AB
echo 'echo C; sleep 7; echo D' >i # does not wait on stdin while sleep
echo 'exit' >i # also kill $(jobs -p)
May be except
will help?
Upvotes: 2
Views: 1198
Reputation: 157947
The behaviour is the same as within an interactive terminal. Just try:
you@yourpc ~ sleep 3<enter>
ls<enter>
wait...
you@yourpc ~ ls<inserted by bash>
file1 file2 file3 ...
The behaviour of a pipe in Linux is described under man 7 pipe
I/O on pipes and FIFOs
...
If a process attempts to read from an empty pipe, then read(2) will block until data is available. If a process attempts to write to a full pipe (see below), then write(2) blocks until sufficient data has been read from the pipe to allow the write to complete.
...
Pipe capacity
A pipe has a limited capacity. If the pipe is full, then a write(2) will block or fail, depending on whether the O_NON‐ BLOCK flag is set (see below). Different implementations have different limits for the pipe capacity. Applications should not rely on a particular capacity: an application should be designed so that a reading process consumes data as soon as it is available, so that a writing process does not remain blocked.
That means the writing end of a fifo has no information about when the reader has read or processed the input.
Upvotes: 1