Problem with setjmp and long jump

Discussion in 'C Programming' started by Sreekanth, Feb 15, 2006.

  1. Sreekanth

    Sreekanth Guest

    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
     
    Sreekanth, Feb 15, 2006
    #1
    1. Advertising

  2. In article <>,
    "Sreekanth" <> 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.
     
    Christian Bau, Feb 15, 2006
    #2
    1. Advertising

  3. Sreekanth

    Sreekanth Guest

    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;
    }
     
    Sreekanth, Feb 15, 2006
    #3
  4. On 15 Feb 2006 02:10:02 -0800, in comp.lang.c , "Sreekanth"
    <> 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 =----
     
    Mark McIntyre, Feb 15, 2006
    #4
  5. Sreekanth

    Chris Torek Guest

    In article <>
    Sreekanth <> 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.
     
    Chris Torek, Feb 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    709
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,223
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    497
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    832
    Peter Nilsson
    Jan 15, 2008
  5. Eadwine Rose
    Replies:
    2
    Views:
    222
    Eadwine Rose
    Oct 15, 2006
Loading...

Share This Page