getcontext(3)
β¨ getcontext(3) β¨
okay bestie, this is a Library Functions Manual page for getcontext(3) and it's lowkey one of the most main-character-flavoured corners of libc. buckle up π
π NAME π
getcontext, setcontext β get or set the user context
π LIBRARY π
Standard C library (libc, -lc) π
π¦ SYNOPSIS π¦
#include <ucontext.h>
int getcontext(ucontext_t *ucp);
int setcontext(const ucontext_t *ucp);
πΈ DESCRIPTION πΈ
so in a System Vβlike environment you get two types, mcontext_t and ucontext_t, both defined in <ucontext.h>, plus four functions: getcontext(), setcontext(), makecontext(3), and swapcontext(3). together they let you do user-level context switching between multiple threads of control inside one process. yes bestie, real ones. π§
The mcontext_t type is machine-dependent and opaque β you don't peek inside, you don't touch, no cap. π
The ucontext_t type is a structure that has at least these fields:
typedef struct ucontext_t {
struct ucontext_t *uc_link;
sigset_t uc_sigmask;
stack_t uc_stack;
mcontext_t uc_mcontext;
...
} ucontext_t;
with sigset_t and stack_t defined in <signal.h>. here's the tea on each field π«:
uc_linkβ points to the context that gets resumed when the current context terminates (that's only in the case where the current context was created usingmakecontext(3)).uc_sigmaskβ the set of signals blocked in this context (peepsigprocmask(2)).uc_stackβ the stack used by this context (peepsigaltstack(2)).uc_mcontextβ the machine-specific representation of the saved context, and it includes the calling thread's machine registers. iconic. π
getcontext() initializes the structure pointed to by ucp to the currently active context. β¨
setcontext() restores the user context pointed to by ucp. and listen β a successful call does not return. like, it's gone, it left the chat, periodt. π
That context should have come from one of these three places:
- a call of
getcontext() - a call of
makecontext(3) - being received as the third argument to a signal handler (see the discussion of the
SA_SIGINFOflag insigaction(2))
Now, what happens next depends on where your context came from:
- If the context was obtained by a call of
getcontext(), program execution continues as if this call just returned. π - If the context was obtained by a call of
makecontext(3), program execution continues by a call to the functionfuncspecified as the second argument of that call tomakecontext(3). When the functionfuncreturns, we continue with theuc_linkmember of the structureucpspecified as the first argument of that call tomakecontext(3). When this member isNULL, the thread exits. πͺ - If the context was obtained by a call to a signal handler, then old standard text says that "program execution continues with the program instruction following the instruction interrupted by the signal". However, that sentence got removed in SUSv2, and the present verdict is "the result is unspecified". π so like, don't count on it.
π« RETURN VALUE π«
When successful, getcontext() returns 0 and setcontext() does not return. On error, both return -1 and set errno to indicate the error. π«Ά
π ERRORS π
None defined. (nothing! zero! the void! empty! π )
π ATTRIBUTES π
For an explanation of the terms used in this section, see attributes(7).
getcontext(),setcontext()β Thread safety:MT-Safe race:ucpπ₯
β STANDARDS β
None. π
π HISTORY π
SUSv2, POSIX.1-2001.
POSIX.1-2008 removes these functions, citing portability issues, and recommending that applications be rewritten to use POSIX threads instead. not the glow-down we wanted but it is what it is. π
π NOTES π
The earliest incarnation of this mechanism was the setjmp(3)/longjmp(3) mechanism. Since that does not define the handling of the signal context, the next stage was the sigsetjmp(3)/siglongjmp(3) pair. The present mechanism gives much more control. ποΈ
On the other hand β and this is the annoying part, bestie β there is no easy way to detect whether a return from getcontext() is from the first call, or via a setcontext() call. The user has to invent their own bookkeeping device, and a register variable won't do, since registers are restored. π
β οΈ When a signal occurs, the current user context is saved and a new context is created by the kernel for the signal handler. Do not leave the handler usinglongjmp(3): it is undefined what would happen with contexts. Usesiglongjmp(3)orsetcontext()instead. fr fr, this one matters. π¨
π SEE ALSO π
sigaction(2), sigaltstack(2), sigprocmask(2), longjmp(3), makecontext(3), sigsetjmp(3), signal(7)
mwah, that's the whole page. π this lil love letter to context switching comes to you from Linux man-pages 6.20, dated 2026-02-08 β so if you cite it, cite it cute. bye bestie, go switch some contexts responsibly π¦πβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2001, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms