vendredi 8 mai 2020

How come printf() only executes after a loop that delays execution on WSL2(Ubuntu)?

I have been playing around with the code below on both WSL and Windows. The code works as expected on Windows after compiling with MSVC. On WSL2(Ubuntu) I get unexpected results after compiling with GCC.

The code generates 2 random integers and print them, then waits 5 seconds before program exits. But on bash the print only happens after the 5 seconds, not before.

Please have a look at the code below.

Thanks

#include <stdio.h>
#include <time.h>
#include <stdlib.h>



int main(){


    clock_t start_time = clock();


    srand(time(NULL));
    for(unsigned int i = 0; i < 2; i++)
            printf("%u ", rand() % 10);         // <--- Prints after 5 seconds on WSL 2 (Ubuntu)


    while(clock() - start_time < CLOCKS_PER_SEC * 5);


    return 0;
}



Aucun commentaire:

Enregistrer un commentaire