OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / draft / man3 / makecontext.3
1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
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
24 .\"
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
28 .\"
29 .TH MAKECONTEXT 3 2009-03-31 "GNU" "Linux Programmer's Manual"
30 .\"O .SH NAME
31 .\"O makecontext, swapcontext \- manipulate user context
32 .SH Ì¾Á°
33 makecontext, swapcontext \- ¥æ¡¼¥¶¥³¥ó¥Æ¥­¥¹¥È¤òÁàºî¤¹¤ë
34 .\"O .SH SYNOPSIS
35 .SH ½ñ¼°
36 .B #include <ucontext.h>
37 .sp
38 .BI "void makecontext(ucontext_t *" ucp ", void (*" func )(),
39 .BI "int " argc ", ...);"
40 .sp
41 .BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
42 .\"O .SH DESCRIPTION
43 .SH ÀâÌÀ
44 .\"O In a System V-like environment, one has the type \fIucontext_t\fP defined in
45 .\"O .I <ucontext.h>
46 .\"O and the four functions
47 .\"O .BR getcontext (2),
48 .\"O .BR setcontext (2),
49 .\"O .BR makecontext ()
50 .\"O and
51 .\"O .BR swapcontext ()
52 .\"O that allow user-level context switching
53 .\"O between multiple threads of control within a process.
54 System V Åª¤Ê´Ä¶­¤Ç¤Ï¡¢
55 \fBmcontext_t\fP ¤ª¤è¤Ó \fBucontext_t\fP ¤È¤¤¤¦ 2 ¤Ä¤Î·¿¤È¡¢
56 .BR getcontext (2),
57 .BR setcontext (2),
58 .BR makecontext (),
59 .BR swapcontext ()
60 ¤È¤¤¤¦ 4 ¤Ä¤Î´Ø¿ô¤¬
61 .I <ucontext.h>
62 ¤ÇÄêµÁ¤µ¤ì¤Æ¤ª¤ê¡¢¤¢¤ë¥×¥í¥»¥¹ÆâÉô¤ÇÀ©¸æ²¼¤Ë¤¢¤ëÊ£¿ô¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¡¢
63 ¥æ¡¼¥¶¥ì¥Ù¥ë¤Î¥³¥ó¥Æ¥­¥¹¥ÈÀÚÂؤ¨¤¬¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
64 .LP
65 .\"O For the type and the first two functions, see
66 .\"O .BR getcontext (2).
67 ¤³¤ì¤é¤Î·¿¤È¡¢ºÇ½é¤Î 2 ¤Ä¤Î´Ø¿ô¤Ë¤Ä¤¤¤Æ¤Ï¡¢
68 .BR getcontext (2)
69 ¤ò»²¾È¤Î¤³¤È¡£
70 .LP
71 .\"O The
72 .\"O .BR makecontext ()
73 .\"O function modifies the context pointed to
74 .\"O by \fIucp\fP (which was obtained from a call to
75 .\"O .BR getcontext (2)).
76 .\"O Before invoking
77 .\"O .BR makecontext (),
78 .\"O the caller must allocate a new stack
79 .\"O for this context and assign its address to \fIucp\->uc_stack\fP,
80 .\"O and define a successor context and
81 .\"O assign its address to \fIucp\->uc_link\fP.
82 .BR makecontext ()
83 ´Ø¿ô¤Ï¡¢¥Ý¥¤¥ó¥¿ \fIucp\fP ¤¬»Ø¤¹¥³¥ó¥Æ¥­¥¹¥È¤òÊѹ¹¤¹¤ë
84 (\fIucp\fP ¤Ï°ÊÁ°¤Î
85 .BR getcontext (2)
86 ¸Æ¤Ó½Ð¤·¤ÇÆÀ¤é¤ì¤¿¤â¤Î¤Ç¤¢¤ë)¡£
87 .BR makecontext ()
88 ¤òµ¯Æ°¤¹¤ëÁ°¤Ë¤Ï¡¢¸Æ¤Ó½Ð¤·¼Ô¤Ï¡¢¤³¤Î¥³¥ó¥Æ¥­¥¹¥ÈÍѤË
89 ¿·¤·¤¤¥¹¥¿¥Ã¥¯¤ò³ÎÊݤ·¡¢¤½¤Î¥¢¥É¥ì¥¹¤ò \fIucp\->uc_stack\fP ¤ËÂåÆþ¤·¡¢
90 ¤µ¤é¤Ë¸å·Ñ¤Î¥³¥ó¥Æ¥­¥¹¥È¤òÄêµÁ¤·¡¢¤½¤Î¥¢¥É¥ì¥¹¤ò \fIucp\->uc_link\fP ¤Ë
91 ÂåÆþ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
92
93 .\"O When this context is later activated (using
94 .\"O .BR setcontext (2)
95 .\"O or
96 .\"O .BR swapcontext ())
97 .\"O the function \fIfunc\fP is called,
98 .\"O and passed the series of integer
99 .\"O .RI ( int )
100 .\"O arguments that follow
101 .\"O .IR argc ;
102 .\"O the caller must specify the number of these arguments in
103 .\"O .IR argc .
104 .\"O When this function returns, the successor context is activated.
105 .\"O If the successor context pointer is NULL, the thread exits.
106 ¤³¤Î¥³¥ó¥Æ¥­¥¹¥È¤¬¾­Íè
107 .RB ( setcontext (2)
108 ¤Þ¤¿¤Ï
109 .BR swapcontext ()
110 ¤Ë¤è¤Ã¤Æ)
111 Í­¸ú¤Ë¤µ¤ì¤ë¤È¡¢´Ø¿ô \fIfunc\fP ¤¬¸Æ¤Ð¤ì¡¢
112 °ú¤­¿ô¤È¤·¤Æ
113 .I argc
114 °Ê¹ß¤ÎÀ°¿ô
115 .RI ( int )
116 °ú¤­¿ô¤ÎÎó¤¬ÅϤµ¤ì¤ë¡£
117 ¸Æ¤Ó½Ð¤·¼Ô¤Ï
118 .I argc
119 ¤Ë¤³¤ì¤é¤Î°ú¤­¿ô¤Î¸Ä¿ô¤ò»ØÄꤷ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
120 ¤³¤Î´Ø¿ô¤¬Ìá¤ë¤È¡¢¸å·Ñ¤Î¥³¥ó¥Æ¥­¥¹¥È¤¬Í­¸ú¤Ë¤Ê¤ë¡£
121 ¸å·Ñ¥³¥ó¥Æ¥­¥¹¥È¤Î¥Ý¥¤¥ó¥¿¤¬ NULL ¤Î¾ì¹ç¡¢¤½¤Î¥¹¥ì¥Ã¥É¤¬½ªÎ»¤¹¤ë¡£
122 .LP
123 .\"O The
124 .\"O .BR swapcontext ()
125 .\"O function saves the current context in
126 .\"O the structure pointed to by \fIoucp\fP, and then activates the
127 .\"O context pointed to by \fIucp\fP.
128 .BR swapcontext ()
129 ´Ø¿ô¤Ï¸½ºß¤Î¥³¥ó¥Æ¥­¥¹¥È¤ò
130 ¥Ý¥¤¥ó¥¿ \fIoucp\fP ¤¬»Ø¤¹¹½Â¤ÂΤËÊݸ¤·¡¢
131 ¥Ý¥¤¥ó¥¿ \fIucp\fP ¤¬»Ø¤¹¥³¥ó¥Æ¥­¥¹¥È¤òÍ­¸ú¤Ë¤¹¤ë¡£
132 .\"O .SH "RETURN VALUE"
133 .SH ÊÖ¤êÃÍ
134 .\"O When successful,
135 .\"O .BR swapcontext ()
136 .\"O does not return.
137 .\"O (But we may return later, in case \fIoucp\fP is
138 .\"O activated, in which case it looks like
139 .\"O .BR swapcontext ()
140 .\"O returns 0.)
141 .\"O On error,
142 .\"O .BR swapcontext ()
143 .\"O returns \-1 and
144 .\"O sets \fIerrno\fP appropriately.
145 À®¸ù¤¹¤ë¤È¡¢
146 .BR swapcontext ()
147 ¤ÏÊÖ¤é¤Ê¤¤
148 (¤·¤«¤·¸å¤Ë \fIoucp\fP ¤¬Í­¸ú¤Ë¤Ê¤Ã¤¿¾ì¹ç¤Ë¤ÏÊ֤뤳¤È¤¬¤¢¤ë¡£
149 ¤³¤Î¤È¤­¤Ë¤Ï
150 .BR swapcontext ()
151 ¤Ï 0 ¤òÊÖ¤¹¤è¤¦¤Ë¸«¤¨¤ë¡£)
152 ¼ºÇÔ¤¹¤ë¤È¡¢
153 .BR swapcontext ()
154 ¤Ï \-1 ¤òÊÖ¤·¡¢
155 \fIerrno\fP ¤ò¥¨¥é¡¼¤Ë±þ¤¸¤ÆÀßÄꤹ¤ë¡£
156 .\"O .SH ERRORS
157 .SH ¥¨¥é¡¼
158 .TP
159 .B ENOMEM
160 .\"O Insufficient stack space left.
161 ¥¹¥¿¥Ã¥¯¤Ë³ä¤êÅö¤Æ¤ë¶õ´Ö¤¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£
162 .\"O .SH VERSIONS
163 .SH ¥Ð¡¼¥¸¥ç¥ó
164 .\"O .BR makecontext ()
165 .\"O and
166 .\"O .BR swapcontext ()
167 .\"O are provided in glibc since version 2.1.
168 .BR makecontext ()
169 ¤È
170 .BR swapcontext ()
171 ¤Ï¡¢¥Ð¡¼¥¸¥ç¥ó 2.1 °Ê¹ß¤Î glibc ¤ÇÄ󶡤µ¤ì¤Æ¤¤¤ë¡£
172 .\"O .SH "CONFORMING TO"
173 .SH ½àµò
174 SUSv2, POSIX.1-2001.
175 .\"O POSIX.1-2008 removes the specifications of
176 .\"O .BR makecontext ()
177 .\"O and
178 .\"O .BR swapcontext (),
179 .\"O citing portability issues, and
180 .\"O recommending that applications be rewritten to use POSIX threads instead.
181 POSIX.1-2008 ¤Ç¤Ï¡¢°Ü¿¢À­¤ÎÌäÂ꤫¤é
182 .BR makecontext ()
183 ¤È
184 .BR swapcontext ()
185 ¤Î»ÅÍͤ¬ºï½ü¤µ¤ì¤Æ¤¤¤ë¡£
186 Âå¤ï¤ê¤Ë¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò POSIX ¥¹¥ì¥Ã¥É¤ò»È¤Ã¤Æ½ñ¤­Ä¾¤¹¤³¤È¤¬
187 ¿ä¾©¤µ¤ì¤Æ¤¤¤ë¡£
188 .\"O .SH NOTES
189 .SH Ãí°Õ
190 .\"O The interpretation of \fIucp\->uc_stack\fP is just as in
191 .\"O .BR sigaltstack (2),
192 .\"O namely, this struct contains the start and length of a memory area
193 .\"O to be used as the stack, regardless of the direction of growth of
194 .\"O the stack.
195 .\"O Thus, it is not necessary for the user program to
196 .\"O worry about this direction.
197 \fIucp\->uc_stack\fP ¤Î²ò¼á¤Ï
198 .BR sigaltstack (2)
199 ¤Î¾ì¹ç¤ÈƱ¤¸¤Ç¤¢¤ë¡£
200 ¤¹¤Ê¤ï¤Á¤³¤Î¹½Â¤ÂΤˤϡ¢
201 ¥¹¥¿¥Ã¥¯¤È¤·¤ÆÍѤ¤¤é¤ì¤ë¥á¥â¥êÎΰè¤Î³«»Ï¥¢¥É¥ì¥¹¤ÈŤµ¤¬´Þ¤Þ¤ì¡¢
202 ¤³¤ì¤Ï¥¹¥¿¥Ã¥¯¤¬¿­¤Ó¤ëÊý¸þ¤¬¤É¤Á¤é¤Ç¤¢¤ë¤«¤Ë¤Ï´Ø·¸¤·¤Ê¤¤¡£
203 ¤·¤¿¤¬¤Ã¤Æ¡¢¥æ¡¼¥¶¥×¥í¥°¥é¥à¤Ï¤³¤Î·ï¤Ë¤Ä¤¤¤Æ¤Ï¿´ÇÛ¤·¤Ê¤¯¤Æ¤è¤¤¡£
204
205 .\"O On architectures where
206 .\"O .I int
207 .\"O and pointer types are the same size
208 .\"O (e.g., x86-32, where both types are 32 bits),
209 .\"O you may be able to get away with passing pointers as arguments to
210 .\"O .BR makecontext ()
211 .\"O following
212 .\"O .IR argc .
213 .\"O However, doing this is not guaranteed to be portable,
214 .\"O is undefined according to the standards,
215 .\"O and won't work on architectures where pointers are larger than
216 .\"O .IR int s.
217 .\"O Nevertheless, starting with version 2.8, glibc makes some changes to
218 .\"O .BR makecontext (3),
219 .\"O to permit this on some 64-bit architectures (e.g., x86-64).
220 .I int
221 ¤È¥Ý¥¤¥ó¥¿·¿¤¬Æ±¤¸Â礭¤µ¤Ç¤¢¤ë¥¢¡¼¥­¥Æ¥¯¥Á¥ã¤Ç¤Ï
222 (x86-32 ¤Ï¤½¤ÎÎã¤Ç¤¢¤ê¡¢Î¾Êý¤Î·¿¤È¤â 32 ¥Ó¥Ã¥È¤Ç¤¢¤ë)¡¢
223 .BR makecontext ()
224 ¤Î
225 .I argc
226 °Ê¹ß¤Î°ú¤­¿ô¤È¤·¤Æ¥Ý¥¤¥ó¥¿¤òÅϤ·¤Æ¤â¤¦¤Þ¤¯Æ°¤¯¤«¤â¤·¤ì¤Ê¤¤¡£
227 ¤·¤«¤·¤Ê¤¬¤é¡¢¤³¤Î¤è¤¦¤Ë¤¹¤ë¤È¡¢°Ü¿¢À­¤ÏÊݾڤµ¤ì¤º¡¢
228 ɸ½à¤Ë½¾¤¨¤ÐÆ°ºî¤Ï̤ÄêµÁ¤Ç¤¢¤ê¡¢¥Ý¥¤¥ó¥¿¤¬
229 .I int
230 ¤è¤ê¤âÂ礭¤¤¥¢¡¼¥­¥Æ¥¯¥Á¥ã¤Ç¤ÏÀµ¤·¤¯Æ°ºî¤·¤Ê¤¤¤³¤È¤À¤í¤¦¡£
231 ¤½¤ì¤Ë¤â´Ø¤ï¤é¤º¡¢¥Ð¡¼¥¸¥ç¥ó 2.8 °Ê¹ß¤Î glibc ¤Ç¤Ï¡¢
232 .BR makecontext (3)
233 ¤ËÊѹ¹¤¬¹Ô¤ï¤ì¡¢(x86-64 ¤Ê¤É¤Î) ¤¤¤¯¤Ä¤«¤Î 64 ¥Ó¥Ã¥È¥¢¡¼¥­¥Æ¥¯¥Á¥ã¤Ç
234 °ú¤­¿ô¤È¤·¤Æ¥Ý¥¤¥ó¥¿¤òÅϤ¹¤³¤È¤¬¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
235 .\"O .SH EXAMPLE
236 .SH Îã
237 .PP
238 .\"O The example program below demonstrates the use of
239 .\"O .BR getcontext (2),
240 .\"O .BR makecontext (),
241 .\"O and
242 .\"O .BR swapcontext ().
243 .\"O Running the program produces the following output:
244 °Ê²¼¤Î¥µ¥ó¥×¥ë¡¦¥×¥í¥°¥é¥à¤Ï¡¢
245 .BR getcontext (2),
246 .BR makecontext (),
247 .BR swapcontext ()
248 ¤Î»ÈÍÑÊýË¡¤ÎÎã¤ò¼¨¤¹¤â¤Î¤Ç¤¢¤ë¡£
249 ¤³¤Î¥×¥í¥°¥é¥à¤ò¼Â¹Ô¤¹¤ë¤È¡¢°Ê²¼¤Î¤è¤¦¤Ê½ÐÎϤ¬ÆÀ¤é¤ì¤ë:
250 .in +4n
251 .nf
252
253 .RB "$" " ./a.out"
254 main: swapcontext(&uctx_main, &uctx_func2)
255 func2: started
256 func2: swapcontext(&uctx_func2, &uctx_func1)
257 func1: started
258 func1: swapcontext(&uctx_func1, &uctx_func2)
259 func2: returning
260 func1: returning
261 main: exiting
262 .fi
263 .in
264 .\"O .SS Program source
265 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
266 \&
267 .nf
268 #include <ucontext.h>
269 #include <stdio.h>
270 #include <stdlib.h>
271
272 static ucontext_t uctx_main, uctx_func1, uctx_func2;
273
274 #define handle_error(msg) \\
275     do { perror(msg); exit(EXIT_FAILURE); } while (0)
276
277 static void
278 func1(void)
279 {
280     printf("func1: started\\n");
281     printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
282     if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
283         handle_error("swapcontext");
284     printf("func1: returning\\n");
285 }
286
287 static void
288 func2(void)
289 {
290     printf("func2: started\\n");
291     printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
292     if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
293         handle_error("swapcontext");
294     printf("func2: returning\\n");
295 }
296
297 int
298 main(int argc, char *argv[])
299 {
300     char func1_stack[16384];
301     char func2_stack[16384];
302
303     if (getcontext(&uctx_func1) == \-1)
304         handle_error("getcontext");
305     uctx_func1.uc_stack.ss_sp = func1_stack;
306     uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
307     uctx_func1.uc_link = &uctx_main;
308     makecontext(&uctx_func1, func1, 0);
309
310     if (getcontext(&uctx_func2) == \-1)
311         handle_error("getcontext");
312     uctx_func2.uc_stack.ss_sp = func2_stack;
313     uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
314     /* Successor context is f1(), unless argc > 1 */
315     uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
316     makecontext(&uctx_func2, func2, 0);
317
318     printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
319     if (swapcontext(&uctx_main, &uctx_func2) == \-1)
320         handle_error("swapcontext");
321
322     printf("main: exiting\\n");
323     exit(EXIT_SUCCESS);
324 }
325 .fi
326 .\"O .SH "SEE ALSO"
327 .SH ´ØÏ¢¹àÌÜ
328 .BR getcontext (2),
329 .BR sigaction (2),
330 .BR sigaltstack (2),
331 .BR sigprocmask (2),
332 .BR sigsetjmp (3)