00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00068
00069 int main(void)
00070 {
00071 printf("Not supported.\n");
00072 exit(1);
00073 }
00074
00075 #endif