context switching with setjmp

From: aditya (adityavasishth_at_gmail.com)
Date: 09/22/04


Date: 21 Sep 2004 20:50:39 -0700

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



Relevant Pages

  • context switching with setjmp
    ... can anbody please tell me that why the control transfers to the ... char stack1; ... signal(SIGALRM, dispatch); ...
    (comp.programming)
  • Re: acting on items passed to a method via a dictiomary
    ... > into methods, but other then that, if you need the full flexibility of a ... one would not have to override the 'setup' method in derived ... dispatch map, which in the examples below is named 'setupMap'. ... each of the appropropiate member functions ...
    (comp.lang.python)
  • Re: returning a char array
    ... Bill wrote: ... I'm trying to return a char[] from a function. ... setup and what would the declaration look like? ...
    (comp.lang.c)
  • [PATCH 12/29] zorro: use non-racy method for proc entries creation
    ... ->data be setup before gluing PDE to main tree. ... char name; ...
    (Linux-Kernel)
  • Re: Options listing
    ... an official birth option. ... Instead have the char ... setup ask you which method you prefer. ... "Our enemies are innovative and resourceful, ...
    (rec.games.roguelike.angband)