makecontext(3) Library Functions Manual makecontext(3) makecontext, swapcontext - C (libc, -lc) #include void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...); int swapcontext(ucontext_t *restrict oucp, const ucontext_t *restrict ucp); In a System V-like environment, one has the type ucontext_t (defined in and described in getcontext(3)) and the four functions getcontext(3), setcontext(3), makecontext(), and swapcontext() that allow user-level context switching between multiple threads of control within a process. makecontext() , ucp ( getcontext(3)). makecontext(), ucp->uc_stack, ucp->uc_link. , ( setcontext(3) swapcontext()), func (int), argc; argc. . NULL, . swapcontext() , oucp, , ucp. When successful, swapcontext() does not return. (But we may return later, in case oucp is activated, in which case it looks like swapcontext() returns 0.) On error, swapcontext() returns -1 and sets errno to indicate the error. ENOMEM . attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |makecontext() | | MT-Safe race:ucp | +----------------------------+----------------------------------------------------------+--------------------------+ |swapcontext() | | MT-Safe race:oucp | | | | race:ucp | +----------------------------+----------------------------------------------------------+--------------------------+ None. glibc 2.1. SUSv2, POSIX.1-2001. Removed in POSIX.1-2008, citing portability issues, and recommending that applications be rewritten to use POSIX threads instead. ucp->uc_stack sigaltstack(2), : , , . , . On architectures where int and pointer types are the same size (e.g., x86-32, where both types are 32 bits), you may be able to get away with passing pointers as arguments to makecontext() following argc. However, doing this is not guaranteed to be portable, is undefined according to the standards, and won't work on architectures where pointers are larger than ints. Nevertheless, starting with glibc 2.8, glibc makes some changes to makecontext(), to permit this on some 64-bit architectures (e.g., x86-64). , , getcontext(3), makecontext() swapcontext(). : $ ./a.out main: swapcontext(&uctx_main, &uctx_func2) func2: func2: swapcontext(&uctx_func2, &uctx_func1) func1: func1: swapcontext(&uctx_func1, &uctx_func2) func2: func1: main: #include #include #include static ucontext_t uctx_main, uctx_func1, uctx_func2; #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) 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) handle_error("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) handle_error("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) handle_error("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) handle_error("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) handle_error("swapcontext"); printf("%s: exiting\n", __func__); exit(EXIT_SUCCESS); } sigaction(2), sigaltstack(2), sigprocmask(2), getcontext(3), sigsetjmp(3) () aereiae , Alexey , Azamat Hackimov , Dmitriy S. Seregin , Dmitry Bolkhovskikh , ITriskTI , Max Is , Yuri Kozlov , ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 15 2024 . makecontext(3)