setjmp/longjmp

Discussion in 'C Programming' started by Ravi Uday, Aug 3, 2004.

  1. Ravi Uday

    Ravi Uday Guest

    Hi,

    Can anyone explain me why do we use setjmp and longjump functions.
    I read through the manual pages/doc but wasnt able to get a clear
    picture of the same.
    Any small example illustrating these would be real helping.
    Also, what happens when one jumps from one process/task to other, even though
    it is OT can anybody clear this concept.


    Thanks,
    - Ravi
    Ravi Uday, Aug 3, 2004
    #1
    1. Advertising

  2. >Can anyone explain me why do we use setjmp and longjump functions.
    >I read through the manual pages/doc but wasnt able to get a clear
    >picture of the same.


    setjmp and longjmp are a long-distance version of goto, and
    therefore they are greatly frowned upon by the Structured Programming Police.
    jmp_buf buf;
    some_type_t *p;

    if (setjmp(buf) == 0) {
    /* try something that might get an error */
    ...
    p = malloc(sizeof(*p));
    if (p == NULL) {
    longjmp(buf, 1); /* we got an error */
    }
    ...
    } else {
    /* we got an error, (someone called longjmp) handle it */
    /* e.g. use the slow, file-based method instead of */
    /* the in-memory method */
    }

    The tricky thing you can do with longjmp, though, is that it
    can be called from functions called from the /* try something that
    might get an error */ code. The functions signalling the error
    need not have any idea where the error handler is (although they
    do need to know where buf is, either passed as a parameter or a
    static or global.)

    Another tricky thing that can be done, and isn't portable, is to
    catch signals and have the signal handler call longjmp(). This
    way, the /* we got an error, handle it */ part might correct for
    division by zero, overflow, or dereferencing NULL. Or it might
    just note that it crashed and log the error.

    void sigsegv_handler(int sig) {
    /* you're pretty much stuck with buf being a global or */
    /* static variable here, as you can't change the parameters */

    longjmp(buf, 1);
    }

    Yet another tricky thing involves a stack of jmp_bufs and nested
    error handlers.

    >Any small example illustrating these would be real helping.


    >Also, what happens when one jumps from one process/task to other, even though
    >it is OT can anybody clear this concept.


    In general, you DON'T jump from one process/task to another. Other
    programs aren't in your address space. And if you try, expect a
    real mess.

    Gordon L. Burditt
    Gordon Burditt, Aug 3, 2004
    #2
    1. Advertising

  3. In article <cen7m1$>,
    Gordon Burditt <> wrote:
    >>Can anyone explain me why do we use setjmp and longjump functions.
    >>I read through the manual pages/doc but wasnt able to get a clear
    >>picture of the same.

    >
    >setjmp and longjmp are a long-distance version of goto, and
    >therefore they are greatly frowned upon by the Structured Programming Police.


    setjmp can also behave as a COME FROM, which is occasionally useful,
    especially if something like this needs to be added to an existing
    program:
    --------
    static jmp_buf env;

    void go_back_to_sleep(void)
    {
    longjmp(env,1);
    }

    int main(void)
    {
    /*Do one-time initialization*/

    /*This means "COME FROM longjmp in go_back_to_sleep()"*/
    setjmp(env);

    /*Wait for wake-up call*/

    /*Do per-run initialization*/

    while(1)
    {
    /*Do stuff, including calling functions that may call
    go_back_to_sleep() under appropriate conditions
    */
    }

    /*never reached*/
    }
    --------
    This is probably not the best way to do this in new code, but it's
    often the easiest way to change an existing program's handling of "We're
    done doing stuff" from "exit" to "go back to sleep and wait for another
    wake-up call".


    dave

    --
    Dave Vandervies

    It's like trying to give yourself a voluntary lobotomy.
    --Andy Glew in comp.arch
    Dave Vandervies, Aug 6, 2004
    #3
    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:
    446
    Eric Sosman
    Oct 2, 2003
  2. Mantorok Redgormor

    setjmp, longjmp

    Mantorok Redgormor, Nov 12, 2003, in forum: C Programming
    Replies:
    2
    Views:
    418
    Ian Woods
    Nov 12, 2003
  3. someone
    Replies:
    5
    Views:
    3,435
    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,238
    -wombat-
    May 4, 2004
  5. Jrferguson

    A bit off topic; setjmp/longjmp

    Jrferguson, Oct 29, 2004, in forum: C Programming
    Replies:
    4
    Views:
    480
    Dan Pop
    Nov 2, 2004
Loading...

Share This Page