Subj : Re: segmentation fault while using setjmp To : comp.os.linux.development.system,comp.os.linux,comp.os.linux.questions From : Larry I Smith Date : Tue Jun 29 2004 06:18 am naveen wrote: > Hello *, > > Here I'm trying to switch control between three threads in a cycle - > parent, child1, child2 in that order. > But after the first cycle of execution i get a segmentation fault. I > guess the problem is with the stack allocated for each thread. I tried > increasing the stack size and i still get the segmentation fault, but > this time after completing a few more cycles. > Can anybody explain this: > > #include > #include > #include > #include > > sigjmp_buf context[3]; > int crt = 0; > > void parent() > { > while(1) > fprintf(stderr, "Parent process\n"); > } > > void child(int *i) > { > while(1) > fprintf(stderr, "Child process %d\n", *i); > } > > /* create child context and return to parent */ > void create(int i) > { > if (sigsetjmp(context[i], 1)) { > child(&crt); > } else { > char stack[1024]; > siglongjmp(context[0], -1); > } > } > > /* context switch */ > void run_thread() > { > int next = (crt + 1) % 3; > > if (!sigsetjmp(context[crt], 1)) { > crt = next; > siglongjmp(context[crt], -1); > } > } > > void handler(int sig) > { > signal(SIGALRM, handler); > alarm(1); > > run_thread(); > } > > main() > { > signal(SIGALRM, handler); > alarm(1); > > if (!sigsetjmp(context[0], 1)) { > create(1); > } > if (!sigsetjmp(context[0], 1)) { > create(2); > } > parent(); > } Use the glibc posix threads library. The setjmp function family is not intended for thread creation and context switching. Regards, Larry -- Anti-spam address, change each 'X' to '.' to reply directly. .