getcontext(3)


NAME

   getcontext, setcontext - get or set the user context

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), and swapcontext(3) 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 {
           struct ucontext *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)),
   uc_sigmask  is  the  set  of  signals  blocked  in  this  context  (see
   sigprocmask(2)),  uc_stack  is  the  stack  used  by  this context (see
   sigaltstack(2)), 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 at  by  ucp
   to the currently active context.

   The  function setcontext() restores the user context pointed at by ucp.
   A successful call does  not  return.   The  context  should  have  been
   obtained  by  a  call  of getcontext(), or makecontext(3), or passed as
   third argument to a signal handler.

   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, 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 appropriately.

ERRORS

   None defined.

ATTRIBUTES

   For   an   explanation   of   the  terms  used  in  this  section,  see
   attributes(7).

   
   Interface                   Attribute      Value            
   
   getcontext(), setcontext()  Thread safety  MT-Safe race:ucp 
   

CONFORMING TO

   SUSv2,  POSIX.1-2001.   POSIX.1-2008  removes  the   specification   of
   getcontext(),   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)/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, 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 her 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.

SEE ALSO

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

COLOPHON

   This  page  is  part of release 4.09 of the Linux man-pages project.  A
   description of the project, information about reporting bugs,  and  the
   latest     version     of     this    page,    can    be    found    at
   https://www.kernel.org/doc/man-pages/.





Opportunity


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.





Free Software


Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.


Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.





Free Books


The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.


Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.





Education


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.


Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.