1MAKECONTEXT(3P) POSIX Programmer's Manual MAKECONTEXT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 makecontext, swapcontext - manipulate user contexts
13
15 #include <ucontext.h>
16
17 void makecontext(ucontext_t *ucp, void (*func)(void),
18 int argc, ...);
19 int swapcontext(ucontext_t *restrict oucp,
20 const ucontext_t *restrict ucp);
21
22
24 The makecontext() function shall modify the context specified by ucp,
25 which has been initialized using getcontext(). When this context is
26 resumed using swapcontext() or setcontext(), program execution shall
27 continue by calling func, passing it the arguments that follow argc in
28 the makecontext() call.
29
30 Before a call is made to makecontext(), the application shall ensure
31 that the context being modified has a stack allocated for it. The
32 application shall ensure that the value of argc matches the number of
33 arguments of type int passed to func; otherwise, the behavior is unde‐
34 fined.
35
36 The uc_link member is used to determine the context that shall be
37 resumed when the context being modified by makecontext() returns. The
38 application shall ensure that the uc_link member is initialized prior
39 to the call to makecontext().
40
41 The swapcontext() function shall save the current context in the con‐
42 text structure pointed to by oucp and shall set the context to the con‐
43 text structure pointed to by ucp.
44
46 Upon successful completion, swapcontext() shall return 0. Otherwise, -1
47 shall be returned and errno set to indicate the error.
48
50 The swapcontext() function shall fail if:
51
52 ENOMEM The ucp argument does not have enough stack left to complete the
53 operation.
54
55
56 The following sections are informative.
57
59 The following example illustrates the use of makecontext():
60
61
62 #include <stdio.h>
63 #include <ucontext.h>
64
65
66 static ucontext_t ctx[3];
67
68
69 static void
70 f1 (void)
71 {
72 puts("start f1");
73 swapcontext(&ctx[1], &ctx[2]);
74 puts("finish f1");
75 }
76
77
78 static void
79 f2 (void)
80 {
81 puts("start f2");
82 swapcontext(&ctx[2], &ctx[1]);
83 puts("finish f2");
84 }
85
86
87 int
88 main (void)
89 {
90 char st1[8192];
91 char st2[8192];
92
93
94 getcontext(&ctx[1]);
95 ctx[1].uc_stack.ss_sp = st1;
96 ctx[1].uc_stack.ss_size = sizeof st1;
97 ctx[1].uc_link = &ctx[0];
98 makecontext(&ctx[1], f1, 0);
99
100
101 getcontext(&ctx[2]);
102 ctx[2].uc_stack.ss_sp = st2;
103 ctx[2].uc_stack.ss_size = sizeof st2;
104 ctx[2].uc_link = &ctx[1];
105 makecontext(&ctx[2], f2, 0);
106
107
108 swapcontext(&ctx[0], &ctx[2]);
109 return 0;
110 }
111
113 None.
114
116 None.
117
119 None.
120
122 exit(), getcontext(), sigaction(), sigprocmask(), the Base Definitions
123 volume of IEEE Std 1003.1-2001, <ucontext.h>
124
126 Portions of this text are reprinted and reproduced in electronic form
127 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
128 -- Portable Operating System Interface (POSIX), The Open Group Base
129 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
130 Electrical and Electronics Engineers, Inc and The Open Group. In the
131 event of any discrepancy between this version and the original IEEE and
132 The Open Group Standard, the original IEEE and The Open Group Standard
133 is the referee document. The original Standard can be obtained online
134 at http://www.opengroup.org/unix/online.html .
135
136
137
138IEEE/The Open Group 2003 MAKECONTEXT(3P)