The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
add the output of a query to a variable to be used in another query hazno Shell Programming and Scripting 0 03-24-2009 08:52 PM
Mac OS X: Why your Mac might not sleep or stay in sleep mode iBot OS X Support RSS 0 03-19-2009 10:50 PM
converting localtime to unixtime AshishK UNIX for Advanced & Expert Users 2 10-30-2007 10:22 AM
Perl + localtime() Optimus_P Shell Programming and Scripting 3 07-09-2003 05:32 PM
sleep Anna UNIX for Dummies Questions & Answers 5 07-10-2001 11:52 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 06-29-2009
nsvora nsvora is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 2
sleep & localtime query

Hi,
I work on HPUX application and i had a query regarding the sleep & localtime system call in HPUX.
Here is the code that we have :

const uint32_t WAKE_INTERVAL_SEC(30*60);
while (true) {

// Find out what time it is
time(&currentTime);

// Thread to align wakeup with clock 30 minute i.e if it is currently 9:20 wakeup after 10 minutes,
// If it is currently 9:05, wake up after 25 minutes.
// Calculate remaining number of seconds in this half hour.
sleepTime = WAKE_INTERVAL_SEC - (currentTime % WAKE_INTERVAL_SEC);
while (1) {

sleep(sleepTime);

// Do regular work
tptr = localtime(&currentTime);

if (12 == tptr->tm_hour && tptr->tm_min <WAKE_INTERVAL_MIN)
{
// Noon work to be done
}
}

Expected Behaviour:
As refered in above code, we expect that every half an hour, some work to be done and if that time happens to be between 12 and 12:30am then we also do noon work.

Actual Behaviour:
Most of the time it works as expected. But some times, sleep seems to exit a few seconds before 12:30 and so the logic fails and does the noon work. Ideally i would expect that noon work condition to fail, since thread has woke up at 12:30 and hence it cannot satisify noon work condition. ( Trace in syslog indicates that regular work has started at 12:30, but logging itself would have taken some time and i suspect sleep could have woken up early)

Question:
- Can sleep wake up before the schedule time ( other than SIGALRM) ? Are there any other conditions where sleep could wake up before scheduled time?
- Can localtime used for comparison return wrong time ?
- Is it possible that there are some time adjustments done internally(may be for internal clock drifting etc) which results in sleep waking up a second earlier than expected or local time doing some kind of adjustment for some reason?

Any inputs in this regards are appreciated.

Thanks & Regards,
nsv
  #2 (permalink)  
Old 06-29-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,720
Why not use cron or at to schedule your code? Those are built to run commands or scripts at exactly the right time.

Plus, sleep(1) is NOT guaranteed to sleep exactly one second.
  #3 (permalink)  
Old 06-30-2009
nsvora nsvora is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 2
Hi Jim,
Thanks for the reply.
Actually threads can wake up any time based on the scheduler, so the logic was to check the time the thread needs to sleep so that its aligned with 30 minute intervals.

With Regards,
nsvora
  #4 (permalink)  
Old 07-01-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,720
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.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0