473,789 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setjmp/longjmp and some table tennis

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 =============== ======
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>

void wrapper();
void ping(jmp_buf, jmp_buf);
void pong(jmp_buf, jmp_buf);

int main() {
wrapper();
return 0;
}
void ping(jmp_buf ping_env, jmp_buf pong_env) {
printf("calling ping\n");
switch (setjmp(ping_en v)) {
case 0:
printf("ping: case 0\n");
pong(ping_env, pong_env);
break;
case 1:
printf("ping: case 1\n");
longjmp(pong_en v, 1);
break;
case 2:
printf("ping: case 2\n");
longjmp(pong_en v, 2);
break;
default:
printf("ping: case default\n");
break;
}
printf("ping is done\n");
}
void pong(jmp_buf ping_env, jmp_buf pong_env) {
printf("\tcalli ng pong\n");
switch (setjmp(pong_en v)) {
case 0:
printf("\tpong: case 0\n");
longjmp(ping_en v, 1);
break;
case 1:
printf("\tpong: case 1\n");
longjmp(ping_en v, 2);
break;
case 2:
printf("\tpong: case 2\n");
longjmp(ping_en v, 3);
break;
default:
printf("\tpong: case default\n");
break;
}
printf("pong is done\n");
}
void wrapper() {
jmp_buf pong_env;
jmp_buf ping_env;
ping(ping_env, pong_env);
}
END CODE =============== =========

This code works OK if I make ping_env and pong_env static global
variables. However, when I try to pass them as parameters the code
crash with a "Bus error".

Any explanation as to why it crashes? (Well, I guess that's because
the jmp_buf variables passed as parameters are somehow not properly
restored, but I'm new to this so...)

And, even better, any advise on how to correct this while keeping the
intent of the program? (Yeah, I know any real-life application of this
pattern would yield pretty awful code but that's not the matter
here...)

Thanks,

nch

Feb 23 '07 #1
3 1898
In article <11************ **********@q2g2 000cwa.googlegr oups.com>,
<no************ @yahoo.comwrote :
>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.
Then setjmp/longjmp is the wrong tool for the job. You're only allowed
to longjmp back to a setjmp in a stack frame that's still live.

>And, even better, any advise on how to correct this while keeping the
intent of the program? (Yeah, I know any real-life application of this
pattern would yield pretty awful code but that's not the matter
here...)
You want coroutines. They're not directly supported by C (or, for
that matter, by a lot of other languages); there are ways to do it
using correct and portable C code, but they're all Rather Ugly and/or
quite limited.

If you don't mind giving up portability, using threads to fake it might
be the cleanest solution, even though they're kind of heavyweight for
something like this (and they'll take a bit of care to get right).
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Always assume that your code will need to be maintained -- and that the
next person to work on it is a homocidal maniac who knows where you live.
--Keith Thompson in comp.lang.c
Feb 23 '07 #2
Dave Vandervies writes:
You want coroutines. They're not directly supported by C (or, for
that matter, by a lot of other languages); there are ways to do it
using correct and portable C code, but they're all Rather Ugly and/or
quite limited.
In this case it's simple enough. Let ping() and pong return the value
to be passed to each other, and write a controlling function which calls
them.

For more complicated examples it gets uglier, yes. E.g. let ping() and
pong() fill a struct with a pointer to the next function to be called
and its arguments, and the controlling function would call via that
struct. Or set an integer representing what to do next, and write
the controlling function as a state machine.

--
Regards,
Hallvard
Feb 23 '07 #3

<no************ @yahoo.comwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.com...
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.
<snip>
And, even better, any advise on how to correct this while keeping the
intent of the program? (Yeah, I know any real-life application of this
pattern would yield pretty awful code but that's not the matter
here...)
Don't use a screwdriver when a pair of pliers is called for.
Use something patterned like this:

typedef void (*Bouncer)( /*whatever needs passing*/ );

void ping( /*whatever needs passing*/ );
void pong( /*whatever needs passing*/ );
....
Bouncer next = NULL;
while (1) {
next = (next==ping) ? pong : ping;
(*next) (/*whatever needs passing*/ );
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Feb 23 '07 #4

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

Similar topics

2
2296
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
3330
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)...
4
2127
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...
5
1875
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
2326
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; };
8
2424
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
2432
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...
4
1359
by: nicho | last post by:
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...
5
1616
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 7.13.1.1 of n869 so my guess is that it's not legal. If it's not legal could someone give me some insight
0
10199
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 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...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
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 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...
0
9020
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, 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...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
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...

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.