Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   [I'm learning C]: Learning to use ucontext (http://www.velocityreviews.com/forums/t867689-im-learning-c-learning-to-use-ucontext.html)

Andrey Popp 01-29-2012 04:06 PM

[I'm learning C]: Learning to use ucontext
 
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.

Shao Miller 01-29-2012 04:42 PM

Re: [I'm learning C]: Learning to use ucontext
 
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!"

Barry Schwarz 01-29-2012 06:07 PM

Re: [I'm learning C]: Learning to use ucontext
 
On Sun, 29 Jan 2012 08:06:12 -0800 (PST), Andrey Popp
<8mayday@gmail.com> 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

Jens Gustedt 01-30-2012 07:38 PM

Re: [I'm learning C]: Learning to use ucontext
 
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

Uncle Steve 01-30-2012 11:58 PM

Re: [I'm learning C]: Learning to use ucontext
 
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. ****.


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.


Keith Thompson 01-31-2012 01:05 AM

Re: [I'm learning C]: Learning to use ucontext
 
Uncle Steve <stevet810@gmail.com> 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) kst-u@mib.org <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"


All times are GMT. The time now is 12:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.