UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2005-12-16 04:08 PM

shiva
Member
From: INDIA
Registered: 2002-08-10
Posts: 94

Re: POSIX timers

I'm using POSIX timers to simulate the system clock and generating interrupts for every 200 milli seconds. POSIX timers provides list of APIs to implement this like timer_create/timer_settime().

Everything works cooly, but when I change my system time forward by 10 min, the timer generates ticks without any problem. But when move back by 10 min from this point, the timer doesn't generate any interrupts after this. The timer wait till the 10 min to elapse.I'm missing the real time ticks during this period. I'm using REALTIME_CLOCK timer in create timer call.

Has anybody info on this behaviour or POSIX timers supports only for real time??? Any other option required to handle this scenario.

Help on this topic is higly appreciated.

Thanks in advance
Shiva

Offline

#2 2005-12-16 04:28 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: POSIX timers

Use CLOCK_MONOTONIC if you want to avoid such things.

Offline

#3 2005-12-17 12:59 PM

shiva
Member
From: INDIA
Registered: 2002-08-10
Posts: 94

Re: POSIX timers

Thanks for your reply. I'm using  SOLARIS 8 and found the CLOCK_MONOTONIC doesn't support in Solaris. This is supported only in Red Hat linux it seems. Any other alternative approach?

Even I tried the timer_settime() with flag as TIMER_ABSTIME to use the relative value in stead of the real value. This also works fine with forward time difference not in the backward time diiference.

I'm looking for some more discussion on this topic.Why the POSIX timer expiration uses the system clock to expire/generate signal? Is this a bug or some patch is already released. It seems the POSIX timer APIs itself has some issue when we change the system time backward.

Any info on this topic???

Thanks in advance
Shiva

Offline

#4 2005-12-17 07:39 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: POSIX timers

With TIMER_ABSTIME you ask for absolute time, that is, wall clock times, so you just get what you ask for. If you want an always increasing wall clock independent timer then use a monotonic clock. It is no bug, it is expected behaviour as far as I can tell.

There are at least three ways to solve your problem:

1) Use a newer Solaris version which does support CLOCK_MONOTONIC (assuming newer ones do).

2) Use a linux version which does support it.

3) Just don't change your clock in strange ways: use NTP or something to keep your  time correct.

As for alternative approaches, why not use nanosleep or something similar?

Offline

#5 2005-12-18 04:08 PM

shiva
Member
From: INDIA
Registered: 2002-08-10
Posts: 94

Re: POSIX timers

Our implementation of POSIX timer should be flexible enough to handle +/- current time. We set the periodic clock shall have expiration value say 200 milli second. The system clock time shall be changing continuosly/randomly +/-  60 min of the current time.

Based on our investigation on Solaris box, it seems the timers work fine on the forward time change, but the timer doesn't raise any signals when we change the system clock backwards.

I'm yet to try the CLOCK_MONOTONIC clock type. Is this issue with timers? or the signal which we raise in the create_timer() call.

Pl clarify on this.

Thanks in advance
Shiva

Offline

#6 2005-12-18 07:50 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: POSIX timers

With CLOCK_REALTIME. you ask for absolute timers. (Sorry, where I said TIMER_ABSTIME in my previous posts I meant CLOCK_REALTIME)

Example:

The current time is 10000 (to make things simpler assume this is the wall clock time in milliseconds), and you want a timer to go off after 200 ms.

Ideally, you just made a timer and telling it to trigger after 200 ms, but you need a monotonic clock for this, and you can't use CLOCK_MONOTONIC.

The option left is to use CLOCK_REALTIME, so you ask the timer to go off at 10000 + 200 = 10200.

Somewhere between 10000 and 10200 someone adjusts the systemtime.

If it was adjusted forward, say to 11000, then the kernel detects that there's a timer which should have gone off long ago, and runs it as quickly as possible. So everything still works, although the time difference between setting the timer and the timer triggering may not be 200 ms, but less.

If the time was adjusted backwards, say to 9000, then you may be in trouble. You still have a timer wanting to go off at 10200, but now it's 9000, so you need to wait 1200 ms before your timer goes off, if the timers aren't updated.

The question remains if existing timers should also be adjusted when the system time is changed. Apparently the Solaris 8 implementation doesn't do that. If this is the correct thing to do or not depends on the posix specification, but keeping in mind that you're using CLOCK_REALTIME, it seems only natural that the timers should be updated when the system time changes. That solaris 8 doesn't do this is only more reason to upgrade (to 10 or something else, like Linux or BSD).

Yet another workaround would be to destroy existing timers and recreate them when changing the time.

Offline

#7 2005-12-19 03:09 PM

shiva
Member
From: INDIA
Registered: 2002-08-10
Posts: 94

Re: POSIX timers

Thanks for your reply.

Again how do we identify the clock time change in the user process area. Is there any registeration available with OS like callback function,to notify the clock time change info.

How do we handle this scenario to destroy the existing timer and start a new one?

-Shiva

Offline

#8 2005-12-19 07:10 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: POSIX timers

Well, as the clock change comes from a user process, you already know somewhere in userspace that you're going to change the time. So you could wrap/intercept whatever is doing the weird timechanges. There is as far as I know no callback mechanism for this.

But this is getting all very awkward, avoiding bugs or shortcommings of your OS in your program code is kind of ugly, and there's no reason at all to not upgrade your OS.

If you really want to keep using solaris 8 then just forget about the timers and make your own timer system instead. E.g. a seperate thread or process which uses nanosleep to wait till the first timer expires and sends some signal or whatever to the thread/process which set the timer. Should be easy enough and avoids signal mess too when you use pthread or something instead.

Offline

#9 2008-07-11 01:18 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: POSIX timers

HI,
i am trying to write a similar code.
the thing is i am newbie to Linux. I would like you to give me hints how to start? what books, tutorials to read if u have any in mind? or code examples?
thanks

Offline

#10 2008-07-11 01:53 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: POSIX timers

Can you be more specific?

Also to prove that you aren't a bot, they tend to post semi-random posts nowadays...

Offline

#11 2008-07-11 01:56 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: POSIX timers

Offline

#12 2008-07-11 02:33 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: POSIX timers

See the manpage of setitimer() or timer_create(), that's probably a good start.

Offline

#13 2008-10-11 11:35 AM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: POSIX timers

Offline

#14 2008-10-12 10:59 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: POSIX timers

Ah, good to hear that. Good luck with your next (or current) project.

Offline

Board footer

Powered by FluxBB