LinuxQuestions.org
Visit Jeremy's Blog.
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 07-08-2007, 11:29 PM   #1
Animalector
LQ Newbie
 
Registered: Jun 2005
Posts: 10

Rep: Reputation: 0
What happens to a timer if you alter the clock


I have a timer setup to initialise at the nsec = 500 000 000 mark. with an interval of sec = 1

SO in theory it goes off mid second every second.

OK so, My question is:

What happens to the timer if I now by alternate process, modify the Linux clock? for example:

if
sec = 53, nsec = 800 000 000
and I force clock to
sec = 54, nsec = 0,
Will the timer then go off again at sec = 54, nsec = 500 000 000.

my code:

Code:
char *tz;
struct itimerspec AbsTimer;
struct timespec	TimeNow;
setenv("TZ", "GMT0", 1);
tzset();

if ((tz = getenv("TZ")) != NULL) {
		syslog(LOG_INFO, "Timezone: Name=%s Offset=%ld Daylight=%d\n", tzname[0], timezone, daylight);
}

   // set up the signal handler to catch signals from CTRL-C (SIGINT)
   // and slay or kill (SIGTERM) and the absolute timer (SIGALRM)
action.sa_handler = SignalHandler;
sigemptyset(&action.sa_mask);

if((sigaddset(&action.sa_mask, SIGTERM) == -1) ||
		(sigaddset(&action.sa_mask, SIGINT) == -1) ||
      (sigaddset(&action.sa_mask, SIGALRM) == -1)) {
      syslog(LOG_INFO, "%m");
      return -1;
}
action.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &action, NULL );
sigaction(SIGINT, &action, NULL );
sigaction(SIGALRM, &action, NULL );

   if (timer_create(CLOCK_REALTIME, NULL, &AbsTimerId) < 0) {
      syslog(LOG_INFO, "%m");
      return -1;
   }
   
   clock_gettime(CLOCK_REALTIME, &TimeNow);
   
   AbsTimer.it_value.tv_sec = TimeNow.tv_sec;
   AbsTimer.it_value.tv_nsec = 500000000;
   AbsTimer.it_interval.tv_sec = 1;
   AbsTimer.it_interval.tv_nsec = 0;

   // arm the absolute timer
   if (timer_settime(AbsTimerId, TIMER_ABSTIME, &AbsTimer, NULL) == -1) {
     	syslog(LOG_INFO, "%m");
     	return -1;
   }
   return 0;

Last edited by Animalector; 07-09-2007 at 05:32 PM.
 
Old 07-09-2007, 05:14 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
I don't have the exact answer, so I would not have jumped in before now. But this question has been hanging for enough hours that I would like to make the following observations.

First, your code is more easily modifiable if you use #define for defining certain constaints, for example in assignments to it_value.tv_nsec, it_interval.tv_sec, and it_interval.tv_nsec. Put these #defines near the beginning of your code, all in one place.

Second, once you've done that, you can more easily play with those values and run experiments of your own. Make the pause interval longer, for example, and modify the time yourself in a separate window while your program's running, to see what the effect is.

Third, to maximize readability, when you post code, please put it between CODE tags. This is most easily done as follows:
  1. Click the Go Advanced icon at the bottom of your editing window.
  2. Highlight the code itself in the new editing window.
  3. Click the # icon at the top of your editing window.

This keeps something like this:

Code:
#include <stdio.h>

int main(void)
{
  printf("Hello, world!\n");

  return 0;

} /* main() */
from looking ugly, like this:

#include <stdio.h>

int main(void)
{
printf("Hello, world!\n");

return 0;

} /* main() */

Fourth, when you post code, if at all possible, isolate the code as a separate, free-standing program that people can compile (which means you've already included the necessary #include statements) and run. This lets them run experiments. Heck, maybe you can run those experiments yourself and save waiting all this time for an answer.

Hope this helps in some way.
 
Old 07-09-2007, 05:31 PM   #3
Animalector
LQ Newbie
 
Registered: Jun 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Hey thanks for that, future posts I will be sure to use the "code" feature.

Unfortunately, the code is pretty deeply embedded into a complete package which I am trying to maintain, the previous software engineer has been gone for a long time.

It all comes down to how the timer runs.. if the OS assumes a "counter" e.g, for 1 sec interval.. count to ten.. then if I change the time, the counter value keeps running and will spit my out at a different time.. (wrong by however much I altered the clock).

Alternatively, if the OS uses a "compare", ie for one sec interval at the 0.5s, if (nsec == 500 000 000, and sec = old sec + 1) trigger timer; this will accept the altered clock and be cool..

Maybe I need to read more documentation about the implementation of the timers¿¿¿???

Thanks so far,
Andy
 
Old 07-10-2007, 08:53 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Well, reading documentation never kills ya. (grin)

But you may wish to play with those timer calls in a separate, toy program.

Just a thought.
 
Old 07-10-2007, 08:56 AM   #5
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
You can see the new functions of the timer (before, after,..) in chapter 7 of this _excellent_ book:

http://lwn.net/Kernel/LDD3/
 
Old 07-10-2007, 07:19 PM   #6
Animalector
LQ Newbie
 
Registered: Jun 2005
Posts: 10

Original Poster
Rep: Reputation: 0
ok, great reference! thank you for that, although I must say I think that's a bit of information overload for me.

From what i read, timers work on jiffies...

- Jiffies are an absolute counter, initialised at start time.

- It is used for short term timing, and is not necessarily related to RTC (system time)

- The linux timer API uses these jiffies as a counter..

- Initialising a counter has a start jiffy, length of jiffies, target jiffy

- So changing the RTC will have (in my understanding) ZERO IMPACT ON THE TIMER which continues to expire after defined number of jiffies.

This is good for me because it explains experimental clock jitter.

if anyone has a different opinion / understanding of the material I just read.. let me know...

Otherwise, Thank you.

Andy
 
  


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
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 03:49 PM
Hardware bug:clock timer configuration lost mathewzhao Slackware 2 07-11-2006 01:32 PM
Linux OS Clock/Timezone shows the clock off by 6 hours between OS's JBailey742 Linux - Software 9 04-06-2006 11:40 PM
Synchronising system clock to UTC while using a timer Animalector Programming 0 03-28-2006 01:45 AM
clock timer problem Half_Elf Linux - General 2 10-02-2004 05:19 PM

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

All times are GMT -5. The time now is 10:47 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