473,777 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with setjmp and long jump

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
longjmp(mybuf[i],1)
and trying to use a for loop to iterate and check for setjmp condition

for(i=0;i <mybuffsize; i++)
if(setjmp(mybuf[i])
dosomething();
But it is giving me a segementation fault and telling me that longjmp
is not able to access the memory location for corresponding setjmp can
anyone suggest me a better way.
I am trying to implement multiple threads using setjmp/longjmp

Sreekanth Ramakrishnan

Feb 15 '06 #1
4 7564
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
"Sreekanth" <sr************ ********@gmail. com> wrote:
I am trying to implement multiple threads using setjmp/longjmp


You are insane.

Use whatever your operating system provides for creating threads. If you
ask for help on comp.lang.c, you don't have the slightest chance to get
multiple threads running reliably using setjmp and longjmp.
Feb 15 '06 #2
I am trying to implement P producer and C consumer problem using setjmp
and longjmp. It has no commerical value but then I am doing this for
academic purpose.

The reason is i dont wnat to kind of use pthread library but implement
a simple rudimentary thread library. maybe it is not of any use but
just for knowledge sake

Check out my code::::

#include <stdio.h>
#include <time.h>
#include <setjmp.h>

#define MAXSIZE 1
jmp_buf prod[10];
jmp_buf cons[10];
jmp_buf thread;
//The number of producers and consumers cannot be more than 10 each.
int numberofproduce rs;
int numberofconsume rs;

typedef struct _buf
{
int number[MAXSIZE];
int size;
}buffer;

buffer sharedbuffer;

void produce(int number)
{
if (sharedbuffer.s ize == MAXSIZE)
{
int myrandom = rand();
myrandom = myrandom %numberofconsum ers;
if (myrandom == 0)
myrandom = 1;
longjmp(cons[myrandom],1);
}
else
{
sharedbuffer.nu mber[sharedbuffer.si ze ++] = number;
printf("The Producer %d is executing\n",nu mber);
longjmp(thread, 1);
}
}
void consume(int number)
{
if (sharedbuffer.s ize == 0)
{
int myrandom = rand();
myrandom = myrandom % numberofproduce rs;
if (myrandom == 0)
myrandom = 1;
longjmp(prod[myrandom],1);
}
else
{
printf("The Conumser is consuming
%d\n",sharedbuf fer.number[--sharedbuffer.si ze]);
longjmp(thread, 1);
}
}

void switcher()
{
int myrandom = rand();
myrandom = myrandom %(numberofprodu cers + numberofconsume rs);

if (myrandom >= numberofproduce rs)
consume(myrando m);
else
produce(myrando m);
}
int main(void)
{
int i;
int tp=0;
int tc=0;
printf("Enter the number of producers : : ");
scanf("%d",&num berofproducers) ;
printf("Enter the number of consumers : : ");
scanf("%d",&num berofconsumers) ;

i = 0;

sharedbuffer.si ze = 1;
while (i < 50)

{
if(setjmp(threa d))
switcher();
else
switcher();
i++;
}

while(!setjmp(c ons[tp])&& tp < numberofconsume rs)
{
tp = tp+1;
}
if(setjmp(cons[tp]))
produce(tp);

tp = 0;
while(!setjmp(p rod[tp]) && tp < numberofproduce rs)
{
tp = tp+1;
}
if (setjmp(prod[tp]))
consume(tp);
return 0;
}

Feb 15 '06 #3
On 15 Feb 2006 02:10:02 -0800, in comp.lang.c , "Sreekanth"
<sr************ ********@gmail. com> wrote:
The reason is i dont wnat to kind of use pthread library but implement
a simple rudimentary thread library. maybe it is not of any use but
just for knowledge sake


Unfortunately, C has no builtin native support for threading. As far
as C is concerned, there is only one execution path. Your code is
just calling either consume() or produce() with a random integer
argument, and then returning to the same point in your original code.
If you /really/ want to implement a thread library you're going to
have to ask in a platform-specific group about how to do this.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 15 '06 #4
In article <11************ **********@g47g 2000cwa.googleg roups.com>
Sreekanth <sr************ ********@gmail. com> wrote:
I am trying to implement multiple threads using setjmp/longjmp


If you are using one of the longjmp() routines that I wrote (I have
written several), it checks to make sure you are not trying to use
it to implement threads.[%] If you are, it aborts.

The longjmp() function is not suitable for implementing threads.
In general, you must write assembly code to handle thread-switching.
Do not use longjmp().

[% Actually, it checks for a more general error, "going the wrong
way in the stack". This happens to include thread switches
approximately half the time. It is meant to catch the much more
common bug of attempting to jump into a stack frame that no longer
exists, and produce a debuggable crash instead of an undebuggable
one. The principle that longjmp() cannot be used to implement
threads still applies, however. Similarly, longjmp() cannot be
used to implement coroutines. Like threads, coroutine implementations
involve stack behavior that is not modeled by simple "up/down"
rules.]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 15 '06 #5

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

Similar topics

6
4857
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
2
2124
by: Ravi Uday | last post by:
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.
18
2453
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 compatibility problems. The contents of the jmp_buf buffer are specific
5
1875
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
2324
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; };
2
1669
by: JS | last post by:
I have this struct: struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int stak; };
8
2422
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...
3
1898
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 =====================
0
9464
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,...
0
10292
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, 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...
0
9923
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
8954
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...
1
7471
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
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();...
0
5368
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
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.