Problem with setjmp/longjmp

Discussion in 'C Programming' started by harman, Mar 16, 2010.

  1. harman

    harman

    Joined:
    Mar 16, 2010
    Messages:
    1
    Hi,

    The following code is just not working

    #define STACK_SIZE 1524

    static void mt_allocate_stack(struct thread_struct *mythrd)
    {
    unsigned int sp = 0;
    void *stck;

    stck = (void *)malloc(STACK_SIZE);

    /*
    * get double word aligned SP -- stacks grow from high
    * to low
    */
    sp = (unsigned int)&((stck));
    sp = sp + STACK_SIZE;
    while((sp % 8) != 0)
    sp--;

    #ifdef linux
    /*
    * keep double word aligned but put in enough
    * space to handle local variables for MtSched
    */
    (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 ( 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
    {
    fprintf(stdout,"\n\n Calling the function = ");
    fflush(stdout);
    }
    }
    }

    All I am doing here is saving a state and then allocating a new stack and storing the pointer in the saved state.

    But the code seg faults at this is line
    else
    {
    fprintf(stdout,"\n\n Calling the function = ");
    fflush(stdout);
    }

    It does not matter what I put in this else statement it always segfaults.

    Can somebody tell me why is that happening.
    harman, Mar 16, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Baruchel

    How dirty is setjmp+fopen+longjmp ?

    Thomas Baruchel, Oct 2, 2003, in forum: C Programming
    Replies:
    2
    Views:
    444
    Eric Sosman
    Oct 2, 2003
  2. Mantorok Redgormor

    setjmp, longjmp

    Mantorok Redgormor, Nov 12, 2003, in forum: C Programming
    Replies:
    2
    Views:
    415
    Ian Woods
    Nov 12, 2003
  3. someone
    Replies:
    5
    Views:
    3,432
    SM Ryan
    May 1, 2004
  4. Michael B Allen

    Is setjmp/longjmp ok?

    Michael B Allen, May 1, 2004, in forum: C Programming
    Replies:
    11
    Views:
    2,235
    -wombat-
    May 4, 2004
  5. aleksa

    setjmp and longjmp problem

    aleksa, Sep 5, 2010, in forum: C Programming
    Replies:
    5
    Views:
    695
    Francois Grieu
    Sep 7, 2010
Loading...

Share This Page