Timers

G

George

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
 
J

Jens.Toerring

George said:
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
 
S

SM Ryan

(e-mail address removed) (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.
 
T

Thomas Matthews

George said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top