473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

setjmp/longjmp

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
Nov 14 '05 #1
2 2101
>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
Nov 14 '05 #2
In article <ce********@library1.airnews.net>,
Gordon Burditt <go***********@burditt.org> 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 dj******@csclub.uwaterloo.ca

It's like trying to give yourself a voluntary lobotomy.
--Andy Glew in comp.arch
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
6
by: someone | last post by:
I have *thought* that setjmp/longjmp() should take a pointer to jmp_buf. And the calling function should hold the actual struct data. But ... I trid on both Win32 and Linux, it seems that...
12
by: Michael B Allen | last post by:
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of code? Or is it frowned apon like goto? I have a need but I don't want to use something that is costly, isn't supported...
4
by: Jrferguson | last post by:
I have a C program that I am trying to port to a Motorola 68k based system. It makes use of setjmp and longjmp which are not supported by my C compiler. I understand the general principle behind...
5
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
by: JS | last post by:
When setjmp is called how can the if statement evaluate to true or false when setjmp only returns 0 or non-zero? struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int...
8
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
4
by: Sreekanth | last post by:
Hi All I am trying to write a code in which I have to access an array of jmp_buf so i have declared it as jmp_buf mybuf Now when i am doing a longjmp like
15
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very profocient in C coding as are some of my co-workers....
3
by: no_click_there | last post by:
Hi, I'm learning to use the setjmp/longjmp functions and still can't really grasp what's going on here. I'd basically like to be able to jump back and forth from (or to) two given functions. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.