Timers

Discussion in 'C Programming' started by George, Aug 10, 2004.

  1. George

    George Guest

    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.83884838?

    Thanks
    George
    George, Aug 10, 2004
    #1
    1. Advertising

  2. George

    -berlin.de Guest

    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.


    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 ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 11, 2004
    #2
    1. Advertising

  3. George

    SM Ryan Guest

    (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.83884838?

    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",hours,minutes,seconds,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.
    SM Ryan, Aug 11, 2004
    #3
  4. 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.
    >
    > How do I get time to print like the following 3:12:30.83884838?
    >
    > Thanks
    > George


    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:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 11, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Inkpen

    Timers in application web programming

    Stephen Inkpen, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    Ken Cox [Microsoft MVP]
    Jul 16, 2003
  2. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    713
    Kelsang Wangchuk
    Jul 31, 2003
  3. Girish Pal Singh

    timers

    Girish Pal Singh, Aug 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    562
    Kevin Spencer
    Aug 11, 2003
  4. Girish Pal Singh

    Web Timers

    Girish Pal Singh, Aug 13, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    443
    George Ter-Saakov
    Aug 13, 2003
  5. Steve C. Orr [MVP, MCSD]

    Re: timers

    Steve C. Orr [MVP, MCSD], Dec 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    393
    Steve C. Orr [MVP, MCSD]
    Dec 8, 2003
Loading...

Share This Page