---
name: "setcontext"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/getcontext/3"
---

# setcontext(3)

get or set the user context

Part of [getcontext(3)](/getcontext/3.md).

## 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

In a System V-like environment, one has the two types *mcontext\_t* and *ucontext\_t* defined in *\<ucontext.h\>* and the four functions **getcontext**(), **setcontext**(), [**makecontext**(3)](/makecontext/3.md), and [**swapcontext**(3)](/swapcontext/3.md) that allow user-level context switching between multiple threads of control within a process.

The *mcontext\_t* type is machine-dependent and opaque. The *ucontext\_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;
```

with *sigset\_t* and *stack\_t* defined in *\<signal.h\>*. Here *uc\_link* points to the context that will be resumed when the current context terminates (in case the current context was created using [**makecontext**(3)](/makecontext/3.md)), *uc\_sigmask* is the set of signals blocked in this context (see [**sigprocmask**(2)](/sigprocmask/2.md)), *uc\_stack* is the stack used by this context (see [**sigaltstack**(2)](/sigaltstack/2.md)), and *uc\_mcontext* is the machine-specific representation of the saved context, that includes the calling thread's machine registers.

The function **getcontext**() initializes the structure pointed to by *ucp* to the currently active context.

The function **setcontext**() restores the user context pointed to by *ucp*. A successful call does not return. The context should have been obtained by a call of **getcontext**(), or [**makecontext**(3)](/makecontext/3.md), or received as the third argument to a signal handler (see the discussion of the **SA\_SIGINFO** flag in [**sigaction**(2)](/sigaction/2.md)).

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)](/makecontext/3.md), program execution continues by a call to the function *func* specified as the second argument of that call to [**makecontext**(3)](/makecontext/3.md). 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)](/makecontext/3.md). 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

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.

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **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.

## NOTES

The earliest incarnation of this mechanism was the [**setjmp**(3)](/setjmp/3.md)/ [**longjmp**(3)](/longjmp/3.md) mechanism. Since that does not define the handling of the signal context, the next stage was the [**sigsetjmp**(3)](/sigsetjmp/3.md)/ [**siglongjmp**(3)](/siglongjmp/3.md) pair. The present mechanism gives much more control. On the other hand, 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)](/longjmp/3.md): it is undefined what would happen with contexts. Use [**siglongjmp**(3)](/siglongjmp/3.md) or **setcontext**() instead.

## SEE ALSO

[**sigaction**(2)](/sigaction/2.md), [**sigaltstack**(2)](/sigaltstack/2.md), [**sigprocmask**(2)](/sigprocmask/2.md), [**longjmp**(3)](/longjmp/3.md), [**makecontext**(3)](/makecontext/3.md), [**sigsetjmp**(3)](/sigsetjmp/3.md), [**signal**(7)](/signal/7.md)
