DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

getcontext(S)


getcontext -- get current user context

Synopsis

   #include <ucontext.h>
   

int getcontext(ucontext_t *ucp);

int setcontext(const ucontext_t *ucp);

Description

These functions, along with those defined in makecontext(S), 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(M) 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. The 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.

References

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

Notices

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.

An equivalent getcontext routine is provided in libc to handle floating point contexts for Pentium III processors; the behavior of setcontext has also been enhanced to handle floating point context changes during signal handling. See ``Pentium III extended floating point support'' in New features for more information.

Considerations for threads programming

setcontext affects the calling thread only; siblings are not affected.

Considerations for lightweight processes

The Threads Library has a ``wrapper'' function for setcontext that will set the thread's signal mask as well. The system call itself only modifies the underlying LWP.
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 - 01 June 2005