JohnnyF
JohnnyF

Reputation: 1101

sleep function in c crazy bug in linux

i have this simple code" printinterval in int

sleep(printinterval/3);
displayPrint();  //// just some printing func
sleep(printinterval/3);
displayPrint(); 
sleep(printinterval-2*(int)(printinterval/3));
displayPrint(); 

the problem is that it doest not do the delay the right way, jumps over 1 sleep

but

sleep(printinterval/3);
printf("\n");
displayPrint();  //// just some printing func
printf("\n");    
sleep(printinterval/3);
printf("\n");    
displayPrint();
printf("\n"); 
sleep(printinterval-2*(int)(printinterval/3));
printf("\n");
displayPrint(); 

works like a charm

any ideas? thx :)

EDITED: thx for the help found this way to fix it

fflush(stdout); // Will now print everything in the stdout buffer

and again, thx

Upvotes: 0

Views: 254

Answers (1)

DhruvPathak
DhruvPathak

Reputation: 43275

The stream is buffered, and flushed when a \n is encountered.

See:Why does printf not flush after the call unless a newline is in the format string?

Upvotes: 9

Related Questions