/*
 * Timer test.
 * Test how many itterations of free running loop will run in one second
 * This example demonstrates the use of signals and interval timer
 * Written by: Ori Idan Helicon technologies Ltd.
 * This demo is distributed under Gnu Public License (GPL)
 */

#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <sched.h>
#include <unistd.h>

unsigned int counter;

void TimerHandler(int signum)
{
        printf("Counter value: %u\n"counter);
        counter = 0;
}

int main(int argcchar *argv[])
{
        struct itimerval timer;
        struct sched_param sched;
        
        /* set priority if argv[1] */
        if(argv[1] != NULL) {
                sched.sched_priority = atoi(argv[1]);
                if(sched.sched_priority > 99) {
                        printf("priority should be: 0 - 99\n");
                        exit(1);
                }
                if(sched_setscheduler(0SCHED_FIFO, &sched)) {
                        perror(argv[0]);
                        exit(1);
                }
        }
        counter = 0;

        signal(SIGALRMTimerHandler);
        timer.it_value.tv_sec = 1;
        timer.it_value.tv_usec = 0;
        timer.it_interval.tv_sec = 1;
        timer.it_interval.tv_usec = 0;
        setitimer(ITIMER_REAL, &timerNULL);

        while(1) {
                counter++;
        }
}