1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" 2006-08-02, mtk, Added example program
25 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
26 .\" Translated Sat Dec 15 2001 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
27 .\" Updated 2008-12-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.15
29 .TH MAKECONTEXT 3 2009-03-31 "GNU" "Linux Programmer's Manual"
31 makecontext, swapcontext \- ¥æ¡¼¥¶¥³¥ó¥Æ¥¥¹¥È¤òÁàºî¤¹¤ë
33 .B #include <ucontext.h>
35 .BI "void makecontext(ucontext_t *" ucp ", void (*" func )(),
36 .BI "int " argc ", ...);"
38 .BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
40 System V Ū¤Ê´Ä¶¤Ç¤Ï¡¢
41 \fBmcontext_t\fP ¤ª¤è¤Ó \fBucontext_t\fP ¤È¤¤¤¦ 2 ¤Ä¤Î·¿¤È¡¢
48 ¤ÇÄêµÁ¤µ¤ì¤Æ¤ª¤ê¡¢¤¢¤ë¥×¥í¥»¥¹ÆâÉô¤ÇÀ©¸æ²¼¤Ë¤¢¤ëÊ£¿ô¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¡¢
49 ¥æ¡¼¥¶¥ì¥Ù¥ë¤Î¥³¥ó¥Æ¥¥¹¥ÈÀÚÂؤ¨¤¬¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
51 ¤³¤ì¤é¤Î·¿¤È¡¢ºÇ½é¤Î 2 ¤Ä¤Î´Ø¿ô¤Ë¤Ä¤¤¤Æ¤Ï¡¢
56 ´Ø¿ô¤Ï¡¢¥Ý¥¤¥ó¥¿ \fIucp\fP ¤¬»Ø¤¹¥³¥ó¥Æ¥¥¹¥È¤òÊѹ¹¤¹¤ë
59 ¸Æ¤Ó½Ð¤·¤ÇÆÀ¤é¤ì¤¿¤â¤Î¤Ç¤¢¤ë)¡£
61 ¤òµ¯Æ°¤¹¤ëÁ°¤Ë¤Ï¡¢¸Æ¤Ó½Ð¤·¼Ô¤Ï¡¢¤³¤Î¥³¥ó¥Æ¥¥¹¥ÈÍѤË
62 ¿·¤·¤¤¥¹¥¿¥Ã¥¯¤ò³ÎÊݤ·¡¢¤½¤Î¥¢¥É¥ì¥¹¤ò \fIucp\->uc_stack\fP ¤ËÂåÆþ¤·¡¢
63 ¤µ¤é¤Ë¸å·Ñ¤Î¥³¥ó¥Æ¥¥¹¥È¤òÄêµÁ¤·¡¢¤½¤Î¥¢¥É¥ì¥¹¤ò \fIucp\->uc_link\fP ¤Ë
64 ÂåÆþ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
66 ¤³¤Î¥³¥ó¥Æ¥¥¹¥È¤¬¾Íè
71 ͸ú¤Ë¤µ¤ì¤ë¤È¡¢´Ø¿ô \fIfunc\fP ¤¬¸Æ¤Ð¤ì¡¢
76 °ú¤¿ô¤ÎÎó¤¬ÅϤµ¤ì¤ë¡£
79 ¤Ë¤³¤ì¤é¤Î°ú¤¿ô¤Î¸Ä¿ô¤ò»ØÄꤷ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
80 ¤³¤Î´Ø¿ô¤¬Ìá¤ë¤È¡¢¸å·Ñ¤Î¥³¥ó¥Æ¥¥¹¥È¤¬Í¸ú¤Ë¤Ê¤ë¡£
81 ¸å·Ñ¥³¥ó¥Æ¥¥¹¥È¤Î¥Ý¥¤¥ó¥¿¤¬ NULL ¤Î¾ì¹ç¡¢¤½¤Î¥¹¥ì¥Ã¥É¤¬½ªÎ»¤¹¤ë¡£
84 ´Ø¿ô¤Ï¸½ºß¤Î¥³¥ó¥Æ¥¥¹¥È¤ò
85 ¥Ý¥¤¥ó¥¿ \fIoucp\fP ¤¬»Ø¤¹¹½Â¤ÂΤËÊݸ¤·¡¢
86 ¥Ý¥¤¥ó¥¿ \fIucp\fP ¤¬»Ø¤¹¥³¥ó¥Æ¥¥¹¥È¤ò͸ú¤Ë¤¹¤ë¡£
91 (¤·¤«¤·¸å¤Ë \fIoucp\fP ¤¬Í¸ú¤Ë¤Ê¤Ã¤¿¾ì¹ç¤Ë¤ÏÊ֤뤳¤È¤¬¤¢¤ë¡£
94 ¤Ï 0 ¤òÊÖ¤¹¤è¤¦¤Ë¸«¤¨¤ë¡£)
98 \fIerrno\fP ¤ò¥¨¥é¡¼¤Ë±þ¤¸¤ÆÀßÄꤹ¤ë¡£
102 ¥¹¥¿¥Ã¥¯¤Ë³ä¤êÅö¤Æ¤ë¶õ´Ö¤¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£
107 ¤Ï¡¢¥Ð¡¼¥¸¥ç¥ó 2.1 °Ê¹ß¤Î glibc ¤ÇÄ󶡤µ¤ì¤Æ¤¤¤ë¡£
110 POSIX.1-2008 ¤Ç¤Ï¡¢°Ü¿¢À¤ÎÌäÂ꤫¤é
114 ¤Î»ÅÍͤ¬ºï½ü¤µ¤ì¤Æ¤¤¤ë¡£
115 Âå¤ï¤ê¤Ë¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò POSIX ¥¹¥ì¥Ã¥É¤ò»È¤Ã¤Æ½ñ¤Ä¾¤¹¤³¤È¤¬
118 \fIucp\->uc_stack\fP ¤Î²ò¼á¤Ï
121 ¤¹¤Ê¤ï¤Á¤³¤Î¹½Â¤ÂΤˤϡ¢
122 ¥¹¥¿¥Ã¥¯¤È¤·¤ÆÍѤ¤¤é¤ì¤ë¥á¥â¥êÎΰè¤Î³«»Ï¥¢¥É¥ì¥¹¤ÈŤµ¤¬´Þ¤Þ¤ì¡¢
123 ¤³¤ì¤Ï¥¹¥¿¥Ã¥¯¤¬¿¤Ó¤ëÊý¸þ¤¬¤É¤Á¤é¤Ç¤¢¤ë¤«¤Ë¤Ï´Ø·¸¤·¤Ê¤¤¡£
124 ¤·¤¿¤¬¤Ã¤Æ¡¢¥æ¡¼¥¶¥×¥í¥°¥é¥à¤Ï¤³¤Î·ï¤Ë¤Ä¤¤¤Æ¤Ï¿´ÇÛ¤·¤Ê¤¯¤Æ¤è¤¤¡£
127 ¤È¥Ý¥¤¥ó¥¿·¿¤¬Æ±¤¸Â礤µ¤Ç¤¢¤ë¥¢¡¼¥¥Æ¥¯¥Á¥ã¤Ç¤Ï
128 (x86-32 ¤Ï¤½¤ÎÎã¤Ç¤¢¤ê¡¢Î¾Êý¤Î·¿¤È¤â 32 ¥Ó¥Ã¥È¤Ç¤¢¤ë)¡¢
132 °Ê¹ß¤Î°ú¤¿ô¤È¤·¤Æ¥Ý¥¤¥ó¥¿¤òÅϤ·¤Æ¤â¤¦¤Þ¤¯Æ°¤¯¤«¤â¤·¤ì¤Ê¤¤¡£
133 ¤·¤«¤·¤Ê¤¬¤é¡¢¤³¤Î¤è¤¦¤Ë¤¹¤ë¤È¡¢°Ü¿¢À¤ÏÊݾڤµ¤ì¤º¡¢
134 ɸ½à¤Ë½¾¤¨¤ÐÆ°ºî¤Ï̤ÄêµÁ¤Ç¤¢¤ê¡¢¥Ý¥¤¥ó¥¿¤¬
136 ¤è¤ê¤âÂ礤¤¥¢¡¼¥¥Æ¥¯¥Á¥ã¤Ç¤ÏÀµ¤·¤¯Æ°ºî¤·¤Ê¤¤¤³¤È¤À¤í¤¦¡£
137 ¤½¤ì¤Ë¤â´Ø¤ï¤é¤º¡¢¥Ð¡¼¥¸¥ç¥ó 2.8 °Ê¹ß¤Î glibc ¤Ç¤Ï¡¢
139 ¤ËÊѹ¹¤¬¹Ô¤ï¤ì¡¢(x86-64 ¤Ê¤É¤Î) ¤¤¤¯¤Ä¤«¤Î 64 ¥Ó¥Ã¥È¥¢¡¼¥¥Æ¥¯¥Á¥ã¤Ç
140 °ú¤¿ô¤È¤·¤Æ¥Ý¥¤¥ó¥¿¤òÅϤ¹¤³¤È¤¬¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
143 °Ê²¼¤Î¥µ¥ó¥×¥ë¡¦¥×¥í¥°¥é¥à¤Ï¡¢
147 ¤Î»ÈÍÑÊýË¡¤ÎÎã¤ò¼¨¤¹¤â¤Î¤Ç¤¢¤ë¡£
148 ¤³¤Î¥×¥í¥°¥é¥à¤ò¼Â¹Ô¤¹¤ë¤È¡¢°Ê²¼¤Î¤è¤¦¤Ê½ÐÎϤ¬ÆÀ¤é¤ì¤ë:
153 main: swapcontext(&uctx_main, &uctx_func2)
155 func2: swapcontext(&uctx_func2, &uctx_func1)
157 func1: swapcontext(&uctx_func1, &uctx_func2)
163 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
166 #include <ucontext.h>
170 static ucontext_t uctx_main, uctx_func1, uctx_func2;
172 #define handle_error(msg) \\
173 do { perror(msg); exit(EXIT_FAILURE); } while (0)
178 printf("func1: started\\n");
179 printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
180 if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
181 handle_error("swapcontext");
182 printf("func1: returning\\n");
188 printf("func2: started\\n");
189 printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
190 if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
191 handle_error("swapcontext");
192 printf("func2: returning\\n");
196 main(int argc, char *argv[])
198 char func1_stack[16384];
199 char func2_stack[16384];
201 if (getcontext(&uctx_func1) == \-1)
202 handle_error("getcontext");
203 uctx_func1.uc_stack.ss_sp = func1_stack;
204 uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
205 uctx_func1.uc_link = &uctx_main;
206 makecontext(&uctx_func1, func1, 0);
208 if (getcontext(&uctx_func2) == \-1)
209 handle_error("getcontext");
210 uctx_func2.uc_stack.ss_sp = func2_stack;
211 uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
212 /* Successor context is f1(), unless argc > 1 */
213 uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
214 makecontext(&uctx_func2, func2, 0);
216 printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
217 if (swapcontext(&uctx_main, &uctx_func2) == \-1)
218 handle_error("swapcontext");
220 printf("main: exiting\\n");