LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   What happens to a timer if you alter the clock (https://www.linuxquestions.org/questions/linux-general-1/what-happens-to-a-timer-if-you-alter-the-clock-567712/)

Animalector 07-08-2007 11:29 PM

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;


wjevans_7d1@yahoo.co 07-09-2007 05:14 PM

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.

Animalector 07-09-2007 05:31 PM

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

wjevans_7d1@yahoo.co 07-10-2007 08:53 AM

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.

nx5000 07-10-2007 08:56 AM

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

http://lwn.net/Kernel/LDD3/

Animalector 07-10-2007 07:19 PM

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


All times are GMT -5. The time now is 07:52 PM.