1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <stdlib.h>
#include <ucontext.h>
#include <unistd.h>
int foo(ucontext_t *old, ucontext_t *new)
{
swapcontext(old, new);
}
int bar(int c)
{
return c + getpid();
}
int baz(volatile int c)
{
return c % 2;
}
#define STACKSIZE 8192
int main(int argc, char *argv[])
{
int n = 10;
char stack[STACKSIZE];
ucontext_t curr, new;
if (argc > 1)
n = atoi(argv[1]);
getcontext(&new);
new.uc_link = &curr;
new.uc_stack.ss_sp = stack;
new.uc_stack.ss_size = STACKSIZE;
makecontext(&new, (void (*)(void))bar, 1, n);
foo(&curr, &new);
n = baz(n);
return !(n < 2);
}
|