03-15-05 10: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;
}
[ Post a follow-up to this message ]
|