[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38040
  • Last Modified:

Sleep Function

I have this piece of code.

printf("Exiting .");
sleep(1);
printf(" .");
sleep(1);
printf(" .");
sleep(1);
printf(" .");
sleep(1);
printf(" .");


I want to produce some sort of animation but then it does not seem to work as expected in unix/linux. The code works fine on windows. How to make that work in unix/linux?


hongjun
0
hongjun
Asked:
hongjun
  • 3
1 Solution
 
sunnycoderCommented:
on windows sleep is in milliseconds and on linux it is in seconds ...
use usleep or nanosleep to achieve what you want
0
 
hongjunAuthor Commented:
I am aware of the differences b/w windows and linux and that's why I do a sleep(1). I am trying to do this animation
Exiting .
ONE SECOND LATER
Exiting . .
ONE SECOND LATER
Exiting . . .
ONE SECOND LATER
....
.....


hongjun
0
 
sunnycoderCommented:
Ok then I guess you are not getting the format as you expect... (dot by dot)
if yes, then you need to either flush the buffers or turn off buffering

printf("Exiting .");
fflush(stdout);
sleep(1);
printf(" .");
fflush(stdout);
sleep(1);
printf(" .");
fflush(stdout);
sleep(1);
printf(" .");
fflush(stdout);
sleep(1);
printf(" .");
fflush(stdout);
0
 
sunnycoderCommented:
the above code will flush the buffers
to turn off buffering, refer to this
http://oldlook.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20675234.html 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now