Re: triggering an event every 5 minutes in C

Jens.Toerring_at_physik.fu-berlin.de
Date: 03/15/05


Date: 15 Mar 2005 17:42:45 GMT

Jon Zero <jzero@nospam.net> wrote:
> 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? I am writing in C in
> a linux environment.

The simplest method is probably to have a SIGALARM signal delivered
to your process after a certain time. Use setitimer(2) to request
the signal after installing a signal handler (using sigaction(2))
for the signal. Within the signal handler you can set the variable,
but you may have to make it a volatile variable since otherwise
the change may not be noticed immediately in the "normal" program
(if it has been copied to a register while the signal handler up-
dates the in-memory version). If any possible make the type of the
variable 'sig_atomic_t', that's the type where you are guaranteed
that it will work (since it can be accessed as an atomic entity)
and it has integer type. Avoid doing much more than changing the
variable in the signal handler, many functions can't be used
reliably there. All of this should work on every POSIX compliant
system, not just Linux.
                                    Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages