©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c9/ctx.c

Go to the documentation of this file.
00001 /*
00002     swapcontext example
00003     AUP2, Sec. 9.05.1 (not in book)
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include <stdio.h>
00022 
00023 #if _POSIX_REALTIME_SIGNALS > 0
00024 
00025 #include <ucontext.h>
00026 
00027 static ucontext_t ctx[3];
00028 
00029 static void
00030 f1 (void)
00031 {
00032     puts("start f1");
00033     swapcontext(&ctx[1], &ctx[2]);
00034     puts("finish f1");
00035 }
00036 
00037 static void
00038 f2 (void)
00039 {
00040     puts("start f2");
00041     swapcontext(&ctx[2], &ctx[1]);
00042     puts("finish f2");
00043 }
00044 
00045 int
00046 main (void)
00047 {
00048     char st1[8192];
00049     char st2[8192];
00050 
00051     getcontext(&ctx[1]);
00052     ctx[1].uc_stack.ss_sp = st1;
00053     ctx[1].uc_stack.ss_size = sizeof st1;
00054     ctx[1].uc_link = &ctx[0];
00055     makecontext(&ctx[1], f1, 0);
00056 
00057     getcontext(&ctx[2]);
00058     ctx[2].uc_stack.ss_sp = st2;
00059     ctx[2].uc_stack.ss_size = sizeof st2;
00060     ctx[2].uc_link = &ctx[1];
00061     makecontext(&ctx[2], f2, 0);
00062 
00063     swapcontext(&ctx[0], &ctx[2]);
00064     return 0;
00065 }
00066 
00067 #else /* _POSIX_REALTIME_SIGNALS */
00068 
00069 int main(void)
00070 {
00071     printf("Not supported.\n");
00072     exit(1);
00073 }
00074 
00075 #endif /* _POSIX_REALTIME_SIGNALS */

Generated on Fri Apr 23 10:57:02 2004 for AUP2 Example Source by doxygen 1.3.1