Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with setjmp and long jump

Reply
Thread Tools

Problem with setjmp and long jump

 
 
Sreekanth
Guest
Posts: n/a
 
      02-15-2006
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

 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      02-15-2006
In article <(E-Mail Removed) .com>,
"Sreekanth" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Sreekanth
Guest
Posts: n/a
 
      02-15-2006
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;
}

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-15-2006
On 15 Feb 2006 02:10:02 -0800, in comp.lang.c , "Sreekanth"
<(E-Mail Removed)> 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 =----
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      02-15-2006
In article <(E-Mail Removed) .com>
Sreekanth <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
setjmp and longjmp problem aleksa C Programming 5 09-07-2010 01:57 AM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Use of Long and Long Long Bart C C Programming 27 01-15-2008 05:27 AM
long long and long Mathieu Dutour C Programming 4 07-24-2007 11:15 AM
Double left click jump to top, same with ctrl or shift to jump to bottom? Eadwine Rose Javascript 2 10-15-2006 08:38 PM



Advertisments