How to trigger an event every 5 minutes

J

Jon Zero

I am trying to figure out what the best way to have my program do
something (change the value of a variable) every x number of minutes. Can
somebody give me a pointer in the right direction?

Jon
 
R

Richard Bos

Jon Zero said:
I am trying to figure out what the best way to have my program do
something (change the value of a variable) every x number of minutes. Can
somebody give me a pointer in the right direction?

In ISO C, the only option, and even then it's not an ideal one, is to
have a busy-loop inside which you check the elapsed time using time()
and difftime(). This fails, of course, if you use an input function and
it waits for longer than x minutes before it receives the next newline.
There are rather better system-specific solutions to this; for example,
if you're on a Unixoid, comp.unix.programmer should be able to help you,
and there is something similar under MS Windows.

Richard
 
T

Thomas Matthews

Jon said:
I am trying to figure out what the best way to have my program do
something (change the value of a variable) every x number of minutes. Can
somebody give me a pointer in the right direction?

Jon
The best method, if your operating system supports it,
is to create a function to process your variable, then
tell the operating system to execute it every X minutes.
Since this is operating system specific, you'll have to
discuss it in an OS specific newsgroup dedicated to your
OS.

Another technique is to read the clock value, add X
minutes to the time and save as the update time.
Periodically, read the clock. If the update time
has passed, update your variable.

There are other portable techniques that involve
Operating System techniques, which take up more
space than we have here.

--
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
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jon Zero

In ISO C, the only option, and even then it's not an ideal one, is to
have a busy-loop inside which you check the elapsed time using time()
and difftime(). This fails, of course, if you use an input function and
it waits for longer than x minutes before it receives the next newline.
There are rather better system-specific solutions to this; for example,
if you're on a Unixoid, comp.unix.programmer should be able to help you,
and there is something similar under MS Windows.

Yes, I should have mentioned that I am writing on a modern linux machine.
I did think about the time/difftime, but I would want this event to happen
no matter what my program is doing (even sitting waiting for input).

Jon
 
K

Keith Thompson

Jon Zero said:
Yes, I should have mentioned that I am writing on a modern linux machine.
I did think about the time/difftime, but I would want this event to happen
no matter what my program is doing (even sitting waiting for input).

Try comp.unix.programmer. There's no portable C solution.
 
R

Rajesh B.K

If you are using a flavour of unix then use the "alarm" API to generate
SIGALRM after the time you have specfied.
Register your funtion that is intended to run, for SIGALRM signal using the
API "signal".The man pages should be able to help you more.Not sure on the
windows platform.

Regards,
Rajesh B.K
 

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
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top