context switching with setjmp

A

aditya

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);
}
 
J

Jack Klein

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.

Not anybody here, no.
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()

Implicit int has been outlawed in C for five years now. All
declarators must specify a type. Wake up and smell the coffee, or
other beverage of your choice, you need to write this:

int f()

....or even better:

int f(void)
{
int i=0;
while(1){
printf("in f (%d)\n",i++);
usleep(SECOND);

Not a standard function, and there doesn't seem to be a prototype in
scope, either.
}
}

g()
{
int i=0;
while(1){
printf("in g (%d)\n",i++);
usleep(SECOND);
}
}


setup()
{
sigsetjmp(jbuf[0],1);

There is no standard C library function 'sigsetjmp', you will need to
ask in a group for the compiler/operating system combination that
provides this non-standard extension.
jbuf[0][0].__jmpbuf[JB_SP] = (unsigned) stack1 + 4096;
jbuf[0][0].__jmpbuf[JB_PC] = (unsigned) f;

The C standard does not specify the contents of the 'jmp_buf' type,
nor does it define the results of a user program trying to modify
them.
sigsetjmp(jbuf[1],1);
jbuf[1][0].__jmpbuf[JB_SP] = (unsigned) stack2 + 4096;
jbuf[1][0].__jmpbuf[JB_PC] = (unsigned) g;

[snip]

Even for the platform you are working on, it is most likely that the
modifications you are doing to those buffers is invalid. But you will
need to ask in a platform-specific group, because the C standard
doesn't define any of this.
 
S

SM Ryan

(e-mail address removed) (aditya) wrote:
# 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.

# 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;
#
# }

# main()
# {
# signal(SIGALRM, dispatch);
# setup();
# ualarm(5*SECOND, 5*SECOND);
# longjmp(jbuf[0],1);
# }

The stack frame saved in setjmp has to be still on the stack when you do the
longjmp. When you do a setjmp in a function and then return from that function,
the jmpbuf becomes undefined and the results of longjmp to it are undefined.

Mucking around with the internals of a jmpbuf value is even more undefined.

Perhaps what you really want to do is use function pointers.

f() {...}
g() {...}
typedef int (*function)();
function switcheroo[2] = {f,g};
static toggle = 0;
void dispatch(int sig)
{
puts("in dispatch");
puts("DISPATCH");
toggle = 1 - toggle;
switcheroo[toggle]();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top