sleep & localtime query


 
Thread Tools Search this Thread
Operating Systems HP-UX sleep & localtime query
# 1  
Old 06-29-2009
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  
Old 06-29-2009
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  
Old 06-30-2009
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  
Old 07-01-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Emc powerpath device & zfs query

Hi, We're trying out a SAN migration from HP EVA to EMC VMAX, and run into a bit of an issue with powerpath and zfs. The method we're currently using to migrate is to export the HP EVA luns from our sun server, replicate using SAN based method, and then present the new luns to our Sun server... (7 Replies)
Discussion started by: badoshi
7 Replies

2. UNIX for Dummies Questions & Answers

Sleep Until Query

Hi, I am a newbie in UNIX please help me develop code for below logic:-- I want my script to implement sleep until a specified time(date doesn't matter) ---------------------------------------- While currenttime > 14:00 and < 18:00 sleep end while run command exit... (9 Replies)
Discussion started by: kiitmale
9 Replies

3. Solaris

Query regarding Indentity Manger & Directory Server

Can anyone tell me what's the difference between Identity Manager, oracle/sun directory services & AD bridge prodcut like centrify or quest (0 Replies)
Discussion started by: fugitive
0 Replies

4. UNIX for Dummies Questions & Answers

bash script & sql query

Hi Guys, I would like with this script "DATA=`ora123 su -c 'echo "SET HEADING OFF;\n SET FEED OFF;\n select USER NAME, ACCOUNT_STATUS from dba_users;\n exit" | sqlplus / as sysdba -s /' ` echo $DATA >> $DAT" make a sql query, but the statement "select USER NAME, ACCOUNT_STATUS from... (6 Replies)
Discussion started by: ixibits
6 Replies

5. Shell Programming and Scripting

Awk & sed query for output

Hello, I have a file. its content are like below. mdn:87439842 imsi:23082038203 Ctime:12082010 01:20:10 mdn:9324783783 imsi:402349823322 Ctime: 12072010 01:20:10 mdn:87439842 imsi:23082038203 Ctime: 23072010 01:20:10 mdn:87439842 imsi:23082038203 Ctime:18072010 01:20:10 mdn:87439842... (3 Replies)
Discussion started by: Sanket11
3 Replies

6. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

7. Shell Programming and Scripting

Oracle SQL Query & connect?

Hi I'm looking to query a table on a database and then iterate over the results in a loop. I believe this is the last part of my script that I need (after finding out threads for passing variables to other scripts and calling functions in other scripts). I've searched the forums but the best... (8 Replies)
Discussion started by: Dird
8 Replies

8. UNIX for Dummies Questions & Answers

sleep 10 && command doesn't work

Hi there, I found a trick to easily postpone a command by a few seconds: supernova:~# sleep 10 && command &If you logout, the command should still be executed... But not all the time. Could anyone of you explain me why the following command is executed even after logging out: supernova:~# sleep... (2 Replies)
Discussion started by: chebarbudo
2 Replies

9. Shell Programming and Scripting

& in SQL query

I have a script that looks for all jobs that contain a particular calendar. Some of the calendars have '&' in them and sql freaks out when it encounters that.. is there a way around this? I have tried: select job_name from job where run_calendar='1&15dom' select job_name from job... (3 Replies)
Discussion started by: Lindarella
3 Replies

10. Shell Programming and Scripting

Date & NUmber Validation Query

Hi Do you have any pointers how to validate numbers (not to contain alphabets and special characters) and date(MM/DD/YYYY) format. I used following regular expression to validate integer, which is not working in the default shell: nodigits="$(echo $testvalue | sed 's/]//g')" ... (4 Replies)
Discussion started by: alok_jax
4 Replies
Login or Register to Ask a Question