Listing 2. init_timer, Setting the Timer Interrupt
/* fd_set variable required to use select() */
static fd_set set_of_pipes;
void init_timer (void (*vec)(), long interval)
{
static int called = 0;
struct sigaction sigspec;
void sigusr1_handler(int);
int p_timer[2];
pid_t ppid, parent_id;
parent_id = getpid();
if (pipe(p_timer))
perror("pipe");
reg_process(p_timer[0], vec);
ppid = fork();
switch (ppid) {
case -1:
perror("fork");
break;
case 0: /* child */
while (1){
usleep(interval);
/* something is written through the pipe to
identify who's requesting the int. */
write(p_timer[1], ".", 1);
kill(parent_id, SIGUSR1);
}
default: /* parent */
if (! called) {
sigspec.sa_handler=sigusr1_handler;
sigemptyset(&sigspec.sa_mask);
/* Don't reset vector when signal is
received (might not be portable) */
sigspec.sa_flags=SA_RESTART;
sigaction(SIGUSR1, &sigspec, NULL);
called = 1;
}
}
}
Copyright © 1994 - 2019 Linux Journal. All rights reserved.