473,396 Members | 1,809 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,396 software developers and data experts.

setjmp longjpm question

Could some knowledgable soul tell me if the following setjmp/longjmp
structure will work from a logical standpoint ? The problem is that
bar() needs to suspend processing and return control to foo() but save
it's current spot so that foo() can call bar() and have it pick up
where it left off. I'm fairly certain that bar() can longjmp to foo(),
but I'm uncertain as to whether foo() can then longjmp back to bar().
Thanks in advance for any insight.
Greg.
static jmp_buf l1;
static jmp_buf l2;

void foo(void)
{
if(setjmp(l1) == 0) bar();
-- do stuff --
longjmp(l2);

}

void bar(void)
{
-- do stuff ---
if(setjmp(l2) == 0) longjmp(l1);
-- do more stuff --
}

Feb 8 '07 #1
4 1345
"nicho" <gn*******@bellsouth.netwrites:
Could some knowledgable soul tell me if the following setjmp/longjmp
structure will work from a logical standpoint ? The problem is that
bar() needs to suspend processing and return control to foo() but save
it's current spot so that foo() can call bar() and have it pick up
where it left off. I'm fairly certain that bar() can longjmp to foo(),
but I'm uncertain as to whether foo() can then longjmp back to bar().
You can't portably use longjmp that way. It's likely to not work
at all on many implementations.
--
Ben Pfaff
bl*@cs.stanford.edu
http://benpfaff.org
Feb 8 '07 #2
nicho wrote On 02/08/07 17:10,:
Could some knowledgable soul tell me if the following setjmp/longjmp
structure will work from a logical standpoint ? The problem is that
bar() needs to suspend processing and return control to foo() but save
it's current spot so that foo() can call bar() and have it pick up
where it left off. I'm fairly certain that bar() can longjmp to foo(),
but I'm uncertain as to whether foo() can then longjmp back to bar().
Thanks in advance for any insight.
Greg.
static jmp_buf l1;
static jmp_buf l2;

void foo(void)
{
if(setjmp(l1) == 0) bar();
-- do stuff --
longjmp(l2);

}

void bar(void)
{
-- do stuff ---
if(setjmp(l2) == 0) longjmp(l1);
-- do more stuff --
}
You can only longjmp() to an "ancestor" in the call
tree, that is, to a function that is still active and has
not returned. Once a function returns, any jmp_buf it
initialized with setjmp() is no longer meaningful.

So, no: This won't work, except perhaps by pure luck.
(Don't be tempted: If you try it once and it happens to
work, there's no reason to believe it will work again.
If you survive the first round in a game of Russian
Roulette, your odds of surviving a second round do not
improve ...)

--
Er*********@sun.com
Feb 8 '07 #3
In article <11*********************@v45g2000cwv.googlegroups. com>,
nicho <gn*******@bellsouth.netwrote:
>Could some knowledgable soul tell me if the following setjmp/longjmp
structure will work from a logical standpoint ? The problem is that
bar() needs to suspend processing and return control to foo() but save
it's current spot so that foo() can call bar() and have it pick up
where it left off. I'm fairly certain that bar() can longjmp to foo(),
but I'm uncertain as to whether foo() can then longjmp back to bar().
As other have said, you can't portably do this, though it has worked
on some systems in the past. You'd be better off using a threads
library - most systems provide one nowadays.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 8 '07 #4
On Feb 8, 6:48 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
As other have said, you can't portably do this, though it has worked
on some systems in the past. You'd be better off using a threads
library - most systems provide one nowadays.
Thanks for all the replies. On reflection, I think I'll crib some
code from the state threads guys and see if I can make that work.
Again, thank you.
G.

Feb 9 '07 #5

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.
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...
18
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
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) {
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....
4
by: Richard Jones | last post by:
Currently ctypes can't play well with any C code that requires use of setjmp as part of its API. libpng is one of those libraries. Can anyone think of a reasonable solution to this? Perhaps...
5
by: Spiros Bousbouras | last post by:
In the following assume that a is int and env is of type jmp_buf a = setjmp(env) ; Is it legal ? It looks reasonable but it seems to violate what is mentioned under "Environmental limits" of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
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,...
0
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...
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
jinu1996
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...

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.