Timers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George

    #1

    Timers

    I have created a program that sends msgs at certain rate. What is the
    best way to do this so that I get a consistent rate all the time?
    For example I want to send 5000 msgs per second, and 1000 msgs per
    second.
    I have used "usleep" and most of the time I get way less than what I
    expect mainly do to system interrupt.

    How do I get time to print like the following 3:12:30.8388483 8?

    Thanks
    George
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Timers

    George <geo_white55@ya hoo.com> wrote:[color=blue]
    > I have created a program that sends msgs at certain rate. What is the
    > best way to do this so that I get a consistent rate all the time?
    > For example I want to send 5000 msgs per second, and 1000 msgs per
    > second.
    > I have used "usleep" and most of the time I get way less than what I
    > expect mainly do to system interrupt.[/color]

    There are a lot of problems here. First of all, there's no solution
    from a standard C point of view - the standard does not make any
    promises about the execution speed of a program etc. But even if
    you resort to (non-standard) extensions like the use of usleep()
    achieving what you want to do might be impossible because, unless
    you have a real-time (or a single-user, single-tasking) operating
    system there are typically hardly any ways that guarantee a reliable
    timing.

    <OT>
    Take for example usleep(). On the systems I know it to exist it puts
    the process to sleep and just guarantees that the process won't get
    woken up before the time passed as the argument is over - but there
    is no implicit promise that the process will be woken up the moment
    that time is over.
    </OT>

    So your only option is to ask the question in a newsgroup that is
    dedicated to the operating system you are using. Perhaps it's
    possible to achieve the time resolution you need, but you better
    don't hold your breath. But perhaps there are also other approaches
    to a solution of your problem which don't require such an exact
    timing...
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • SM Ryan

      #3
      Re: Timers

      geo_white55@yah oo.com (George) wrote:
      # I have created a program that sends msgs at certain rate. What is the
      # best way to do this so that I get a consistent rate all the time?
      # For example I want to send 5000 msgs per second, and 1000 msgs per
      # second.
      # I have used "usleep" and most of the time I get way less than what I
      # expect mainly do to system interrupt.

      Do something like
      base-time = current-time
      for each send i
      t = base-time + i*send-interval
      while current-time < t
      sleep t-current time
      send message i
      The actual period will vary slightly, but overall it should correct
      back to the desired frequency.

      # How do I get time to print like the following 3:12:30.8388483 8?

      Depends how you're representing time. You might be able to trick
      strftime, but I would just do the conversion and format explicitly.
      If your time value is a single integer, you can do something like
      long nanoseconds = time%1000000000 ;
      time /= 1000000000;
      long seconds = time%60;
      time /= 60;
      long minutes = time%60;
      long hours = time/60;
      printf("%d:%02d :%02d.%09d",hou rs,minutes,seco nds,nanoseconds );
      If you time value is a struct, you'll have to pull nanoseconds and
      seconds out of the appropriate fields.

      --
      SM Ryan http://www.rawbw.com/~wyrmwif/
      I love the smell of commerce in the morning.

      Comment

      • Thomas Matthews

        #4
        Re: Timers

        George wrote:[color=blue]
        > I have created a program that sends msgs at certain rate. What is the
        > best way to do this so that I get a consistent rate all the time?
        > For example I want to send 5000 msgs per second, and 1000 msgs per
        > second.
        > I have used "usleep" and most of the time I get way less than what I
        > expect mainly do to system interrupt.
        >
        > How do I get time to print like the following 3:12:30.8388483 8?
        >
        > Thanks
        > George[/color]

        The best method is to have your operating system
        execute your function at a given time interval.
        This is all operating system specific and best
        answered in a newsgroup about your O.S.

        For example, if your platform has an interrupt
        that fires every millisecond, you could have
        the interrupt call your function that sends out
        a message (one of the 1000 per second).

        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        Working...