From mboxrd@z Thu Jan 1 00:00:00 1970 From: kkonaka@mac.com Subject: makecontext(3) Date: Mon, 28 Jul 2003 15:06:30 -0400 Sender: linux-c-programming-owner@vger.kernel.org Message-ID: Mime-Version: 1.0 (generated by SEMI 1.14.5 - "Awara-Onsen") Return-path: List-Id: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: linux-c-programming@vger.kernel.org on the surface(?) calls to makecontext() appear to have to be preceded by getcontext() to completely initialize its arg: ucontext_t struct (that is, if I comment out getcontext() calls from a program like the below, it causes SEGV). why is it the case? kenji --- #include #include #include static ucontext_t u0; static ucontext_t u1; static ucontext_t u2; #define STACK_SIZE (1024 * 8) #define NLOOP 7 static int i; static int j; static void f() { for (i = 0; i < NLOOP; i++) { printf("f(): i = %d\n", i); if (swapcontext(&u1, &u2) < 0) err(1, NULL); } } static void g() { for (j = 0; j < NLOOP; j++) { printf("g(): j = %d\n", j); if (swapcontext(&u2, &u0) < 0) err(1, NULL); } } main() { static unsigned char _u1_stack[STACK_SIZE]; static unsigned char _u2_stack[STACK_SIZE]; getcontext(&u1); u1.uc_stack.ss_sp = _u1_stack; u1.uc_stack.ss_size = sizeof _u1_stack; u1.uc_stack.ss_flags = 0; u1.uc_link = NULL; makecontext(&u1, f, 0); getcontext(&u2); u2.uc_stack.ss_sp = _u2_stack; u2.uc_stack.ss_size = sizeof _u2_stack; u2.uc_stack.ss_flags = 0; u2.uc_link = NULL; makecontext(&u2, g, 0); while (1) { if (swapcontext(&u0, &u1) < 0) err(1, NULL); printf("main: %d %d\n", i, j); if (i == NLOOP && j == NLOOP) exit(0); } } --