Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Timers

Reply
Thread Tools

Timers

 
 
George
Guest
Posts: n/a
 
      08-10-2004
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
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-10-2004
George <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      08-11-2004
http://www.velocityreviews.com/forums/(E-Mail 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,n anoseconds);
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.
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      08-11-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timers spf R Siffredi Cisco 1 10-31-2005 05:11 PM
Web Timers Girish Pal Singh ASP .Net 4 08-13-2003 08:59 PM
timers Girish Pal Singh ASP .Net 2 08-11-2003 01:00 PM
System.Timers.Timer vs. System.Threading.Timer Kelsang Wangchuk ASP .Net 0 07-31-2003 04:28 PM
Timers in application web programming Stephen Inkpen ASP .Net 1 07-16-2003 02:12 AM



Advertisments