bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why does a Bash shell script write prompts followed by reads and do


From: Bob Proulx
Subject: Re: Why does a Bash shell script write prompts followed by reads and do it right?
Date: Tue, 1 Sep 2015 14:46:04 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

John McKown wrote:
> Not a bug, so likely the wrong forum.

Agreed.

> Have you tried doing a fflush() after the fputs()? I.e. something to tell
> the Kernel to "write this out immediately!".

Almost.  It is the C library libc that buffers output with fputs() not
the kernel.  The libc normally checks to see if the output is a "slow"
device such as a tty device.  If so then there is no buffering.  If
the output is a "fast" device such as a file then libc buffers the
output into larger writes for efficiency.  But that only applies when
the output is redirected.  If it is a prompt then the output will be
to the tty and there won't be any buffering.  The fflush() in that
case won't do anything.

> In the case of write(), I think you need to use the O_SYNC flag in
> the open() call.

No.  That isn't necessary at all.  It doesn't matter to programs in
userland if the write is synchronous or not.  Userland programs can't
tell the difference.  Using synchronous I/O is all about what happens
at the instant of a power failure or hard system crash.

Robert Parker wrote:
> Yet when I attempt the same in a C program, the system always writes 2
> prompts, then waits for a read.

Basically that doesn't make sense.  I do not believe that is what is
actually happening.  Please go back and look more deeply.

Since you are talking about prompts I assume you are writing to a tty
device.  There is no buffering and the output will appear on the tty
immediately without need for opening it synchronously or flushing or
anything extra.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]