0

I am facing some problems with the usage of setitimer functionality.

When I execute the code I couldn't see expected result. The signal handles printf( ) works when I comment the while(1) part in main function (which is my busy executing code). In my understanding both (main( ) and signal_handler()) should run in time sliced fashion or in parallel depending on Linux scheduler.

Can anyone help me out in debugging or correcting my understanding?

Here is the code:

void timer_handler (int signum) {

    printf("...\n"); 
    printf ("Timer expired %d times\n", ++count);
} 

void Tmr_Init(void) {

    struct sigaction sa;
    struct itimerval timer; 

    memset (&sa, 0, sizeof (sa));
    sa.sa_handler = &timer_handler; 
    sigaction (SIGPROF, &sa, NULL);timer.it_value.tv_sec = 2;

    timer.it_value.tv_usec = 0;
    timer.it_interval.tv_sec = 1;
    timer.it_interval.tv_usec = 0;

    setitimer (ITIMER_PROF, &timer, NULL);
} 

int main () {

    Tmr_Init(); 

    /* Do busy work. */
    while (1) {
        printf("In main!!\n"); 
        sleep(2);
    }

    return 0;
} 
Bowdzone
  • 3,827
  • 11
  • 39
  • 52
  • 2
    You need to provide the code. Also, [avoid using `printf` in the signal handler](http://stackoverflow.com/q/16891019/1009479) – Yu Hao Oct 21 '13 at 05:17
  • void timer_handler (int signum) { printf ("Timer expired %d times\n", ++count); } int main () { Tmr_Init(); /* Do busy work. */ while (1) { printf("In main!!\n"); sleep(2); } return 0; } – Vinay Kumar Kotegowder Oct 21 '13 at 05:35
  • If you see the code above, if I comment the while(1) code part in main( ) function signal handler functionality works perfect. – Vinay Kumar Kotegowder Oct 21 '13 at 05:37
  • [Edit](http://stackoverflow.com/posts/19486688/edit) your question to add more code. – Yu Hao Oct 21 '13 at 05:40
  • void timer_handler (int signum){printf("...\n"); printf ("Timer expired %d times\n", ++count);} void Tmr_Init(void) {struct sigaction sa;struct itimerval timer; memset (&sa, 0, sizeof (sa));sa.sa_handler = &timer_handler; sigaction (SIGPROF, &sa, NULL);timer.it_value.tv_sec = 2; timer.it_value.tv_usec = 0;timer.it_interval.tv_sec = 1; timer.it_interval.tv_usec = 0;setitimer (ITIMER_PROF, &timer, NULL);} int main () {Tmr_Init(); /* Do busy work. */while (1){printf("In main!!\n"); sleep(2);}return 0;} – Vinay Kumar Kotegowder Oct 21 '13 at 05:48
  • Hi Ya Hao: Sorry you have to format locally the code what I posted above. I am facing problem in formatting while adding the code. – Vinay Kumar Kotegowder Oct 21 '13 at 05:49
  • 1
    No, I mean you need to put the code into the question itself by editing it. Don't post them in the comment. – Yu Hao Oct 21 '13 at 05:49

0 Answers0