setjmp/longjmp with threads

Discussion in 'C Programming' started by sturlamolden, Aug 5, 2009.

  1. sturlamolden

    sturlamolden Guest

    Here is a short question, forgive me if it shows plain ignorance of C:

    - Can I setjmp in one thread and longjmp to the saved jmp_buf in
    another?

    If you wonder why I would like to do this evilness, the reason is that
    Windows has no fork system call. So I wondered if fork could be faked
    with non-local goto and threads, obviously with the limitation that no
    new address space is created. To my knowledge, setjmp is the only
    function apart from fork that returns twice.


    Regards,
    Sturla
     
    sturlamolden, Aug 5, 2009
    #1
    1. Advertising

  2. sturlamolden

    Tom St Denis Guest

    On Aug 5, 4:15 am, sturlamolden <> wrote:
    > Here is a short question, forgive me if it shows plain ignorance of C:
    >
    > - Can I setjmp in one thread and longjmp to the saved jmp_buf in
    > another?
    >
    > If you wonder why I would like to do this evilness, the reason is that
    > Windows has no fork system call. So I wondered if fork could be faked
    > with non-local goto and threads, obviously with the limitation that no
    > new address space is created. To my knowledge, setjmp is the only
    > function apart from fork that returns twice.


    Typically fork() is used as part of executing a new process. In a
    Linux environment if you wanted threads in the same process you'd use
    threads (which are a new type of process but that's not the point).

    But why not just re-write your program so it does this

    if (fork()) {
    patha();
    } else {
    pathb();
    }
    return 0; /* end of function */
    }

    That way you can port it to threads in Windows by just calling pathb()
    in a thread.

    Tom
     
    Tom St Denis, Aug 5, 2009
    #2
    1. Advertising

  3. sturlamolden wrote:
    > Here is a short question, forgive me if it shows plain ignorance of C:
    >
    > - Can I setjmp in one thread and longjmp to the saved jmp_buf in
    > another?


    This question is platform dependent and cannot be answered within the
    context of the C standard.

    Anyway, threads usually comprise three things:
    - a set of register values
    - a stack
    - some kind of thread identity within the operating system

    The context saved by setjmp typically includes only a set of register
    values. Thus calling jmp_buf will use
    - the stack of the thread that called setjmp
    - the identity of the thread that called setjmp

    > If you wonder why I would like to do this evilness, the reason is that
    > Windows has no fork system call. So I wondered if fork could be faked
    > with non-local goto and threads, obviously with the limitation that no
    > new address space is created. To my knowledge, setjmp is the only
    > function apart from fork that returns twice.


    After fork() all address values are still the same, including the
    address of objects on the stack. A new thread on the other hand will
    have a new, empty stack at a different location.

    After fork() both the parent process and the child process can return
    from the function that called fork(). A new thread on the other hand
    cannot return beyond CreateThread or pthread_create.

    --
    Brüder, in die Tonne die Freiheit,
    Brüder, ein Stoppschild davor.
    Egal was die Schwarzen Verlangen
    Rufen wir: Ja! Brav im Chor.
     
    Alexander Bartolich, Aug 5, 2009
    #3
  4. sturlamolden <> writes:
    > Here is a short question, forgive me if it shows plain ignorance of C:
    >
    > - Can I setjmp in one thread and longjmp to the saved jmp_buf in
    > another?
    >
    > If you wonder why I would like to do this evilness, the reason is that
    > Windows has no fork system call. So I wondered if fork could be faked
    > with non-local goto and threads, obviously with the limitation that no
    > new address space is created. To my knowledge, setjmp is the only
    > function apart from fork that returns twice.


    comp.programming.threads might be a good place for this question.

    setjmp and longjmp are standard C, but fork and threads are not.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 5, 2009
    #4
    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:
    458
    Eric Sosman
    Oct 2, 2003
  2. Mantorok Redgormor

    setjmp, longjmp

    Mantorok Redgormor, Nov 12, 2003, in forum: C Programming
    Replies:
    2
    Views:
    432
    Ian Woods
    Nov 12, 2003
  3. someone
    Replies:
    5
    Views:
    3,469
    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,259
    -wombat-
    May 4, 2004
  5. Ravi Uday

    setjmp/longjmp

    Ravi Uday, Aug 3, 2004, in forum: C Programming
    Replies:
    2
    Views:
    549
    Dave Vandervies
    Aug 6, 2004
Loading...

Share This Page