f



Question on pipes

Hi All,

When you pipe something from opne program to anothyer

     echo abc | ReadAPipe

Does the first program terminate before the second program
grabs the data?

Is both are running and the second is just reading what
comes along, how does the second program know when the
first program is no longer sending data?

Many thanks,
-T
0
T
11/13/2016 9:14:52 AM
comp.os.linux.misc 33599 articles. 1 followers. amosa69 (78) is leader. Post Follow

8 Replies
692 Views

Similar Articles

[PageSpeed] 0

On 13/11/16 09:14, T wrote:
> Hi All,
>
> When you pipe something from opne program to anothyer
>
>     echo abc | ReadAPipe
>
> Does the first program terminate before the second program
> grabs the data?
>
> Is both are running and the second is just reading what
> comes along, how does the second program know when the
> first program is no longer sending data?
>
> Many thanks,
> -T

http://man7.org/linux/man-pages/man7/pipe.7.html

I/O on pipes and FIFOs
======================
The only difference between pipes and FIFOs is the manner in which they 
are created and opened.  Once these tasks have been accomplished, I/O on 
pipes and FIFOs has exactly the same semantics.

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.  Nonblocking I/O is 
possible by using the fcntl(2) F_SETFL operation to enable the 
O_NONBLOCK open file status flag.

The communication channel provided by a pipe is a byte stream: there is 
no concept of message boundaries.

If all file descriptors referring to the write end of a pipe have been 
closed, then an attempt to read(2) from the pipe will see end-of-file 
(read(2) will return 0).  If all file descriptors referring to the read 
end of a pipe have been closed, then a write(2) will cause a SIGPIPE 
signal to be generated for the calling process.  If the calling process 
is ignoring this signal, then write(2) fails with
the error EPIPE.  An application that uses pipe(2) and fork(2) should 
use suitable close(2) calls to close unnecessary duplicate file 
descriptors; this ensures that end-of-file and SIGPIPE/EPIPE are 
delivered when appropriate.

It is not possible to apply lseek(2) to a pipe.
--------------------------------------------------------

So basically the reading program will either block (no data available) 
read some data (data available) or return 0 (pipe is closed).

It's up to the program to exit when it's read all the data there is.


-- 
“It is hard to imagine a more stupid decision or more dangerous way of 
making decisions than by putting those decisions in the hands of people 
who pay no price for being wrong.”

Thomas Sowell
0
The
11/13/2016 10:10:12 AM
T <T@invalid.invalid> wrote:
>Hi All,
>
>When you pipe something from opne program to anothyer
>
>     echo abc | ReadAPipe
>
>Does the first program terminate before the second program
>grabs the data?
>
>Is both are running and the second is just reading what
>comes along, how does the second program know when the
>first program is no longer sending data?
>
>Many thanks,
>-T

The second program can detect an EOF on stdin.

-- 
Floyd L. Davidson                         http://www.apaflo.com/
Utqiagvik (Barrow, Alaska)                      floyd@apaflo.com
0
floyd
11/13/2016 10:13:25 AM
On 11/13/2016 02:13 AM, Floyd L. Davidson wrote:
> T <T@invalid.invalid> wrote:
>> Hi All,
>>
>> When you pipe something from opne program to anothyer
>>
>>     echo abc | ReadAPipe
>>
>> Does the first program terminate before the second program
>> grabs the data?
>>
>> Is both are running and the second is just reading what
>> comes along, how does the second program know when the
>> first program is no longer sending data?
>>
>> Many thanks,
>> -T
>
> The second program can detect an EOF on stdin.
>

What happens if there is no data in the pipe?

0
T
11/13/2016 10:30:18 AM
On 13/11/16 10:30, T wrote:
> On 11/13/2016 02:13 AM, Floyd L. Davidson wrote:
>> T <T@invalid.invalid> wrote:
>>> Hi All,
>>>
>>> When you pipe something from opne program to anothyer
>>>
>>>     echo abc | ReadAPipe
>>>
>>> Does the first program terminate before the second program
>>> grabs the data?
>>>
>>> Is both are running and the second is just reading what
>>> comes along, how does the second program know when the
>>> first program is no longer sending data?
>>>
>>> Many thanks,
>>> -T
>>
>> The second program can detect an EOF on stdin.
>>
>
> What happens if there is no data in the pipe?
>
read blocks.
Until there is, or EOF.


-- 
"What do you think about Gay Marriage?"
"I don't."
"Don't what?"
"Think about Gay Marriage."

0
The
11/13/2016 10:35:58 AM
On 2016-11-13, T <T@invalid.invalid> wrote:
> Hi All,
>
> When you pipe something from opne program to anothyer
>
>      echo abc | ReadAPipe
>
> Does the first program terminate before the second program
> grabs the data?

No. 
(echo hi;sleep 3; echo bye)|cat


>
> Is both are running and the second is just reading what
> comes along, how does the second program know when the
> first program is no longer sending data?

When the pipe gets closed, by the first program closing it, or dying, or
whatever.

>
> Many thanks,
> -T
0
William
11/14/2016 8:52:28 AM
On 2016-11-13, T <T@invalid.invalid> wrote:
> On 11/13/2016 02:13 AM, Floyd L. Davidson wrote:
>> T <T@invalid.invalid> wrote:
>>> Hi All,
>>>
>>> When you pipe something from opne program to anothyer
>>>
>>>     echo abc | ReadAPipe
>>>
>>> Does the first program terminate before the second program
>>> grabs the data?
>>>
>>> Is both are running and the second is just reading what
>>> comes along, how does the second program know when the
>>> first program is no longer sending data?
>>>
>>> Many thanks,
>>> -T
>>
>> The second program can detect an EOF on stdin.
>>
>
> What happens if there is no data in the pipe?
>
It waits
(sleep 3; echo hi;)|cat
0
William
11/14/2016 8:54:52 AM
On 11/14/2016 12:52 AM, William Unruh wrote:
> On 2016-11-13, T <T@invalid.invalid> wrote:
>> Hi All,
>>
>> When you pipe something from opne program to anothyer
>>
>>      echo abc | ReadAPipe
>>
>> Does the first program terminate before the second program
>> grabs the data?
>
> No.
> (echo hi;sleep 3; echo bye)|cat
>
>
>>
>> Is both are running and the second is just reading what
>> comes along, how does the second program know when the
>> first program is no longer sending data?
>
> When the pipe gets closed, by the first program closing it, or dying, or
> whatever.
>
>>
>> Many thanks,
>> -T


Fascinating example.  Thank you!
0
T
11/14/2016 12:31:17 PM
T <T@invalid.invalid> Wrote in message:
> On 11/13/2016 02:13 AM, Floyd L. Davidson wrote:
>> T <T@invalid.invalid> wrote:
>>> Hi All,
>>>
>>> When you pipe something from opne program to anothyer
>>>
>>>     echo abc | ReadAPipe
>>>
>>> Does the first program terminate before the second program
>>> grabs the data?
>>>
>>> Is both are running and the second is just reading what
>>> comes along, how does the second program know when the
>>> first program is no longer sending data?
>>>
>>> Many thanks,
>>> -T
>>
>> The second program can detect an EOF on stdin.
>>
> 
> What happens if there is no data in the pipe?
> 
> 

Nothing comes out at the outlet.  Just like the minds
of trolls.
-- 
0
dlbendigo
11/15/2016 8:12:52 AM
Reply: