I am trying to learn setjump and longjump in C. Can anyone help me with the output of the following code along with the code flow and the explanation for the the cases.i have called a function funcall() in the code. The alarm is being called for the first iteration but in the subsequent iterations the alarm is not being called. Why is it so? Does alarm(0) prevents any future alarm requests?
EDIT : Even after deleting the "print_T()" line in the signal handler the code doesnot seem to run.
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <time.h>
#define LEN 50
jmp_buf po;
void print_T() {
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
}
static void ti() {
print_T();
longjmp(po,1);
}
int funcall(int reply) {
static int p;
p = 0;
signal(SIGALRM, ti);
int q = setjmp(po);
if(q == 0)
printf("the value BEFORE setjmp %d for reply %d\n", p, reply);
else
printf("the value AFTER setjmp %d for reply %d\n", p, reply);
alarm(5);
if(p > 0) {
printf("INVOKING THE ALARM");
alarm(0);
return -1;
}
p++;
for(int i = 0; i < 100000; i++)
for(int j = 0; j < 100000; j++);
return 0;
}
int main() {
int a;
int reply;
time_t start, end;
for(reply=0; ; reply++) {
printf("~~~~~~~~~~~~~~~~~~~~~~~~~ITERATION NUMBER %d\n", reply);
time(&start);
a = funcall(reply);
time(&end);
double tin = difftime(end, start);
printf("*********************ITERATION END and a returned is %d after %f\n", a, tin );
double tidiff = difftime(end, start);
if(a < 0)
if(reply == 10) {
break;
}
}
}