-2

I have a code that does something similar to the following repeatedly over a loop:

$ cat test.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
    char arr[6] = {'h','e','l','l','o','!'};
    for(int x=0; x<6 ; x++){
        printf("%c",arr[x]);
        usleep(1000000);
        printf("%c",arr[x]);
        usleep(1000000);
    }
    printf("\n");

    return 0;
}

I see that printf() executes one after the other WITHOUT any delay (due to usleep), and then the program sleeps for the total usleep time at the end before the next iteration. Seems like all the usleep() calls happen together in the end.

I tried -O0 flag in gcc, because I suspected its the effect of compiler optimization. But I guess -O0 flag does not disable whatever optimization category this case falls under (if my guess is correct about the compiler being the reason for this behavior).

I am trying to understand the reason for this behavior and how to achieve the desired behavior from my program.

Note: I know it might be possible to replace usleep() with some compute-heavy function call that take an equivalent amount of time, but that is not the solution I am looking for.

jww
  • 97,681
  • 90
  • 411
  • 885
ab91
  • 1
  • 1
  • 10
    Don't describe the code, show the code. See [mcve]. – user3386109 Jun 05 '19 at 16:25
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And as mentioned, please try to create a [mcve] to show us. – Some programmer dude Jun 05 '19 at 16:25
  • 3
    A C compiler wouldn't combine calls to `usleep()`. Something else must be the cause. – Ian Abbott Jun 05 '19 at 16:37
  • Here you go, an example @user3386109 char arr[6] = {'h','e','l','l','o','!'}; for(int x=0; x<6 ; x++){ printf("%c",arr[x]); usleep(1000000); } – ab91 Jun 05 '19 at 17:41
  • 4
    Add `fflush(stdout);` after `printf`. – HolyBlackCat Jun 05 '19 at 17:49
  • 1
    That's just output buffering. Duplicate of https://stackoverflow.com/questions/4201083/sleep-delays-output-until-end. – melpomene Jun 05 '19 at 17:54
  • @HolyBlackCat TY! Looked into https://stackoverflow.com/questions/22901901/what-does-fflushstdin-do-in-c-programing – ab91 Jun 05 '19 at 18:01
  • Possible duplicate of [How to turn off buffering of stdout in C](https://stackoverflow.com/q/7876660/608639), [sleep() delays output until end](https://stackoverflow.com/q/4201083/608639), etc – jww Jun 06 '19 at 04:03

1 Answers1

0

You are using usleep() wrong. Use sleep(1) instead.

From man usleep:

EINVAL usec is greater than or equal to 1000000. (On systems where that is considered an error.)

Once you fix that you should do fflush() after printf() to avoid another surprise with output buffering.

Joshua
  • 40,822
  • 8
  • 72
  • 132