Subject: Re: bug#11620: Bug in sleep--linux 2.6

From: Bob Proulx <[email-masked]>
Date: Sun, 3 Jun 2012 14:37:41 -0600

retitle 11620 libc I/O buffers not flushed confusion
tag 11620 + notabug
close 11620
thanks

vikas bansal wrote:
> The sleep function when written at the end, forces the program to sleep,
> preventing him to execute any printf() instructions before it.

You have confused the sleep(3) libc C library routine with the
coreutils sleep(1) command line utility.

Please see this reference:

  http://www.gnu.org/software/coreutils/faq/#I-am-trying-to-compile-a-C-program-_002e_002e_002e

> Attaching a program.

Thank you for showing us the program even if it isn't anything to do
with coreutils.

> printf("going to sleep");

Missing "\n" newline at end of line?

> while (*shm != '*')
> sleep(1);
> exit(0);

The misunderstanding you are seeing is that printf() is calling the
libc stdio function which is buffering the output data.  This data is
only flushed when exit() is called.  The libc buffers data differently
depending upon the context of the file such as if the output is a tty
or a file.

If you wish to ensure that data is output then you should fflush() the
data in the libc I/O buffers.

  http://pubs.opengroup.org/onlinepubs/009696799/functions/fflush.html

Bob