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

A

Andrey Popp

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.
 
S

Shao Miller

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.
 
B

Barry Schwarz

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.
 
J

Jens Gustedt

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
 
U

Uncle Steve

Hello,

Am 01/29/2012 05:06 PM, schrieb Andrey Popp:

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
 
K

Keith Thompson

Uncle Steve said:
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*
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top