Pejman Moghadam / C-programming

C - setitimer

Public Domain


setitimer : set interval timer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>

double count;

void catcher(int signum)
{
    static i = 1;
    if (signum == SIGALRM) {
            printf(" i = %05d , count = %.0f \n", i++, count);
            return;
    } else if (signum == SIGINT) {
            printf("\n SIGINT \n");
            exit(EXIT_SUCCESS);
    }
}

int main()
{
    struct itimerval tval;
    struct sigaction act;

    /* Catching SIGALRM and SIGINT */
    memset(&act, 0, sizeof(act));
    act.sa_handler = &catcher;
    sigaction(SIGALRM, &act, NULL);
    sigaction(SIGINT, &act, NULL);

    /* Timer kicks in after one microsecond */
    tval.it_value.tv_sec = 0;
    tval.it_value.tv_usec = 1;
    /* Timer fires every  three seconds */
    tval.it_interval.tv_sec = 3;
    tval.it_interval.tv_usec = 0;
    /* Starting a timer to send the SIGALRM signal */
    setitimer(ITIMER_REAL, &tval, NULL);

    /* Infinite addition */
    for (count = 0; ;count++);

    return 0;
}

BY: Pejman Moghadam
TAG: c, setitimer, signal
DATE: 2014-01-19 01:39:14


Pejman Moghadam / C-programming [ TXT ]