473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 7540
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
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
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**********************@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 (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
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...
2
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...
18
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...
5
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
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...
2
by: JS | last post by:
I have this struct: struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int stak; };
8
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
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....
3
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. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...

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.