I write a timer which periodically send signal interrupt to implement function "contextswitch"
but something wrong with my code
it can print "inside thr_quantum", "inside timer", "inside interrupt"
but never print "inside contextswitch"
I cannot find out what's wrong with it
please help
Thank you very much!
The following is my code.
Code:
/*myhandler: contextswitch*/
void contextswitch(void)
{
fprintf(stderr, "inside contextswitch\n");
.............
.............
}
/* set up myhandler for SIGALRM */
static int th_interrupt(void)
{
fprintf(stderr, "inside interrupt\n");
struct sigaction act;
act.sa_handler = contextswitch;
act.sa_flags = 0;
return (sigemptyset(&act.sa_mask) || sigaction(SIGALRM , &act, NULL));
}
/* set ITIMER_REAL for 5-ms intervals */
static int th_timer(time_t n)
{
fprintf(stderr, "inside timer\n");
struct itimerval value;
value.it_interval.tv_sec = 0;
value.it_interval.tv_usec = n;
value.it_value = value.it_interval;
return (setitimer(ITIMER_REAL, &value, NULL));
}
void thr_quantum(int n)
{
fprintf(stderr, "inside quantum\n");
if (th_interrupt() == -1) {
perror("Cannot set up interrupt");
exit( 1 );
}
if (th_timer(n) == -1) {
perror("Cannot set up timer");
exit( 1 );
}
}