Re: cynchronised sleep capbilty..

From: Peter Edwards <peadar.edwards_at_gmail.com>
Date: Wed, 2 Feb 2005 10:44:31 +0000
On Tue, 1 Feb 2005 13:20:15 -0800 (PST), Julian Elischer
<julian_at_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
Received on Wed Feb 02 2005 - 09:44:35 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:27 UTC