Periodic thread with clock_nanosleep


 
Thread Tools Search this Thread
Top Forums Programming Periodic thread with clock_nanosleep
# 1  
Old 12-08-2012
Periodic thread with clock_nanosleep

Hi
I have a periodic task (with the highest priority) which I away every X nano-second.
I am using the function clock_nanosleep with REAL_TIME timer.
when I wake up, I versify that I was awake on time,
and check if the delta between the last time I get to sleep and the current time is X nano-second.
(I verify this with the function clock_gettime, and again with REAL_TIME timer).
sometimes when the thread wake up, the delta is not X nano second (it can be sometimes less or high above
X nanosecond)
I am connected to NTP.

1. why sometimes the thread wake up before or after X nano second (I see this through the delta)
2. if the reason is the NTP and REAL_TIME timer, why the thread wake up after it finish to sleep
and the delta is less than X nano second ?

Thanks
# 2  
Old 12-08-2012
It would help if you provide some factual data like what OS you are using, how long does the task is asked to wait and how long does it actually does it.
I any case, on a POSIX compliant system, the clock_nanosleep call might last more than expected because the delay might be rounded up or the system might bee busy doing something else. It shouldn't be shorter unless your process is interrupted by something else.
# 3  
Old 12-08-2012
In addition to what jlliagre said, NTP will occasionally cause the real_time clock to shift forwards or backwards to align with the external time source. If your system supports the monotonic clock option, you could try using CLOCK_MONOTONIC instead of CLOCK_REALTIME.

You haven't said anything about the return value you observed from clock_nanosleep(). If it returned 0, it should not have awakened early according to the clock you specified in your call. If it returned early due to a signal, it was a relative sleep, and the last argument to clock_nanosleep() wasn't a null pointer then clock_nanosleep() would have stored the amount of time remaining on the requested timeout in the timespec structure pointed to by that last argument.
# 4  
Old 12-08-2012
More information

Hi
Thanks for your answer.
- I'm using red-hat.
- the thread which is periodic has the highest priority among all the other threads.
- when the thread wake up, I check the last parameter (remain parameter) to see if the thread was interrupt, but it hasn't.

so what could be the reason that the delta (which I check with the clock_gettime function) is less than the amount of time is need to be ?
(if I look at clock_nanosleep it seem the delta is OK, but not if we look at the delta from clock_gettime)
# 5  
Old 12-08-2012
"highest priority" has to be a realtime class - Red Hat MRG kernel.

Otherwise you will get some latency - what you are observing when the thread wakes up late. As Don said check return codes to see why the code wakes up early.

clock_gettime will reflect NTP updates to delta time as you observe it. Do you run
Code:
ckconfig ntpdate on

at startup. The reason I ask is: if the system is off time-wise
then ntp makes repeated small changes to correct the system clock. While your clock is off ntp will increment/decrement time in small intervals, so that the system clock may change relative to the clock ticks nanosleep is using to measure time.

Kind of like Relativity on a micro scale.

What does your drift file show? It is usually /etc/ntp/drift unless changed by the configuration. If the absolute value of number there is more than a few ms, then ntp will be working like mad to correct the time. It also will allow you to infer which direction ntp is attempting to correct time.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. What is on Your Mind?

Vuejs Periodic Table by Kadin Zhang

Was working on Vue.js and stumbled upon this beautiful Vue project by Kadin Zhang Periodicity is a dynamic periodic table built with Vue.js that animates and graphs data to aid the visualization of chemical concepts. The code is available on GitHub (2 Replies)
Discussion started by: Neo
2 Replies

2. Shell Programming and Scripting

awk Script: removing periodic boundaries

SOLVED, thank you! Edit2: Good news everyone, I managed to get it down to a "simple" problem, but I still have some syntax issues. Here is the code which troubles me: awk 'BEGIN{x2=0;x1=0;crit=0;} $1 < 1000000 {x2=$4; diffx=x2-x1; x1=x2; diffx > 3.6 ? {crit=1} : {crit=0};... (2 Replies)
Discussion started by: Consti
2 Replies

3. Ubuntu

Wireless periodic drops - Ubuntu 12.10

Ubuntu wireless has been fine until fresh installation of 12.10. Now, I have periodic drops of the wireless. I can manually disconnect and then reconnect to get the service back. Sometimes, I am unable to disconnect. When this happens, I have to log off and then on again. (0 Replies)
Discussion started by: jamarsh
0 Replies

4. UNIX for Dummies Questions & Answers

Remove 1st character in periodic lines

Hi, I have a file that looks like this, the unity of information is composed of four lines, and these extends for millions. My objective is to remove the highligthed "T". How to attack this? This character is always constant in type "T" and position "1st" but the rest of the line is... (7 Replies)
Discussion started by: sargotrons
7 Replies

5. Cybersecurity

Periodic check of user password strength

I need to periodically run a check on the passwords of the users (Redhat 5.0) to verify that all passwords meet minimal standards. I remember seeing a script years ago that grabbed the encrypted passwords from the file and checked if they matched any of the encrypted strings in another file, plus... (1 Reply)
Discussion started by: tlynnch
1 Replies

6. Shell Programming and Scripting

Tcsh periodic requires carriage return

Hello All, My first post on this forum. I am using cygwin to help me with some routine jobs on my laptop, with tcsh as my shell. I need to copy a set of files from one directory to a network drive every minute. So I have set tperiod = 1 alias periodic 'cp *.txt /cygdrive/z/Data' ... (0 Replies)
Discussion started by: OmniVision
0 Replies

7. UNIX for Dummies Questions & Answers

create a periodic execution of a script?

Hello every body goal: create a script that control periodicly ( every 30 min ) if a process is already actif. How can I do that? thanks (3 Replies)
Discussion started by: hoang
3 Replies
Login or Register to Ask a Question