Thread: sleep() call (apparently) blocking code execution

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Michigan U.P.
    Posts
    20

    sleep() call (apparently) blocking code execution

    (Ubuntu 18.04, gcc version 7.5.0)

    I don't understand why individual sleep(); calls produce the desired pause in program processing. When I put sleep(); inside a for() loop, it seems all the iterations of for() happen at once (at the terminal output). One would think sleep() behaves the same inside or outside of a loop structure.

    Also, during execution, when the for() loop is entered, my normally-flashing terminal cursor STOPS FLASHING. Is my system posessed? Thx.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main()  {
    int y;
    
    printf("\nOne\n");
    sleep(1);
    printf("Two\n");
    sleep(2);
    printf("Three\n");
    sleep(3);
    
    for (y = 0; y < 10; y++) {
       printf("%d   ", y);
       sleep(2);
       }
    
    printf("\n\nDone\n\n");
    return(0);
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    I don't really understand your question.
    But you are aware that the sleep time is in milliseconds, not seconds, right?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Michigan U.P.
    Posts
    20
    john.c:

    I got this from an online GCC resource:

    Function: unsigned int sleep (unsigned int seconds)
    The sleep function waits for seconds seconds or until a signal is delivered, whichever happens first.

    I was vague about the problem. When it runs, the first 3 printf() statements display one at a time, with a gradually increasing delay (as we see in the code). In the for() loop, I was hoping for numbers to get printed one at a time, like 'one' 'two' and 'three' did. But nothing happens for perhaps 6 full seconds, then the numbers 0 thru 9 all appear at once.

    That's not what I was hoping for.
    Thx.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    You're absolutely right, it is seconds! I was mixing it up with the windows Sleep function.

    Your problem is simply one of buffering. You need to add a fflush(stdout); statement after the printf to flush the output buffer. stdout is usually "line buffered", which means it will be automatically flushed when a newline is printed (or if the buffer is full). But up to that point it just keeps filling up the buffer.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Jul 2012
    Location
    Michigan U.P.
    Posts
    20
    Using an fflush(stdout); in the for() loop does fix the problem. Your observation is appreciated. I found elsewhere on the net that no fflush(); is needed if the printf() includes a newline in the output string "line \n". I've been using 'C' for well-on 4 decades, and this is a new one to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-20-2014, 05:41 AM
  2. Replies: 1
    Last Post: 07-15-2014, 12:48 PM
  3. Is it possible to call a function during execution?
    By workisnotfun in forum C Programming
    Replies: 1
    Last Post: 02-17-2013, 12:54 AM
  4. A non-blocking sleep whit visual studio c++
    By Oolong in forum Windows Programming
    Replies: 5
    Last Post: 04-06-2010, 11:16 PM
  5. Is sleep system call ?
    By ch4 in forum C Programming
    Replies: 4
    Last Post: 11-28-2008, 10:40 AM

Tags for this Thread