The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Operating Systems > HP-UX
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 07-01-2009
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,790
I tried to convey this clearly:

You cannot guarantee EXACTLY how long sleep is. You have to add some slop (fudge factor) in your seconds calculation one way or another, it is not going to work out perfectly every time - example assumes you have POSIX compliant time - ie., no leap seconds.:

Code:
calculate duration of sleep:
time_t how_long_to_sleep=1800;
how_long_to_sleep-= time(NULL) % 1800;
sleep(!how_long_to_sleep ? 1800 : how_long_to_sleep);

time test after wakeup:

Code:
time_t now=time(NULL);
now=now % 1800;
if( now < 2 && now > 1797)
   // sleep period ended close enough
else
  // sleep period is off by too much
fi

You could also try usleep(value ) which has finer granularity in ms, but again value is the MINIMUM. It may be longer. For example a higher priority job than this one is running.