Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


getcontext(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |NOTES |SEE ALSO |COLOPHON

getcontext(3)            Library Functions Manualgetcontext(3)

NAME        top

       getcontext, setcontext - get or set the user context

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <ucontext.h>int getcontext(ucontext_t *ucp);int setcontext(const ucontext_t *ucp);

DESCRIPTION        top

       In a System V-like environment, one has the two typesmcontext_t       anducontext_t defined in<ucontext.h> and the four functionsgetcontext(),setcontext(),makecontext(3), andswapcontext(3)       that allow user-level context switching between multiple threads       of control within a process.       Themcontext_t type is machine-dependent and opaque.  Theucontext_t type is a structure that has at least the following       fields:           typedef struct ucontext_t {               struct ucontext_t *uc_link;               sigset_t          uc_sigmask;               stack_t           uc_stack;               mcontext_t        uc_mcontext;               ...           } ucontext_t;       withsigset_t andstack_t defined in<signal.h>.  Hereuc_link       points to the context that will be resumed when the current       context terminates (in case the current context was created usingmakecontext(3)),uc_sigmask is the set of signals blocked in this       context (seesigprocmask(2)),uc_stack is the stack used by this       context (seesigaltstack(2)), anduc_mcontext is the machine-       specific representation of the saved context, that includes the       calling thread's machine registers.       The functiongetcontext() initializes the structure pointed to byucp to the currently active context.       The functionsetcontext() restores the user context pointed to byucp.  A successful call does not return.  The context should have       been obtained by a call ofgetcontext(), ormakecontext(3), or       received as the third argument to a signal handler (see the       discussion of theSA_SIGINFOflag insigaction(2)).       If the context was obtained by a call ofgetcontext(), program       execution continues as if this call just returned.       If the context was obtained by a call ofmakecontext(3), program       execution continues by a call to the functionfunc specified as       the second argument of that call tomakecontext(3).  When the       functionfunc returns, we continue with theuc_link member of the       structureucp specified as the first argument of that call tomakecontext(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, this sentence was removed in SUSv2, and the       present verdict is "the result is unspecified".

RETURN VALUE        top

       When successful,getcontext() returns 0 andsetcontext() does not       return.  On error, both return -1 and seterrno to indicate the       error.

ERRORS        top

       None defined.

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌─────────────────────────────┬───────────────┬──────────────────┐       │InterfaceAttributeValue│       ├─────────────────────────────┼───────────────┼──────────────────┤       │getcontext(),setcontext()  │ Thread safety │ MT-Safe race:ucp │       └─────────────────────────────┴───────────────┴──────────────────┘

STANDARDS        top

       None.

HISTORY        top

       SUSv2, POSIX.1-2001.       POSIX.1-2008 removes these functions, citing portability issues,       and recommending that applications be rewritten to use POSIX       threads instead.

NOTES        top

       The earliest incarnation of this mechanism was thesetjmp(3)/longjmp(3) mechanism.  Since that does not define the       handling of the signal context, the next stage was thesigsetjmp(3)/siglongjmp(3) pair.  The present mechanism gives much       more control.  On the other hand, there is no easy way to detect       whether a return fromgetcontext() is from the first call, or via       asetcontext() 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.

SEE ALSO        top

sigaction(2),sigaltstack(2),sigprocmask(2),longjmp(3),makecontext(3),sigsetjmp(3),signal(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17getcontext(3)

Pages that refer to this page:PR_SET_SYSCALL_USER_DISPATCH(2const)sigaction(2)sigreturn(2)makecontext(3)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp