Unix Programming - triggering an event every 5 minutes in C

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2005 > triggering an event every 5 minutes in C





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author triggering an event every 5 minutes in C
Jon Zero

2005-03-15, 5:59 pm

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.

Jon

John Smith

2005-03-15, 5:59 pm

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


One way is to start a thread, sleep for 5 mins, change the variable and go
to sleep again.

Look into pthreads (threads and mutex since you need to have to establish a
lock between the threads accessing your variable).

-- John


Jens.Toerring@physik.fu-berlin.de

2005-03-15, 5:59 pm

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
Sunny Suen

2005-03-15, 5:59 pm

John Smith wrote:
minutes. Can[vbcol=seagreen]
in C in[vbcol=seagreen]
>
> One way is to start a thread, sleep for 5 mins, change the variable

and go
> to sleep again.
>
> Look into pthreads (threads and mutex since you need to have to

establish a
> lock between the threads accessing your variable).
>
> -- John


Try the alarm (2) and sigaction (2) system calls:
unsigned int alarm(unsigned int sec) from <unistd.h>
int sigaction(int sig, const struct sigaction *act, struct sigaction
*oact) from <sigal.h>;


An example about setting an action timer to do something every 300 secs
through an auxiliary routine, say action_handler():

int time = 300;
int set_action_timer(int time) {
struct sigaction act, oact;
int n;

act.sa_handler = &action_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;

if (sigaction(SIGALRM, &act, &oact) < 0) {
perror("Error: sigaction(SIGALRM)");
exit(1);
}

n = alarm(time);
return n;
}

void action_handler(int signal) {
if (signal == SIGALRM) {
/* do your stuff here */
set_action_timer(300);
}
return;
}

Måns Rullgård

2005-03-15, 5:59 pm

Jon Zero <jzero@nospam.net> writes:

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


man setitimer

--
Måns Rullgård
mru@inprovide.com
David Schwartz

2005-03-15, 5:59 pm


"Jon Zero" <jzero@nospam.net> wrote in message
news:pan.2005.03.15.17.04.30.947909@nospam.net...

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


It depends a lot upon the whole structure of the rest of your program.
For example, if it's multi-threaded, the answer is totally different. If
it's an event loop, the answer is totally different.

DS


Pascal Bourguignon

2005-03-16, 2:53 am

Jon Zero <jzero@nospam.net> writes:

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


Read David's answer.

In most cases something like could work, but be sure to read the man
pages of signal(2) and alarm(2):


int variable=0;

int change_variable(int signal){
variable++;
alarm(300); }

int main(){
signal(SIGALRM,change_variable);
alarm(300);
int last=-1;
while(1){
if(last!=variable){
last=variable;
printf("%d\n",variable); }}}



--
__Pascal Bourguignon__ http://www.informatimago.com/

Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com