getcontext, setcontext -- get and set current user context


cc . . . -lc
#include <ucontext.h>

int getcontext(ucontext_t *ucp);

int setcontext(ucontext_t *ucp);


These functions are useful for implementing user level context switching between multiple threads of control within a process.

getcontext initializes the structure pointed to by ucp to the current user context of the calling thread. The user context is defined by ucontext(FP) and includes the contents of the calling thread's machine registers, signal mask and execution stack.

setcontext restores the user context pointed to by ucp. A successful call to setcontext does not return; program execution resumes at the point specified by the context structure passed to setcontext. The context structure should have been one created either by a prior call to getcontext or makecontext or passed as the third argument to a signal handler (see sigaction(S)). If the context structure was one created with getcontext, program execution continues as if the corresponding call of getcontext had just returned. If the context structure was one created with makecontext, program execution continues with the function specified to makecontext.

Return values

On success, setcontext does not return and getcontext returns 0. On failure, setcontext and getcontext return -1 and set errno to identify the error.


The pointer ucp did not print to a valid address.


When a signal handler is executed, the current user context is saved and a new context is created by the kernel. If the thread leaves the signal handler via longjmp (see setjmp(S)) the original context will not be restored, and future calls to getcontext will not be reliable. Signal handlers should use siglongjmp (see setjmp(S)) or setcontext instead.


linking library

See also

makecontext(S), setjmp(S), sigaction(S), sigaltstack(S), sigprocmask(S), ucontext(FP)

Standards conformance

getcontext and setcontext are conformant with:

AT&T SVID Issue 3.

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003