makecontext(3) Library Functions Manual makecontext(3) makecontext, swapcontext - (libc -lc) #include void makecontext(ucontext_t *ucp, typeof(void (int arg0, ...)) *func, int argc, ...); int swapcontext(ucontext_t *restrict oucp, const ucontext_t *restrict ucp); System V ucontext_t ( getcontext(3)) getcontext(3) setcontext(3) makecontext() swapcontext() . makecontext() ucp ( getcontext(3)). makecontext() ucp->uc_stack ucp->uc_link. ( setcontext(3) swapcontext()) func (int) argc argc. . NULL . swapcontext() oucp ucp. swapcontext(). ( oucp swapcontext() 0.) swapcontext() -1 errno . ENOMEM . attributes(7). +----------------------+------------------------------------+----------+ | | | | +----------------------+------------------------------------+----------+ |makecontext() | | | | | | | | | | | | | | (MT-Safe) | | | | :ucp | +----------------------+------------------------------------+----------+ |swapcontext() | | MT-Safe | | | | race:oucp | | | | race:ucp | +----------------------+------------------------------------+----------+ . glibc 2.1. SUSv2, POSIX.1-2001. POSIX.1-2008 POSIX . ucp->uc_stack sigaltstack(2) . . int ( x86-32 32 ) makecontext() argc. int. glibc 2.8 glibc makecontext() 64 ( x86-64). getcontext(3) makecontext() swapcontext(). : $ ./a.out main: swapcontext(&uctx_main, &uctx_func2) func2: started func2: swapcontext(&uctx_func2, &uctx_func1) func1: started func1: swapcontext(&uctx_func1, &uctx_func2) func2: returning func1: returning main: exiting #include #include #include #include static ucontext_t uctx_main, uctx_func1, uctx_func2; static void func1(void) { printf("%s: started\n", __func__); printf("%s: swapcontext(&uctx_func1, &uctx_func2)\n", __func__); if (swapcontext(&uctx_func1, &uctx_func2) == -1) err(EXIT_FAILURE, "swapcontext"); printf("%s: returning\n", __func__); } static void func2(void) { printf("%s: started\n", __func__); printf("%s: swapcontext(&uctx_func2, &uctx_func1)\n", __func__); if (swapcontext(&uctx_func2, &uctx_func1) == -1) err(EXIT_FAILURE, "swapcontext"); printf("%s: returning\n", __func__); } int main(int argc, char *argv[]) { char func1_stack[16384]; char func2_stack[16384]; if (getcontext(&uctx_func1) == -1) err(EXIT_FAILURE, "getcontext"); uctx_func1.uc_stack.ss_sp = func1_stack; uctx_func1.uc_stack.ss_size = sizeof(func1_stack); uctx_func1.uc_link = &uctx_main; makecontext(&uctx_func1, func1, 0); if (getcontext(&uctx_func2) == -1) err(EXIT_FAILURE, "getcontext"); uctx_func2.uc_stack.ss_sp = func2_stack; uctx_func2.uc_stack.ss_size = sizeof(func2_stack); /* Successor context is f1(), unless argc > 1 */ uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1; makecontext(&uctx_func2, func2, 0); printf("%s: swapcontext(&uctx_main, &uctx_func2)\n", __func__); if (swapcontext(&uctx_main, &uctx_func2) == -1) err(EXIT_FAILURE, "swapcontext"); printf("%s: exiting\n", __func__); exit(EXIT_SUCCESS); } sigaction(2), sigaltstack(2), sigprocmask(2), getcontext(3), sigsetjmp(3) 3 . . : . 6.18 8 2026 makecontext(3)