473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

Thanks
George
Nov 14 '05 #1
3 1327
George <ge*********@yahoo.com> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
ge*********@yahoo.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.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.
Nov 14 '05 #3
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

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: GDumencu | last post by:
I have a C# program that have to run all the time. For some reasons I cannot make it a service. This program has 3 timers and by time to time ( days or weeks) one of them stops. Some times, starts...
0
by: Dmitry Demchuk | last post by:
Hi everybody. Recently I ran into situation with System.Threading.Timer in my ASP.NET application. I reinstalled Windows on one of my servers and got timers stop firing events after while, they...
0
by: Cider123 | last post by:
I was originally using: System.Windows.Forms.Timer It started to lock my Window Service up, so I went to the next evolution: System.Threading.Timer All was good. I was using it to send...
3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
3
by: Jeff Greenland | last post by:
Hello everyone, I am having problems with Timers in a web application. They just seem to stop running after 15 minutes or so. My web application is set up like this: When a user hits a...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
1
by: | last post by:
Frustrated.. (I have seen other posts regarding this problem with no resolution..) I am using dotnet 1.1 with latest SP on a Win2KP box (actually 2 boxes), have even run the service on WinXP SP2...
1
by: Jonathan Woods | last post by:
Hi there, I have three methods these need to execute at every interval time. I would like to know which option is better? Option A) Three System.Timers.Timer objects these execute each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.