LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-29-2009, 12:13 PM   #1
nsvora
LQ Newbie
 
Registered: Oct 2006
Posts: 16

Rep: Reputation: 0
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
 
Old 06-29-2009, 11:49 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,790

Rep: Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538
I'd make a point of printing out the relevant values at each step just to check what's really going on, instead of assuming anything.
iirc, the sleep() fn is usually written to complete at the 'top' of the second ie it should sleep for at least <asked_for_secs>, but may sleep for longer, particularly if processor is busy.
http://www.opengroup.org/onlinepubs/...ons/sleep.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL goes to sleep & web-site hangs indefinetely... jaggy00 Linux - Server 1 06-19-2008 01:38 PM
RHEL4u2 perl localtime() call issues (perl & glibc) Brad.Scalio@noaa.gov Linux - Enterprise 3 01-23-2007 09:27 PM
Searching for Server (BOOTP/DHCP) <sleep> <sleep> .. .. Eileen Linux - Networking 12 10-21-2005 01:14 AM
Fedora3 & monitor sleep mode zeky Linux - General 1 03-16-2005 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration