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 2 2124
>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
In article <ce********@lib rary1.airnews.n et>,
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_slee p(void)
{
longjmp(env,1);
}
int main(void)
{
/*Do one-time initialization*/
/*This means "COME FROM longjmp in go_back_to_slee p()"*/
setjmp(env);
/*Wait for wake-up call*/
/*Do per-run initialization*/
while(1)
{
/*Do stuff, including calling functions that may call
go_back_to_slee p() 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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
setjmp/longjmp() are taking stuct:
c:\> dmc sj.c (Digital Mars Compiler Version 8.38n)
link sj,,,user32+kernel32/noi;
c:\> sj.exe
sizeof(jmp_buf) = 64
----------------------------------------------------------------- sj.c
|
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 consistenly, or something
that might pull in exotic text segments, etc.
Specifically I have a hairly algorithm loop that uses what is currently
a macro V. Here's a snipplet:
for (k = d; k >= -d; k -= 2) {
if (k == -d || (k != d && V(fwd, m, k - 1)...
|
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 setjmp/longjmp, but I am somewhat lost
in the detail of how to actually implement this in assembler. (My compiler
supports in-line assembler which I hope will prove usefull). I will be very
gratefull for any help or pointers with this problem. I am...
|
by: candy |
last post by:
hi all,
Consider the following C code:
void funct(){
}
| |
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 stack;
};
|
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)
{
|
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
|
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. They (my co-workers) say (with
vehement ardor ;) ) that the usage of setjmp() emplyoyed in function"C"
that was called from function "B" that was called from function "A"
that was called form the main(), will cause havoc in the stack. And it
makes...
|
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.
Let's consider the following toy-example (yes, the example is stupid
but never mind...)
BEGIN CODE =====================
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |