Problem with setjmp/longjmp

Posted by user294732 on Stack Overflow See other posts from Stack Overflow or by user294732
Published on 2010-03-16T12:34:49Z Indexed on 2010/03/16 12:56 UTC
Read the original article Hit count: 258

Filed under:
|

The code below is just not working. Can anybody point out why

#define STACK_SIZE 1524

static void mt_allocate_stack(struct thread_struct *mythrd)

{

    unsigned int sp = 0;
    void *stck;

    stck = (void *)malloc(STACK_SIZE);

    sp = (unsigned int)&((stck));
    sp = sp + STACK_SIZE;
    while((sp % 8) != 0)
    sp--;

#ifdef linux

    (mythrd->saved_state[0]).__jmpbuf[JB_BP] = (int)sp;
    (mythrd->saved_state[0]).__jmpbuf[JB_SP] = (int)sp-500;
#endif

}

void mt_sched()

{

    fprintf(stdout,"\n Inside the mt_sched");
    fflush(stdout);

    if ( current_thread->state == NEW )
     {
         if ( setjmp(current_thread->saved_state) == 0 )
         {
            mt_allocate_stack(current_thread);

            fprintf(stdout,"\n Jumping to thread = %u",current_thread->thread_id);
            fflush(stdout);
            longjmp(current_thread->saved_state, 2);
         }
         else
         {
            new_fns();
         }
      }
}

All I am trying to do is to run the new_fns() on a new stack. But is is showing segmentation fault at new_fns().

Can anybody point me out what's wrong.

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux