Thread: sleep() function problem or logic problem?

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    sleep() function problem or logic problem?

    I cant understand why the sleep() function doesn't show the variable every 1 second. It seems it waits 5 seconds (since I have five rows) and then prints the whole row at once.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void) {
    
        int M[5][5];
        int row = 0, column = 0;
    
        for (row = 0; row < 5; row++) {
            for (column = 0; column < 5; column++) { //  {{{
                if ((row + column) < 4) {
                    M[row][column] = 'A';
                }
                else {
                    if ((row + column) == 4) {
                        M[row][column] = 'O';
                    }
                    else {
                        M[row][column] = 'B';
                    }
                }
            }
        } // }}}
    
        for (row = 0; row < 5; row++) { // {{{
            for (column = 0; column < 5; column++) { //  {{{
                sleep(1);
                printf("%c\t", M[row][column]);
            }
            printf("\n");
    
        } // }}}
    
        return 0; 
    }
    
    /*
     * vim:foldmethod=marker foldmarker={{{,}}}
     */
    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's because console output is buffered. Your inner loop from line 27 to 30 is stacking stuff up in the buffer, which is not printed to the screen until the loop exits and you send a linefeed in line 31.

    You may be able to fix this by inserting fflush(stdout); at line 30.
    Last edited by CommonTater; 11-16-2011 at 04:45 PM.

  3. #3
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by CommonTater View Post
    That's because console output is buffered. Your inner loop from line 27 to 30 is stacking stuff up in the buffer, which is not printed to the screen until the loop exits and you send a linefeed in line 31.

    You may be able to fix this by inserting fflush(stdout); at line 30.
    Thank you for the information about the buffered output and for the fflush(stdout) tip. Unfortunately, it didn't work.

    I'll keep trying. Thanks.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What does your new code look like? What doesn't work about it? Wrong output? No delay? Too much delay? I added the fflush line just like Tater said, and it works:
    Code:
        for (row = 0; row < 5; row++) {
            for (column = 0; column < 5; column++) {
                sleep(1);
                printf("%c\t", M[row][column]);
                fflush(stdout);
            }
            printf("\n");

  5. #5
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    What does your new code look like? What doesn't work about it? Wrong output? No delay? Too much delay? I added the fflush line just like Tater said, and it works:
    Code:
      
        for (row = 0; row < 5; row++) {
            for (column = 0; column < 5; column++) {
                sleep(1);
                printf("%c\t", M[row][column]);
                fflush(stdout);
            }
            printf("\n");
    The code is exactly like the snippet above.

    The problem is that it seems to be summing all the sleep(1). It waits for about 5 seconds and only then prints the output, but everything at once. The data output is correct, but not one element of the array at a time, as I would expect.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That sounds exactly like what would happen without the fflush call. It wouldn't display anything on a line until you print the new line. Are you sure you didn't accidentally place the fflush outside the inner loop, i.e. between the closing curly brace and the printf("\n")? Are you sure you saved the change and that the code recompiled properly with the change?

  7. #7
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    That sounds exactly like what would happen without the fflush call. It wouldn't display anything on a line until you print the new line. Are you sure you didn't accidentally place the fflush outside the inner loop, i.e. between the closing curly brace and the printf("\n")? Are you sure you saved the change and that the code recompiled properly with the change?
    For sure, the fflush(stdout) is inside the inner loop.

    And I did recompile the code, with:

    Code:
    gcc -std=c99 -Wall array.c -o array
    It shows no errors or warnings at all.

    I run arch linux, in case that would make any difference.

    EDIT: Sorry, I was recompiling the wrong file. Shame on me.

    It works just fine now.

    Thanks everybody for the time spent helping me. I really appreciate it.
    Last edited by FernandoBasso; 11-16-2011 at 06:09 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The distro shouldn't make a difference, it's all Linux. I can't replicate your problem based on your text description, so I'll need exact code. Do me a favor, and from the command line, type 'cat array.c', and paste the contents in a reply here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Sleep and GetTickCount().
    By IamBMF in forum C Programming
    Replies: 13
    Last Post: 02-03-2008, 09:25 AM
  2. Problem with function sleep()
    By vicst197 in forum C Programming
    Replies: 29
    Last Post: 04-06-2007, 05:16 AM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. Sleep problem
    By GARGONII in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2002, 10:58 PM
  5. A sleep problem
    By Ryan in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2002, 06:09 PM