hi all,
cananybody please tell me that why the control transfers to f() and
then g() and then f() and so on,when longjmp(jbuf[0],1) is called in
main.
Here is the complete code.
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#define SECOND 1000000
char stack1[4096];
char stack2[4096];
jmp_buf jbuf[2];
f()
{
int i=0;
while(1){
printf("in f (%d)\n",i++);
usleep(SECOND);
}
}
g()
{
int i=0;
while(1){
printf("in g (%d)\n",i++);
usleep(SECOND);
}
}
setup()
{
sigsetjmp(jbuf[0],1);
jbuf[0][0].__jmpbuf[JB_SP] = (unsigned) stack1 + 4096;
jbuf[0][0].__jmpbuf[JB_PC] = (unsigned) f;
sigsetjmp(jbuf[1],1);
jbuf[1][0].__jmpbuf[JB_SP] = (unsigned) stack2 + 4096;
jbuf[1][0].__jmpbuf[JB_PC] = (unsigned) g;
}
static toggle = 0;
void dispatch(int sig)
{
puts("in dispatch");
if (sigsetjmp(jbuf[toggle],1) == 1)
return;
puts("DISPATCH");
toggle = 1 - toggle;
siglongjmp(jbuf[toggle],1);
}
main()
{
signal(SIGALRM, dispatch);
setup();
ualarm(5*SECOND, 5*SECOND);
longjmp(jbuf[0],1);
}