#include #include static ucontext_t ctx[3]; //0 for main, 1 for f1, 2 for f2. //-> swap +-> return to //main -> f2 -> f1 -> f2 +-> f1 +->main. static void f1 (void) { puts("start f1"); swapcontext(&ctx[1], &ctx[2]); puts("finish f1"); } static void f2 (void) { puts("start f2"); swapcontext(&ctx[2], &ctx[1]); puts("finish f2"); } int main (void) { char st1[8192]; char st2[8192]; getcontext(&ctx[1]); ctx[1].uc_stack.ss_sp = st1; ctx[1].uc_stack.ss_size = sizeof st1; ctx[1].uc_link = &ctx[0]; //f1 returns to main. makecontext(&ctx[1], f1, 0); getcontext(&ctx[2]); ctx[2].uc_stack.ss_sp = st2; ctx[2].uc_stack.ss_size = sizeof st2; ctx[2].uc_link = &ctx[1]; //f2 returns to f1. makecontext(&ctx[2], f2, 0); // assign ctx[2] to f2. swapcontext(&ctx[0], &ctx[2]); // give CPU to f2. return 0; } /* OUTPUT start f2 start f1 finish f2 finish f1 */