0

Hi!

I'm trying to use the nanosleep and sleep functions in a program I am trying to write.

I first tried this -

fprintf(stdout,"Hey guys..");
struct timespec t_req, t_rem;
t_req.tv_sec = 1;
t_req.tv_nsec = 500;
if (nanosleep(&t_req, &t_rem) < 0)
   return 0;
fprintf(stdout,"...What's up?");

Since the function nanosleep did not return a negative number, I expected there to be a 1 second (at least) gap between the two outputs.

However, that doesn't seem to be the case. As soon as I run the program, the program waits for 1 second and then throws out all the output.

How can I ensure that the program waits?

Thanks
Kedar

2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by mitrmkar
1

Considering using fflush() to have the output printed immediately

fprintf(stdout,"Hey guys..");
/* force the output onto the screen .. */ 
fflush(stdout);
struct timespec t_req, t_rem;
t_req.tv_sec = 1;
t_req.tv_nsec = 500;
if (nanosleep(&t_req, &t_rem) < 0)
   return 0;
fprintf(stdout,"...What's up?");
Votes + Comments
Yes
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.