John Wendel wrote:
I'm not an expert on this stuff, but my understanding is that setitimer
gives your process access to one of three predefined timers (REAL,
VIRTUAL, and PROF) and only the REAL timer is actually useful.
So your second call to setitimer is mucking up the settings from the
first call.
See "man timer_create" for the POSIX timer stuff that lets you have lots
of timers.
I recommend the O'REILLY book POSIX 4 by Bill Gallmeister for a good
explanation of this fairly complicated topic.
Regards,
John
I just tried the following experiment and it worked fine with 4 timers!
I must be doing something stupid in my other code...
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <signal.h>
#include <sys/time.h>
void catch_signal1(int ignored);
void catch_signal2(int ignored);
void catch_signal3(int ignored);
void catch_signal4(int ignored);
void catch_signal1 (int ignored) {
fprintf(stderr,"Timer 1 caught...\n");
}
void catch_signal2 (int ignored) {
fprintf(stderr,"Timer 2 caught...\n");
}
void catch_signal3 (int ignored) {
fprintf(stderr,"Timer 3 caught...\n");
}
void catch_signal4 (int ignored) {
fprintf(stderr,"Timer 4 caught...\n");
}
main(int argc, char **argv) {
start_timer1(1,catch_signal1);
start_timer1(2,catch_signal2);
start_timer1(3,catch_signal3);
start_timer1(8,catch_signal4);
while(1) {
pause();
}
}
pid_t start_timer1 (int interval, void(*func)(void)) {
pid_t child;
struct itimerval it;
struct sigaction sa;
if (!(child = fork())) {
memset(&sa, 0, sizeof(sa));
sa.sa_handler = func;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
memset(&it, 0, sizeof(it));
it.it_interval.tv_sec = interval;
it.it_value.tv_sec = interval;
setitimer(ITIMER_REAL, &it, NULL);
while (1) {
pause();
}
}
return child;
}