Help with sleep command 
Author Message
 Help with sleep command

I need help understanding why this is happening...here is the code

main()
{
printf("Hello ");
sleep(10);
printf("Goodbye ");

Quote:
}

When I run this on Unix, it sleeps ten seconds then outputs the
'hello'and 'goodbye'.  Why doesn't it output 'Hello' then wait ten
seconds, then output 'Goodbye'?  I have tried so many different possible
combinations using sleep and everytime, it adds all the sleep commands
and does them before anything else...why is this?...please e-mail or post.

                                Shawn



Mon, 05 May 1997 09:55:37 GMT  
 Help with sleep command
|> I need help understanding why this is happening...here is the code
|>
|> main()
|> {
|> printf("Hello ");
|> sleep(10);
|> printf("Goodbye ");
|> }
|>
|> When I run this on Unix, it sleeps ten seconds then outputs the
|> 'hello'and 'goodbye'.  Why doesn't it output 'Hello' then wait ten
|> seconds, then output 'Goodbye'?  I have tried so many different possible
|> combinations using sleep and everytime, it adds all the sleep commands
|> and does them before anything else...why is this?...please e-mail or post.

The printf() function uses buffered I/O by default. That means that until
it sees a newline it figures it can store up whatever you are printing
until then. There are several ways to force printing right away. One way
is to do an explicit 'fflush (stdout);' after each printf(). Another way is
to use the setvbuf() function to change the buffering mode of stdout.
A third way is to use an I/O stream which is already unbuffered like
stderr which you can access via the fprintf() function. There are surely
many other ways, but these three are bound to be the most common.
--





Mon, 05 May 1997 10:48:24 GMT  
 Help with sleep command


Quote:
>I need help understanding why this is happening...here is the code

>main()
>{
>printf("Hello ");
>sleep(10);
>printf("Goodbye ");
>}

>When I run this on Unix, it sleeps ten seconds then outputs the
>'hello'and 'goodbye'.  Why doesn't it output 'Hello' then wait ten
>seconds, then output 'Goodbye'?  I have tried so many different possible
>combinations using sleep and everytime, it adds all the sleep commands
>and does them before anything else...why is this?...please e-mail or post.

At last a reason to say fflush() without invoking undefined
behavior!  You should flush stdout before you go to sleep...

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Mon, 05 May 1997 11:15:57 GMT  
 Help with sleep command

: I need help understanding why this is happening...here is the code

: main()
: {
: printf("Hello ");
: sleep(10);
: printf("Goodbye ");
: }

: When I run this on Unix, it sleeps ten seconds then outputs the
: 'hello'and 'goodbye'.  Why doesn't it output 'Hello' then wait ten
: seconds, then output 'Goodbye'?  I have tried so many different possible
: combinations using sleep and everytime, it adds all the sleep commands
: and does them before anything else...why is this?...please e-mail or post.

        printf puts the output steam in buffer, it outputs the contents of
buffer to "stdout", until "\n" is encounted or the buffer is full, or you
can use fflush to output the contents of buffer explicitly.

        #include <stdio.h>

        main()  
        {      
        printf("Hello ");

        fflush(stdout);

        sleep(10);
        printf("Goodbye ");
        }

--
Chan Chung.



Sat, 10 May 1997 14:06:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Sleep command for noninteger time

2. Header for sleep command

3. I need a DOS version of UNIX sleep command

4. Sleep command and suspend

5. interrupt sleep() or successive sleeps

6. Q: How to set Sleep() between Sleep(0) and Sleep(1)?

7. help: function sleep()

8. Help(making a process "sleep")

9. Help On Sleep Function?

10. help on the function sleep

11. Help:need a specialised sleep function

12. passing commands to the command processor

 

 
Powered by phpBB® Forum Software