{"id":6875,"date":"2022-12-20T19:33:45","date_gmt":"2022-12-20T22:33:45","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/getcontext-man3\/"},"modified":"2022-12-20T19:33:45","modified_gmt":"2022-12-20T22:33:45","slug":"getcontext-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/getcontext-man3\/","title":{"rendered":"GETCONTEXT (man3)"},"content":{"rendered":"<h1 align=\"center\">GETCONTEXT<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">getcontext, setcontext \u2212 get or set the user context<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <ucontext.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>int getcontext(ucontext_t *<\/b><i>ucp<\/i><b>); <br \/> int setcontext(const ucontext_t *<\/b><i>ucp<\/i><b>);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In a System V-like environment, one has the two types <i>mcontext_t<\/i> and <i>ucontext_t<\/i> defined in <i><ucontext.h><\/i> and the four functions <b>getcontext<\/b>(), <b>setcontext<\/b>(), <b>makecontext<\/b>(3), and <b>swapcontext<\/b>(3) that allow user-level context switching between multiple threads of control within a process.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>mcontext_t<\/i> type is machine-dependent and opaque. The <i>ucontext_t<\/i> type is a structure that has at least the following fields:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">typedef struct ucontext_t { <br \/> struct ucontext_t *uc_link; <br \/> sigset_t uc_sigmask; <br \/> stack_t uc_stack; <br \/> mcontext_t uc_mcontext; <br \/> &#8230; <br \/> } ucontext_t;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">with <i>sigset_t<\/i> and <i>stack_t<\/i> defined in <i><signal.h><\/i>. Here <i>uc_link<\/i> points to the context that will be resumed when the current context terminates (in case the current context was created using <b>makecontext<\/b>(3)), <i>uc_sigmask<\/i> is the set of signals blocked in this context (see <b>sigprocmask<\/b>(2)), <i>uc_stack<\/i> is the stack used by this context (see <b>sigaltstack<\/b>(2)), and <i>uc_mcontext<\/i> is the machine-specific representation of the saved context, that includes the calling thread\u2019s machine registers.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The function <b>getcontext<\/b>() initializes the structure pointed to by <i>ucp<\/i> to the currently active context.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The function <b>setcontext<\/b>() restores the user context pointed to by <i>ucp<\/i>. A successful call does not return. The context should have been obtained by a call of <b>getcontext<\/b>(), or <b>makecontext<\/b>(3), or received as the third argument to a signal handler (see the discussion of the <b>SA_SIGINFO<\/b> flag in <b>sigaction<\/b>(2)).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the context was obtained by a call of <b>getcontext<\/b>(), program execution continues as if this call just returned.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the context was obtained by a call of <b>makecontext<\/b>(3), program execution continues by a call to the function <i>func<\/i> specified as the second argument of that call to <b>makecontext<\/b>(3). When the function <i>func<\/i> returns, we continue with the <i>uc_link<\/i> member of the structure <i>ucp<\/i> specified as the first argument of that call to <b>makecontext<\/b>(3). When this member is NULL, the thread exits.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the context was obtained by a call to a signal handler, then old standard text says that &#8220;program execution continues with the program instruction following the instruction interrupted by the signal&#8221;. However, this sentence was removed in SUSv2, and the present verdict is &#8220;the result is unspecified&#8221;.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">When successful, <b>getcontext<\/b>() returns 0 and <b>setcontext<\/b>() does not return. On error, both return \u22121 and set <i>errno<\/i> appropriately.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None defined.<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-1653781.png\" alt=\"Image grohtml-1653781.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">SUSv2, POSIX.1-2001. POSIX.1-2008 removes the specification of <b>getcontext<\/b>(), citing portability issues, and recommending that applications be rewritten to use POSIX threads instead.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The earliest incarnation of this mechanism was the <b>setjmp<\/b>(3)\/<b>longjmp<\/b>(3) mechanism. Since that does not define the handling of the signal context, the next stage was the <b>sigsetjmp<\/b>(3)\/<b>siglongjmp<\/b>(3) pair. The present mechanism gives much more control. On the other hand, there is no easy way to detect whether a return from <b>getcontext<\/b>() is from the first call, or via a <b>setcontext<\/b>() call. The user has to invent their own bookkeeping device, and a register variable won\u2019t do since registers are restored.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <b>longjmp<\/b>(3): it is undefined what would happen with contexts. Use <b>siglongjmp<\/b>(3) or <b>setcontext<\/b>() instead.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>sigaction<\/b>(2), <b>sigaltstack<\/b>(2), <b>sigprocmask<\/b>(2), <b>longjmp<\/b>(3), <b>makecontext<\/b>(3), <b>sigsetjmp<\/b>(3), <b>signal<\/b>(7)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> 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\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  getcontext, setcontext \u2212 get or set the user context <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,3239,3007],"class_list":["post-6875","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-getcontext","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6875"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6875\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}