Why stdout won't flush when redirecting input from a fifo?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a program that displays a prompt message and waits for the user to input some text.
$ program
Input a line of text: <aaaa>
Some more output.
$
Now, I also want to be able to provide this input, programmatically, via a FIFO, like so:
$ program < fifo
But what's happening is, the prompt on stdout does not appear until I have supplied the input to fifo
.
I next tried a very simple equivalent of my program, like so:
$ echo aaaa < fifo
Even here, aaaa
doesn't appear until fifo
has been supplied its input.
Question: How do I make my output till the point I wait for input from stdin
appear on stdout
when using FIFO?
stdout stdin fifo
add a comment |Â
up vote
2
down vote
favorite
I have a program that displays a prompt message and waits for the user to input some text.
$ program
Input a line of text: <aaaa>
Some more output.
$
Now, I also want to be able to provide this input, programmatically, via a FIFO, like so:
$ program < fifo
But what's happening is, the prompt on stdout does not appear until I have supplied the input to fifo
.
I next tried a very simple equivalent of my program, like so:
$ echo aaaa < fifo
Even here, aaaa
doesn't appear until fifo
has been supplied its input.
Question: How do I make my output till the point I wait for input from stdin
appear on stdout
when using FIFO?
stdout stdin fifo
The script/program doesn't even start to execute until there is data in the pipe. Also, unrelated to your issue: Prompting should always be done on the standard error stream, not and standard output.
â Kusalananda
Jun 14 at 14:15
That's (shocking) news to me! Why should the program not even start? If the input were coming in from a regular disk file (and, not from a fifo), would the same semantics of program execution hold? Input should get read ONLY when it's needed, not before. So, a reading from fifo should block only on reaching the point of input in the program, no?
â Harry
Jun 14 at 14:23
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a program that displays a prompt message and waits for the user to input some text.
$ program
Input a line of text: <aaaa>
Some more output.
$
Now, I also want to be able to provide this input, programmatically, via a FIFO, like so:
$ program < fifo
But what's happening is, the prompt on stdout does not appear until I have supplied the input to fifo
.
I next tried a very simple equivalent of my program, like so:
$ echo aaaa < fifo
Even here, aaaa
doesn't appear until fifo
has been supplied its input.
Question: How do I make my output till the point I wait for input from stdin
appear on stdout
when using FIFO?
stdout stdin fifo
I have a program that displays a prompt message and waits for the user to input some text.
$ program
Input a line of text: <aaaa>
Some more output.
$
Now, I also want to be able to provide this input, programmatically, via a FIFO, like so:
$ program < fifo
But what's happening is, the prompt on stdout does not appear until I have supplied the input to fifo
.
I next tried a very simple equivalent of my program, like so:
$ echo aaaa < fifo
Even here, aaaa
doesn't appear until fifo
has been supplied its input.
Question: How do I make my output till the point I wait for input from stdin
appear on stdout
when using FIFO?
stdout stdin fifo
asked Jun 14 at 13:39
Harry
3701313
3701313
The script/program doesn't even start to execute until there is data in the pipe. Also, unrelated to your issue: Prompting should always be done on the standard error stream, not and standard output.
â Kusalananda
Jun 14 at 14:15
That's (shocking) news to me! Why should the program not even start? If the input were coming in from a regular disk file (and, not from a fifo), would the same semantics of program execution hold? Input should get read ONLY when it's needed, not before. So, a reading from fifo should block only on reaching the point of input in the program, no?
â Harry
Jun 14 at 14:23
add a comment |Â
The script/program doesn't even start to execute until there is data in the pipe. Also, unrelated to your issue: Prompting should always be done on the standard error stream, not and standard output.
â Kusalananda
Jun 14 at 14:15
That's (shocking) news to me! Why should the program not even start? If the input were coming in from a regular disk file (and, not from a fifo), would the same semantics of program execution hold? Input should get read ONLY when it's needed, not before. So, a reading from fifo should block only on reaching the point of input in the program, no?
â Harry
Jun 14 at 14:23
The script/program doesn't even start to execute until there is data in the pipe. Also, unrelated to your issue: Prompting should always be done on the standard error stream, not and standard output.
â Kusalananda
Jun 14 at 14:15
The script/program doesn't even start to execute until there is data in the pipe. Also, unrelated to your issue: Prompting should always be done on the standard error stream, not and standard output.
â Kusalananda
Jun 14 at 14:15
That's (shocking) news to me! Why should the program not even start? If the input were coming in from a regular disk file (and, not from a fifo), would the same semantics of program execution hold? Input should get read ONLY when it's needed, not before. So, a reading from fifo should block only on reaching the point of input in the program, no?
â Harry
Jun 14 at 14:23
That's (shocking) news to me! Why should the program not even start? If the input were coming in from a regular disk file (and, not from a fifo), would the same semantics of program execution hold? Input should get read ONLY when it's needed, not before. So, a reading from fifo should block only on reaching the point of input in the program, no?
â Harry
Jun 14 at 14:23
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
The FIFO is opened by the shell, but the opening of it is blocking until the FIFO is opened for writing by some other process. Since redirections are processed before the execution of the command, the program is not even executed until the shell's open()
call returns. This is documented behaviour for the open()
C library function (used by the shell):
O_NONBLOCK
When opening a FIFO with
O_RDONLY
orO_WRONLY
set:
If
O_NONBLOCK
is set, anopen()
for reading-only
shall return without delay. Anopen()
for writing-only shall return an error if no process currently
has the file open for reading.
If
O_NONBLOCK
is clear, anopen()
for reading-only
shall block the calling thread until a thread opens
the file for writing. Anopen()
for writing-only
shall block the calling thread until a thread opens
the file for reading.
So the shell is obviously not using O_NONBLOCK
when it's opening the FIFO.
Solution:
cat fifo | program
cat
reads from the FIFO until end-of-file, then exits.tail -f fifo | program
tail
reads from the FIFO until end-of-file, then hangs around to wait for more until terminated.
Which of these is best suited to your situation depends a bit on what's writing to the FIFO at the other end, and how it does the writing.
Third solution involves having the program opening the FIFO when needed.
According to the Rationale section on sh
, a shell must set standard input to blocking because...
If the shell did not reset this flag, it would immediately terminate because no input data would be available yet and that would be considered the same as end-of-file.
1
FIFOs on Linux and FreeBSD can be opened inO_RDWR
mode, yielding a third choice.
â JdeBP
Jun 14 at 15:50
Hmh, as for the last quote, wouldn't theread()
on the pipe just block until a writer comes along. Or should I say "couldn't it". That's what seems to happen on Linux anyway, if you open in read-write mode... except then it never gives an EOF condition. Logically enough, the same process is a writer too.
â ilkkachu
Jun 14 at 16:24
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
The FIFO is opened by the shell, but the opening of it is blocking until the FIFO is opened for writing by some other process. Since redirections are processed before the execution of the command, the program is not even executed until the shell's open()
call returns. This is documented behaviour for the open()
C library function (used by the shell):
O_NONBLOCK
When opening a FIFO with
O_RDONLY
orO_WRONLY
set:
If
O_NONBLOCK
is set, anopen()
for reading-only
shall return without delay. Anopen()
for writing-only shall return an error if no process currently
has the file open for reading.
If
O_NONBLOCK
is clear, anopen()
for reading-only
shall block the calling thread until a thread opens
the file for writing. Anopen()
for writing-only
shall block the calling thread until a thread opens
the file for reading.
So the shell is obviously not using O_NONBLOCK
when it's opening the FIFO.
Solution:
cat fifo | program
cat
reads from the FIFO until end-of-file, then exits.tail -f fifo | program
tail
reads from the FIFO until end-of-file, then hangs around to wait for more until terminated.
Which of these is best suited to your situation depends a bit on what's writing to the FIFO at the other end, and how it does the writing.
Third solution involves having the program opening the FIFO when needed.
According to the Rationale section on sh
, a shell must set standard input to blocking because...
If the shell did not reset this flag, it would immediately terminate because no input data would be available yet and that would be considered the same as end-of-file.
1
FIFOs on Linux and FreeBSD can be opened inO_RDWR
mode, yielding a third choice.
â JdeBP
Jun 14 at 15:50
Hmh, as for the last quote, wouldn't theread()
on the pipe just block until a writer comes along. Or should I say "couldn't it". That's what seems to happen on Linux anyway, if you open in read-write mode... except then it never gives an EOF condition. Logically enough, the same process is a writer too.
â ilkkachu
Jun 14 at 16:24
add a comment |Â
up vote
4
down vote
accepted
The FIFO is opened by the shell, but the opening of it is blocking until the FIFO is opened for writing by some other process. Since redirections are processed before the execution of the command, the program is not even executed until the shell's open()
call returns. This is documented behaviour for the open()
C library function (used by the shell):
O_NONBLOCK
When opening a FIFO with
O_RDONLY
orO_WRONLY
set:
If
O_NONBLOCK
is set, anopen()
for reading-only
shall return without delay. Anopen()
for writing-only shall return an error if no process currently
has the file open for reading.
If
O_NONBLOCK
is clear, anopen()
for reading-only
shall block the calling thread until a thread opens
the file for writing. Anopen()
for writing-only
shall block the calling thread until a thread opens
the file for reading.
So the shell is obviously not using O_NONBLOCK
when it's opening the FIFO.
Solution:
cat fifo | program
cat
reads from the FIFO until end-of-file, then exits.tail -f fifo | program
tail
reads from the FIFO until end-of-file, then hangs around to wait for more until terminated.
Which of these is best suited to your situation depends a bit on what's writing to the FIFO at the other end, and how it does the writing.
Third solution involves having the program opening the FIFO when needed.
According to the Rationale section on sh
, a shell must set standard input to blocking because...
If the shell did not reset this flag, it would immediately terminate because no input data would be available yet and that would be considered the same as end-of-file.
1
FIFOs on Linux and FreeBSD can be opened inO_RDWR
mode, yielding a third choice.
â JdeBP
Jun 14 at 15:50
Hmh, as for the last quote, wouldn't theread()
on the pipe just block until a writer comes along. Or should I say "couldn't it". That's what seems to happen on Linux anyway, if you open in read-write mode... except then it never gives an EOF condition. Logically enough, the same process is a writer too.
â ilkkachu
Jun 14 at 16:24
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
The FIFO is opened by the shell, but the opening of it is blocking until the FIFO is opened for writing by some other process. Since redirections are processed before the execution of the command, the program is not even executed until the shell's open()
call returns. This is documented behaviour for the open()
C library function (used by the shell):
O_NONBLOCK
When opening a FIFO with
O_RDONLY
orO_WRONLY
set:
If
O_NONBLOCK
is set, anopen()
for reading-only
shall return without delay. Anopen()
for writing-only shall return an error if no process currently
has the file open for reading.
If
O_NONBLOCK
is clear, anopen()
for reading-only
shall block the calling thread until a thread opens
the file for writing. Anopen()
for writing-only
shall block the calling thread until a thread opens
the file for reading.
So the shell is obviously not using O_NONBLOCK
when it's opening the FIFO.
Solution:
cat fifo | program
cat
reads from the FIFO until end-of-file, then exits.tail -f fifo | program
tail
reads from the FIFO until end-of-file, then hangs around to wait for more until terminated.
Which of these is best suited to your situation depends a bit on what's writing to the FIFO at the other end, and how it does the writing.
Third solution involves having the program opening the FIFO when needed.
According to the Rationale section on sh
, a shell must set standard input to blocking because...
If the shell did not reset this flag, it would immediately terminate because no input data would be available yet and that would be considered the same as end-of-file.
The FIFO is opened by the shell, but the opening of it is blocking until the FIFO is opened for writing by some other process. Since redirections are processed before the execution of the command, the program is not even executed until the shell's open()
call returns. This is documented behaviour for the open()
C library function (used by the shell):
O_NONBLOCK
When opening a FIFO with
O_RDONLY
orO_WRONLY
set:
If
O_NONBLOCK
is set, anopen()
for reading-only
shall return without delay. Anopen()
for writing-only shall return an error if no process currently
has the file open for reading.
If
O_NONBLOCK
is clear, anopen()
for reading-only
shall block the calling thread until a thread opens
the file for writing. Anopen()
for writing-only
shall block the calling thread until a thread opens
the file for reading.
So the shell is obviously not using O_NONBLOCK
when it's opening the FIFO.
Solution:
cat fifo | program
cat
reads from the FIFO until end-of-file, then exits.tail -f fifo | program
tail
reads from the FIFO until end-of-file, then hangs around to wait for more until terminated.
Which of these is best suited to your situation depends a bit on what's writing to the FIFO at the other end, and how it does the writing.
Third solution involves having the program opening the FIFO when needed.
According to the Rationale section on sh
, a shell must set standard input to blocking because...
If the shell did not reset this flag, it would immediately terminate because no input data would be available yet and that would be considered the same as end-of-file.
edited Jun 14 at 15:04
answered Jun 14 at 14:43
Kusalananda
101k13199312
101k13199312
1
FIFOs on Linux and FreeBSD can be opened inO_RDWR
mode, yielding a third choice.
â JdeBP
Jun 14 at 15:50
Hmh, as for the last quote, wouldn't theread()
on the pipe just block until a writer comes along. Or should I say "couldn't it". That's what seems to happen on Linux anyway, if you open in read-write mode... except then it never gives an EOF condition. Logically enough, the same process is a writer too.
â ilkkachu
Jun 14 at 16:24
add a comment |Â
1
FIFOs on Linux and FreeBSD can be opened inO_RDWR
mode, yielding a third choice.
â JdeBP
Jun 14 at 15:50
Hmh, as for the last quote, wouldn't theread()
on the pipe just block until a writer comes along. Or should I say "couldn't it". That's what seems to happen on Linux anyway, if you open in read-write mode... except then it never gives an EOF condition. Logically enough, the same process is a writer too.
â ilkkachu
Jun 14 at 16:24
1
1
FIFOs on Linux and FreeBSD can be opened in
O_RDWR
mode, yielding a third choice.â JdeBP
Jun 14 at 15:50
FIFOs on Linux and FreeBSD can be opened in
O_RDWR
mode, yielding a third choice.â JdeBP
Jun 14 at 15:50
Hmh, as for the last quote, wouldn't the
read()
on the pipe just block until a writer comes along. Or should I say "couldn't it". That's what seems to happen on Linux anyway, if you open in read-write mode... except then it never gives an EOF condition. Logically enough, the same process is a writer too.â ilkkachu
Jun 14 at 16:24
Hmh, as for the last quote, wouldn't the
read()
on the pipe just block until a writer comes along. Or should I say "couldn't it". That's what seems to happen on Linux anyway, if you open in read-write mode... except then it never gives an EOF condition. Logically enough, the same process is a writer too.â ilkkachu
Jun 14 at 16:24
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449816%2fwhy-stdout-wont-flush-when-redirecting-input-from-a-fifo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
The script/program doesn't even start to execute until there is data in the pipe. Also, unrelated to your issue: Prompting should always be done on the standard error stream, not and standard output.
â Kusalananda
Jun 14 at 14:15
That's (shocking) news to me! Why should the program not even start? If the input were coming in from a regular disk file (and, not from a fifo), would the same semantics of program execution hold? Input should get read ONLY when it's needed, not before. So, a reading from fifo should block only on reaching the point of input in the program, no?
â Harry
Jun 14 at 14:23