Operating System - Linux
1828371 Members
2898 Online
109976 Solutions
New Discussion

What does the setjmp function do?

 
SOLVED
Go to solution
David Yandry
Frequent Advisor

What does the setjmp function do?

Hi C Experts:

I am trying to maintain some very old C code and I am very confused. The source looks something like this:

if (setjmp(jump)) != 0) {
recover();
warning("Task not completed!");
}
process_command();

I have read the man page and it just doesn't make sense! I have found that when Control-C is pressed that I do see the "Task not completed!" message but nothing about this makes any sense to me.

Any ideas?

TIA,
David
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: What does the setjmp function do?

It's really not possible to talk about setjmp without talking about its companion function, longjmp. Most of the time setjmp() does nothing and returns 0. In that case, all it does is record the process's current context in a structure (that is typically an array); that's jump in your example. Now somewhere, possibly far removed from your code snippet is a longjmp function which also has the variable jump as the actual parameter and longjmp cause the program to return to the same context as when setjmp() was called but this time setjmp returns a non-zero value defined in the longjmp function.

You were probably taught that goto's are bad; well setjmp/longjmp allow you to do goto's even outside the current function (or even outside the source file that contains the setjmp). If local goto's are bad then non-local goto's must be superbad -- but that doesn't mean that they aren't sometimes useful.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: What does the setjmp function do?

I should add that setjmp/longjmp is commonly tied to a signal handler so I'll bet that you will find the longjmp() that corresponds to this setjmp() inside some signal handler.

Read the setjmp man page again and it should make a little more sense now.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: What does the setjmp function do?

Okay David, I spent about 5 minutes throwing together a complete working example. The idea is that setjmp() is called to setup the environment. It normally returns 0 and thgen enters Func_A which in turn calls Func_B which in turn calls Func_C. Func_C loops 60 times printing a message and sleeping 1 second between each message. I intentionally deeply nested this loop inside multiple functions so that the effect of the non-local goto caused by longjmp would be obvious. Press or whatever your n'rupt ket is set to and the interupt handler is triggered which makes setjmp return a non-zero value. You actually have to interrupt the process 3 times to terminate the process.

Oh, and to make the ghost of Edsger Dijkstra really mad, I intention put in a local goto as well as the non-local setjmp/longjmp goto.

Compile the attached code like this:
cc setjmp.c -o setjmp and then simply execute "setjmp".
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: What does the setjmp function do?

Okay David, I spent about 5 minutes throwing together a complete working example. The idea is that setjmp() is called to setup the environment. It normally returns 0 and thgen enters Func_A which in turn calls Func_B which in turn calls Func_C. Func_C loops 60 times printing a message and sleeping 1 second between each message. I intentionally deeply nested this loop inside multiple functions so that the effect of the non-local goto caused by longjmp would be obvious. Press or whatever your n'rupt key is set to and the interupt handler is triggered which makes setjmp return a non-zero value. You actually have to interrupt the process 3 times to terminate the process.

Oh, and to make the ghost of Edsger Dijkstra really mad, I intention put in a local goto as well as the non-local setjmp/longjmp goto.

Compile the attached code like this:
cc setjmp.c -o setjmp and then simply execute "setjmp".
If it ain't broke, I can fix that.
David Yandry
Frequent Advisor

Re: What does the setjmp function do?

Hi A. Clay:

Your example really helped! I think I at least understand the concept but I still haven't found the longjmp function in the source. I will continue to look for it.

Thanks,
David
A. Clay Stephenson
Acclaimed Contributor

Re: What does the setjmp function do?

Bear in mind that the corresponding longjmp could (and likely is) in another .c file; it could even be stuck in a library that is linked to your code but that would be truly evil and devious on the part of the original developer.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: What does the setjmp function do?

David: I still haven't found the longjmp function

As Clay says, it could be anywhere.
But why not let the computer find it? Run your application in the debugger and set a breakpoint in longjmp. That will find the first use.

Note you don't need debug info, if you can find it after you know the function or shared lib names.