#include<stdio.h>
#include<setjmp.h>
#include<signal.h>
jmp_buf env;
void alarmHandler()
{
printf("\n in alarm Handle");
longjmp(env,1);
}
int main()
{
signal(SIGALRM,alarmHandler);
alarm(2);
for(;;)
{
printf("\nhello");
sleep(1);
if(setjmp(env))
{
printf("\n inside if");
signal(SIGALRM,alarmHandler);
alarm(2);
}
}
return 0;
}
At start, actives monitor for signal then active alarm for 2 sec.(countdown).
Inside the for
loop, every time, it keeps saving setjmp(env)
as time ends last setjmp(env)
will be called using longjmp()
.
As setjmp()
by default return 0 if called from longjmp()
, whatever is second arg value is that value return.