setjmp/longjmp with threads

S

sturlamolden

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
 
T

Tom St Denis

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
 
A

Alexander Bartolich

sturlamolden said:
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.
 
K

Keith Thompson

sturlamolden said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top