Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Feb 2005 10:44:31 +0000
From:      Peter Edwards <peadar.edwards@gmail.com>
To:        Julian Elischer <julian@elischer.org>
Cc:        current@freebsd.org
Subject:   Re: cynchronised sleep capbilty..
Message-ID:  <34cb7c8405020202446192b3bb@mail.gmail.com>
In-Reply-To: <20050201130340.D92335@localhost>
References:  <20050201101113.J572@localhost> <20050201190318.GE45608@cirb503493.alcatel.com.au> <20050201130340.D92335@localhost>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 1 Feb 2005 13:20:15 -0800 (PST), Julian Elischer
<julian@elischer.org> wrote:
> 
> 
> On Wed, 2 Feb 2005, Peter Jeremy wrote:
> 
> > On Tue, 2005-Feb-01 10:20:24 -0800, Julian Elischer wrote:
> > >while:
> > >do
> > >     report results
> > >     sleep -until_next 10
> > >done
> >
> > How about:
> > 1) Re-write the loop in C, perl or equivalent using setitimer().  You
> >    can system() out to collect the results.
> > 2) <kludge>Write a small C program that uses setitimer() and signals
> >    its parent whenever the timer triggers.  Run it in the background
> >    and just pause within the sh loop.</kludge>
> >
> >
> 
> this is what I ended up doing..
> 
> # like sleep 10 except that it phase locks to teh 10 second boundary
> # so that multiple machines are talking about the same sample period.

I do something similar in C that requires no long-term drift, but it's
a little more general: Use an absolute time for sleeps, rather than
relative to "now". e.g.:

time_t wakeup = time(0);
while (!done) {
    // avoid multiple firings if "dostuff()" takes longer than interval
    while (wakeup < time(0))
        wakeup += interval;
    sleepUntil(wakeup);
    dostuff();
}

Where sleepUntil can be implemented either as a sleep relative to
wakeupTime - now, or use a pthreads function like
pthread_cond_timedwait(), for example. The shell code would probably
be equally simple



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?34cb7c8405020202446192b3bb>