How to calculate how long it elapsed despite of user changing ofsystem time? - Unix

This is a discussion on How to calculate how long it elapsed despite of user changing ofsystem time? - Unix ; Hi, In my program, there is a logic which calculates how long the time passed since a program status was determined, it can be illustrated as blow: time_t prev_time; void when_foo_happened(void) { if (time(NULL) - prev_time > 2) { do_bar(); ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: How to calculate how long it elapsed despite of user changing ofsystem time?

  1. How to calculate how long it elapsed despite of user changing ofsystem time?

    Hi,

    In my program, there is a logic which calculates how long the time
    passed since a program status was determined, it can be illustrated as
    blow:

    time_t prev_time;
    void when_foo_happened(void)
    {
    if (time(NULL) - prev_time > 2) {
    do_bar();
    prev_time = time(NULL);
    }
    }

    And, the when_foo_happened(void) routine will be called from another
    piece of code which usually in a dead loop that also takes care of
    something else.

    My problem is, between two calls of when_foo_happened(), the value of
    system time may be largely changed by user or another user process.
    So I can not ensure the do_bar() will get called in a time interval of
    about 2 seconds.

    I guess my problem is caused by the using of time(NULL) function. But
    I don't prefer to use something like alarm(), since an asynchronous
    signal may break one of my read() operation which is waiting in other
    piece of code.

    So, my question: where can I read a stable time value which will
    always go forward and never be changed by other process?

    Thanks.

    -
    narke

  2. Re: How to calculate how long it elapsed despite of user changingofsystem time?

    On Thu, 25 Sep 2008 01:22:40 -0700, Steven Woody wrote:

    > So, my question: where can I read a stable time value which will always
    > go forward and never be changed by other process?


    So you want some kind of monotonic clock?

    SYNOPSIS
    #include
    int clock_gettime( CLOCK_MONOTONIC, struct timespec *tp );

    CONFORMING TO
    SUSv2, POSIX.1-2001.


  3. Re: How to calculate how long it elapsed despite of user changing ofsystem time?

    On Sep 25, 4:35 pm, viza
    wrote:
    > On Thu, 25 Sep 2008 01:22:40 -0700, Steven Woody wrote:
    > > So, my question: where can I read a stable time value which will always
    > > go forward and never be changed by other process?

    >
    > So you want some kind of monotonic clock?
    >
    > SYNOPSIS
    > #include
    > int clock_gettime( CLOCK_MONOTONIC, struct timespec *tp );
    >
    > CONFORMING TO
    > SUSv2, POSIX.1-2001.


    It seems what I desired, I will try it! There is one sentence I can
    not understand in the man page, maybe you would help:

    All implementations support the system-wide realtime clock,
    which is
    identified by CLOCK_REALTIME. Its time represents seconds and
    nanosec-
    onds since the Epoch. When its time is changed, *timers for a
    relative
    interval are unaffected, but timers for an absolute point in
    time are
    affected.*

    What I don't understand is marked between *...*

    Thanks.

  4. Re: How to calculate how long it elapsed despite of user changing of system time?

    Steven Woody writes:
    > On Sep 25, 4:35 pm, viza
    > wrote:
    >> On Thu, 25 Sep 2008 01:22:40 -0700, Steven Woody wrote:
    >> > So, my question: where can I read a stable time value which will always
    >> > go forward and never be changed by other process?

    >>
    >> So you want some kind of monotonic clock?
    >>
    >> SYNOPSIS
    >> #include
    >> int clock_gettime( CLOCK_MONOTONIC, struct timespec *tp );
    >>
    >> CONFORMING TO
    >> SUSv2, POSIX.1-2001.

    >
    > It seems what I desired, I will try it! There is one sentence I can
    > not understand in the man page, maybe you would help:
    >
    > All implementations support the system-wide realtime clock,
    > which is
    > identified by CLOCK_REALTIME. Its time represents seconds and
    > nanosec-
    > onds since the Epoch. When its time is changed, *timers for a
    > relative
    > interval are unaffected, but timers for an absolute point in
    > time are
    > affected.*
    >
    > What I don't understand is marked between *...*


    The realtime clock can be changed administratively. The monotonic
    clock cannot. This means for as long as you are not trying to
    sychronize actions on different computers and don't need timestamps
    valid accross a reboot, the monotonic clock (if
    available) is a better choice for measurements.

  5. Re: How to calculate how long it elapsed despite of user changingofsystem time?

    Hi

    On Thu, 25 Sep 2008 06:48:41 -0700, Steven Woody wrote:
    > On Sep 25, 4:35 pm, viza
    >>
    >> So you want some kind of monotonic clock?
    >> int clock_gettime( CLOCK_MONOTONIC, struct timespec *tp );


    > It seems what I desired, I will try it! There is one sentence I can not
    > understand in the man page, maybe you would help:
    >
    > All implementations support the system-wide realtime clock, which is
    > identified by CLOCK_REALTIME. Its time represents seconds and
    > nanoseconds since the Epoch. When its time is changed, *timers for a
    > relative interval are unaffected, but timers for an absolute point in
    > time are affected.*


    That refers to the relationship between these clocks and the ones used by
    getitimer(2) etc, which provide an alternative way of doing the same
    thing. Another (linux specific) alternative is timerfd_create(2).

  6. Re: How to calculate how long it elapsed despite of user changing ofsystem time?

    On Sep 25, 11:09 pm, viza
    wrote:
    > Hi
    >
    > On Thu, 25 Sep 2008 06:48:41 -0700, Steven Woody wrote:
    > > On Sep 25, 4:35 pm, viza

    >
    > >> So you want some kind of monotonic clock?
    > >> int clock_gettime( CLOCK_MONOTONIC, struct timespec *tp );

    > > It seems what I desired, I will try it! There is one sentence I can not
    > > understand in the man page, maybe you would help:

    >
    > > All implementations support the system-wide realtime clock, which is
    > > identified by CLOCK_REALTIME. Its time represents seconds and
    > > nanoseconds since the Epoch. When its time is changed, *timers for a
    > > relative interval are unaffected, but timers for an absolute point in
    > > time are affected.*

    >
    > That refers to the relationship between these clocks and the ones used by
    > getitimer(2) etc, which provide an alternative way of doing the same
    > thing. Another (linux specific) alternative is timerfd_create(2).


    So much thanks for all your help!

+ Reply to Thread