[I'm learning C]: Learning to use ucontext

Discussion in 'C Programming' started by Andrey Popp, Jan 29, 2012.

  1. Andrey Popp

    Andrey Popp Guest

    Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:

    #include <stdlib.h>
    #include <stdio.h>
    #include <ucontext.h>

    static ucontext_t routine_main;
    static ucontext_t routine1;
    static ucontext_t routine2;

    void runner(int n) {
    fprintf(stderr, "runner %d\n", n);
    while(1) {
    fprintf(stderr, "runner %d\n", n);
    if (n == 1)
    swapcontext(&routine1, &routine2);
    else
    swapcontext(&routine2, &routine1);
    }
    }

    int main(int argc, char **argv) {
    char st1c[8192];
    char st2c[8192];

    if (getcontext(&routine1) == -1)
    abort();
    routine1.uc_link = &routine_main;
    routine1.uc_stack.ss_sp = st1c;
    routine1.uc_stack.ss_size = sizeof st1c;
    makecontext(&routine1, (void(*)(void))runner, 1, 1);

    if (getcontext(&routine2) == -1)
    abort();
    routine2.uc_link = &routine_main;
    routine2.uc_stack.ss_sp = st2c;
    routine2.uc_stack.ss_size = sizeof st2c;
    makecontext(&routine1, (void(*)(void))runner, 1, 2);

    fprintf(stderr, "start executing\n");
    if (swapcontext(&routine_main, &routine1) == -1)
    abort();
    fprintf(stderr, "exiting\n");
    return 0;
    }

    which doesn't work the way it should — it just constantly prints "start executing", which means it doesn't change execution context from routeine_main to routine1 but instead from routine_main to routine_main.
    Andrey Popp, Jan 29, 2012
    #1
    1. Advertising

  2. Andrey Popp

    Shao Miller Guest

    On 1/29/2012 11:06, Andrey Popp wrote:
    > Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:
    >


    As far as I know, 'ucontext.h' isn't standard C. I think that's worth
    noting.

    > #include<stdlib.h>
    > #include<stdio.h>
    > #include<ucontext.h>
    >
    > static ucontext_t routine_main;
    > static ucontext_t routine1;
    > static ucontext_t routine2;
    >
    > void runner(int n) {
    > fprintf(stderr, "runner %d\n", n);
    > while(1) {
    > fprintf(stderr, "runner %d\n", n);
    > if (n == 1)
    > swapcontext(&routine1,&routine2);
    > else
    > swapcontext(&routine2,&routine1);
    > }
    > }
    >
    > int main(int argc, char **argv) {
    > char st1c[8192];
    > char st2c[8192];
    >
    > if (getcontext(&routine1) == -1)
    > abort();
    > routine1.uc_link =&routine_main;
    > routine1.uc_stack.ss_sp = st1c;
    > routine1.uc_stack.ss_size = sizeof st1c;
    > makecontext(&routine1, (void(*)(void))runner, 1, 1);
    >
    > if (getcontext(&routine2) == -1)
    > abort();
    > routine2.uc_link =&routine_main;
    > routine2.uc_stack.ss_sp = st2c;
    > routine2.uc_stack.ss_size = sizeof st2c;
    > makecontext(&routine1, (void(*)(void))runner, 1, 2);
    >


    Does the line immediately above include a typo? Did you mean to modify
    the 'routine1' context or the 'routine2' context there?

    > fprintf(stderr, "start executing\n");
    > if (swapcontext(&routine_main,&routine1) == -1)
    > abort();
    > fprintf(stderr, "exiting\n");
    > return 0;
    > }
    >
    > which doesn't work the way it should — it just constantly prints "start executing", which means it doesn't change execution context from routeine_main to routine1 but instead from routine_main to routine_main.


    That's unfortunate.

    --
    "The stationery store has moved. Aaargh!"
    Shao Miller, Jan 29, 2012
    #2
    1. Advertising

  3. On Sun, 29 Jan 2012 08:06:12 -0800 (PST), Andrey Popp
    <> wrote:

    >Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:
    >
    >#include <stdlib.h>
    >#include <stdio.h>
    >#include <ucontext.h>
    >
    >static ucontext_t routine_main;


    snip

    > swapcontext(&routine1, &routine2);


    snip

    > if (getcontext(&routine1) == -1)
    > abort();
    > routine1.uc_link = &routine_main;
    > routine1.uc_stack.ss_sp = st1c;
    > routine1.uc_stack.ss_size = sizeof st1c;
    > makecontext(&routine1, (void(*)(void))runner, 1, 1);


    snip

    >which doesn't work the way it should — it just constantly prints "start executing", which means it doesn't change execution context from routeine_main to routine1 but instead from routine_main to routine_main.


    If you gave us the contents of ucontext.h and the definitions of all
    the non-standard functions you call, someone might be inclined to help
    you debug this. It might also help if you described what "change
    execution context" means. As it stands, a response other than "logic
    error on line 42 of routine x" is just wishful thinking.

    --
    Remove del for email
    Barry Schwarz, Jan 29, 2012
    #3
  4. Andrey Popp

    Jens Gustedt Guest

    Hello,

    Am 01/29/2012 05:06 PM, schrieb Andrey Popp:
    > Hello, I started to experimenting with ucontext to implement basic
    > cooperative multithreading and stuccoed with this piece of code:


    This sounds like a bad idea to me. My man page tells me:

    POSIX.1-2008 removes the specifications of makecontext() and
    swapcontext(), citing portability issues, and recommending
    that applications be rewritten to use POSIX threads instead.

    so such an implementation looks like a dead end to me.

    Jens
    Jens Gustedt, Jan 30, 2012
    #4
  5. Andrey Popp

    Uncle Steve Guest

    On Mon, Jan 30, 2012 at 08:38:39PM +0100, Jens Gustedt wrote:
    > Hello,
    >
    > Am 01/29/2012 05:06 PM, schrieb Andrey Popp:
    > > Hello, I started to experimenting with ucontext to implement basic
    > > cooperative multithreading and stuccoed with this piece of code:

    >
    > This sounds like a bad idea to me. My man page tells me:
    >
    > POSIX.1-2008 removes the specifications of makecontext() and
    > swapcontext(), citing portability issues, and recommending
    > that applications be rewritten to use POSIX threads instead.
    >
    > so such an implementation looks like a dead end to me.
    >
    > Jens


    Posix threads suck. You hafta **** around like you wouldn't believe
    in order to make something workable.

    **** posix, even though I rely on it. Shit.


    Regards,

    Uncle Steve

    --
    I don't even want the Charter back, as it was clearly 'used' by a big
    blue dude when he ran out of toilet paper. And the Criminal Code of
    Canada is of course now officially listed in Jane's Miscellaneous
    Munitions under the Anti-Personnel section.
    Uncle Steve, Jan 30, 2012
    #5
  6. Uncle Steve <> writes:
    [...]
    > Posix threads suck. You hafta f*** around like you wouldn't believe
    > in order to make something workable.
    >
    > F*** posix, even though I rely on it. S***.


    *Plonk*

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 31, 2012
    #6
    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. Hal Vaughan
    Replies:
    7
    Views:
    458
  2. Longfellow

    Newbie: learning to use malloc().

    Longfellow, Jul 16, 2006, in forum: C Programming
    Replies:
    11
    Views:
    524
    Keith Thompson
    Jul 17, 2006
  3. KDawg44

    Learning to use wxPython

    KDawg44, Apr 27, 2007, in forum: Python
    Replies:
    2
    Views:
    369
    KDawg44
    Apr 27, 2007
  4. -Lost

    No point in learning C? Use C++ instead?

    -Lost, Aug 14, 2007, in forum: C Programming
    Replies:
    24
    Views:
    723
    CBFalconer
    Aug 17, 2007
  5. Replies:
    14
    Views:
    260
    Tad McClellan
    Apr 26, 2006
Loading...

Share This Page