avatar
A question related to pipe# Programming - 葵花宝典
s*g
1
i have two programs, P1 print one line of data to stdout every 1 sec.
P2 read in data from stdin and do some processing, now I am using pipe
as follows in linux:
P1 | P2
what happens is that P2 is not getting data from the pipe every sec, it
seems
that it wait for the pipe to be filled full by P1, and read a lot of lines
out.
My question is: why the pipe performs like this way? wouldn't P2 should
read one line as soon as it sees a '\n'?
avatar
t*t
2
usually, all streams are block buffered, unless:
if it is stderr, it's unbuffered
if it is not stderr, and refers to a terminal, it is line buffered.
you may change the behaviour by using setbuf().

【在 s********g 的大作中提到】
: i have two programs, P1 print one line of data to stdout every 1 sec.
: P2 read in data from stdin and do some processing, now I am using pipe
: as follows in linux:
: P1 | P2
: what happens is that P2 is not getting data from the pipe every sec, it
: seems
: that it wait for the pipe to be filled full by P1, and read a lot of lines
: out.
: My question is: why the pipe performs like this way? wouldn't P2 should
: read one line as soon as it sees a '\n'?

avatar
s*g
3
I guess i don't understand why pipe does not behave like stdout,
for stdout, the buffer is flushed either when the buffer is full or it
sees a '\n'. Pipe will not be flushed even P1 puts a '\n' at the end of
each line.

【在 t****t 的大作中提到】
: usually, all streams are block buffered, unless:
: if it is stderr, it's unbuffered
: if it is not stderr, and refers to a terminal, it is line buffered.
: you may change the behaviour by using setbuf().

avatar
t*t
4
because pipe does not refer to a terminal, of course! can't you read?...

【在 s********g 的大作中提到】
: I guess i don't understand why pipe does not behave like stdout,
: for stdout, the buffer is flushed either when the buffer is full or it
: sees a '\n'. Pipe will not be flushed even P1 puts a '\n' at the end of
: each line.

avatar
c*x
5
use flush
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。