I'd like to be able to have a function (that takes about one second) to run every minute. It samples low frequency system data and generates a plot. I'd really like one point per minute. Any thoughts would be helpful. Here's what I've tried and the problems I've had:
  1. Used sleep 60 at the end of the function. This gives a roughly 61 second period for executing the function. (Because the function takes about a second to execute.) That means that data for some minutes will be skipped.
  2. Used alarm 60 in a signal handler for $SIG{ALRM}. The clock seems to creep with this, too, but I am not sure why. It looks as though I get an interrupt every 60.5 seconds. I'll still lose data points this way.
  3. Used
    select undef, undef, undef, 1.0; run_func if ! (time%60);
    This seemed to skip every other minute for reasons I cannot figure out.
In cases 1 and 2 above, I've sat in a wait loop (wait while (1);) as the body of the program.

I'm using perl 5.005_003 on Linux 2.2.16 (mostly Red Hat). If it makes any difference, the function (run_func above) is a read of about 50 lines using XML::Simple, a few additions, and a plot made via GD::Graph.

--traveler


In reply to Reasonably accurate timing by traveler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.