getitimer

インターバルタイマーを使って一定時間間隔でシグナルを発生。

#include < sys/time.h >

#include < signal.h >
#include < unistd.h >
#include < stdio.h >
#include < string.h >

static void
interval()
{
    write(2, ".", 1);
}

int main()
{
    struct itimerval val;
    struct sigaction act;
    sigset_t set;
    
    memset(&act, 0, sizeof act);
    act.sa_handler = interval;
    if (sigaction(SIGALRM, &act, NULL)< 0){
        perror("sigaction");
        return 1;
    }
    
    val.it_interval.tv_sec = 1;
    val.it_interval.tv_usec = 0;
    val.it_value = val.it_interval;
    if (setitimer(ITIMER_REAL, & val, NULL) < 0){
        perror("setitimer");
        return 1;
    }
    
    if (getitimer(ITIMER_REAL, &val) < 0){
        perror("getitimer");
        return 1;
    }
    printf(
    "it_interval = %ld.%ld\n"
           "it_value = %ld.%ld\n",
           val.it_interval.tv_sec, val.it_interval.tv_usec,
           val.it_value.tv_sec, val.it_value.tv_usec);
    
    sigemptyset(&set);
    for(;;){
        sigsuspend(&set);
    }
    return 0;
}