Problem with setjmp and long jump

S

Sreekanth

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,1)


and trying to use a for loop to iterate and check for setjmp condition

for(i=0;i <mybuffsize; i++)
if(setjmp(mybuf)
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
 
C

Christian Bau

"Sreekanth said:
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.
 
S

Sreekanth

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;
}
 
M

Mark McIntyre

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
 
C

Chris Torek

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.]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top