- context switching with setjmp
- Posted by aditya on September 22nd, 2004
hi all,
can anbody please tell me that why the control transfers to the
function f() and then g() and then again f() and so on, when
longjmp(jbuf[0],1) is called in main.
Here is the complete program.
#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);
}
- Posted by CBFalconer on September 22nd, 2004
aditya wrote:
Don't multi-post. Cross post if you really must, but then set
followups to your primary newsgroup. I have already seen this in
c.l.c and this is an annoyance.
You have already been told that your code is non-standard, and
system specific. Therefore you need to find a group that deals
with your particular system. This is not it.
--
"It is not a question of staying the course, but of changing
the course" - John Kerry, 2004-09-20
"Ask any boat owner the eventual result of continuing the
present course indefinitely" - C.B. Falconer, 2004-09-20
- Posted by Programmer Dude on September 22nd, 2004
CBFalconer writes:
==
"It is not a question of staying the course, but of changing
the course" - John Kerry, 2004-09-20
"Ask any boat owner the eventual result of continuing the
present course indefinitely" - C.B. Falconer, 2004-09-20
How about asking the Space Shuttle pilot?