By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,368 Members | 1,701 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,368 IT Pros & Developers. It's quick & easy.

Problem with setjmp and long jump

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
In article <11**********************@g47g2000cwa.googlegroups .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

P: n/a
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 numberofproducers;
int numberofconsumers;

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

buffer sharedbuffer;

void produce(int number)
{
if (sharedbuffer.size == MAXSIZE)
{
int myrandom = rand();
myrandom = myrandom %numberofconsumers;
if (myrandom == 0)
myrandom = 1;
longjmp(cons[myrandom],1);
}
else
{
sharedbuffer.number[sharedbuffer.size ++] = number;
printf("The Producer %d is executing\n",number);
longjmp(thread,1);
}
}
void consume(int number)
{
if (sharedbuffer.size == 0)
{
int myrandom = rand();
myrandom = myrandom % numberofproducers;
if (myrandom == 0)
myrandom = 1;
longjmp(prod[myrandom],1);
}
else
{
printf("The Conumser is consuming
%d\n",sharedbuffer.number[--sharedbuffer.size]);
longjmp(thread,1);
}
}

void switcher()
{
int myrandom = rand();
myrandom = myrandom %(numberofproducers + numberofconsumers);

if (myrandom >= numberofproducers)
consume(myrandom);
else
produce(myrandom);
}
int main(void)
{
int i;
int tp=0;
int tc=0;
printf("Enter the number of producers : : ");
scanf("%d",&numberofproducers);
printf("Enter the number of consumers : : ");
scanf("%d",&numberofconsumers);

i = 0;

sharedbuffer.size = 1;
while (i < 50)

{
if(setjmp(thread))
switcher();
else
switcher();
i++;
}

while(!setjmp(cons[tp])&& tp < numberofconsumers)
{
tp = tp+1;
}
if(setjmp(cons[tp]))
produce(tp);

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

Feb 15 '06 #3

P: n/a
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

P: n/a
In article <11**********************@g47g2000cwa.googlegroups .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 (4039.22'N, 11150.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 discussion thread is closed

Replies have been disabled for this discussion.