* 3.5.rc2 tty buffering bug ?
@ 2012-06-14 16:13 Dave Jones
2012-06-14 16:22 ` Dave Jones
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Dave Jones @ 2012-06-14 16:13 UTC (permalink / raw)
To: Linux Kernel; +Cc: Alan Cox
I found something odd that happens with redirection.
this code...
#include <stdio.h>
void main()
{
printf("begin\n");
if (fork() != 0)
printf("child\n");
else
printf("parent\n");
printf("end\n");
}
usually exits before the child gets to print anything, so stdout looks like..
begin
parent
end
but when redirected to a file, sometimes it prints what we expect, but other times
(if the child exits first) it seems to print..
begin
child
end
begin
parent
end
How does 'begin' get printed a second time ?
Dave
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: 3.5.rc2 tty buffering bug ?
2012-06-14 16:13 3.5.rc2 tty buffering bug ? Dave Jones
@ 2012-06-14 16:22 ` Dave Jones
2012-06-14 16:30 ` Andreas Schwab
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Dave Jones @ 2012-06-14 16:22 UTC (permalink / raw)
To: Linux Kernel, Alan Cox
On Thu, Jun 14, 2012 at 12:13:31PM -0400, Dave Jones wrote:
> #include <stdio.h>
>
> void main()
> {
> printf("begin\n");
>
> if (fork() != 0)
> printf("child\n");
> else
> printf("parent\n");
>
> printf("end\n");
> }
>
> How does 'begin' get printed a second time ?
Someone pointed out that an fflush is needed before the fork.
Somehow I've managed to go all these years without knowing that.
Dave
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: 3.5.rc2 tty buffering bug ?
2012-06-14 16:13 3.5.rc2 tty buffering bug ? Dave Jones
2012-06-14 16:22 ` Dave Jones
@ 2012-06-14 16:30 ` Andreas Schwab
2012-06-14 16:41 ` Alan Cox
2012-06-15 10:32 ` Cong Wang
3 siblings, 0 replies; 5+ messages in thread
From: Andreas Schwab @ 2012-06-14 16:30 UTC (permalink / raw)
To: Dave Jones; +Cc: Linux Kernel, Alan Cox
Dave Jones <davej@redhat.com> writes:
> How does 'begin' get printed a second time ?
It's buffered in stdio, which is then cloned.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: 3.5.rc2 tty buffering bug ?
2012-06-14 16:13 3.5.rc2 tty buffering bug ? Dave Jones
2012-06-14 16:22 ` Dave Jones
2012-06-14 16:30 ` Andreas Schwab
@ 2012-06-14 16:41 ` Alan Cox
2012-06-15 10:32 ` Cong Wang
3 siblings, 0 replies; 5+ messages in thread
From: Alan Cox @ 2012-06-14 16:41 UTC (permalink / raw)
To: Dave Jones; +Cc: Linux Kernel
> How does 'begin' get printed a second time ?
You've been away from user space too long. Read the documentation for
stdio. Stdio is buffering, both forks get the buffer, both write it.
Try the experiment with write()
Alan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: 3.5.rc2 tty buffering bug ?
2012-06-14 16:13 3.5.rc2 tty buffering bug ? Dave Jones
` (2 preceding siblings ...)
2012-06-14 16:41 ` Alan Cox
@ 2012-06-15 10:32 ` Cong Wang
3 siblings, 0 replies; 5+ messages in thread
From: Cong Wang @ 2012-06-15 10:32 UTC (permalink / raw)
To: Dave Jones, Linux Kernel, Alan Cox
On Fri, Jun 15, 2012 at 12:13 AM, Dave Jones <davej@redhat.com> wrote:
> but when redirected to a file, sometimes it prints what we expect, but other times
> (if the child exits first) it seems to print..
>
> begin
> child
> end
> begin
> parent
> end
>
>
> How does 'begin' get printed a second time ?
>
setbuf(3) said:
"
Normally all files are block buffered. [...]
If a stream refers to a terminal (as stdout normally does) it is line buffered.
The standard error stream stderr is always unbuffered by default.
"
So when you redirect stdout to a file, "begin\n" is buffered too, both
in child and in parent.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-06-15 10:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-14 16:13 3.5.rc2 tty buffering bug ? Dave Jones
2012-06-14 16:22 ` Dave Jones
2012-06-14 16:30 ` Andreas Schwab
2012-06-14 16:41 ` Alan Cox
2012-06-15 10:32 ` Cong Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox