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 using makecontext(3)).
  • uc_sigmask β€” the set of signals blocked in this context (peep sigprocmask(2)).
  • uc_stack β€” the stack used by this context (peep sigaltstack(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_SIGINFO flag in sigaction(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 function func specified as the second argument of that call to makecontext(3). When the function func returns, we continue with the uc_link member of the structure ucp specified as the first argument of that call to makecontext(3). When this member is NULL, 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 using longjmp(3): it is undefined what would happen with contexts. Use siglongjmp(3) or setcontext() 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms