segmentation fault while using setjmp
From: naveen (joga2052_at_yahoo.com)
Date: 06/29/04
- Next message: Kasper Dupont: "Re: segmentation fault while using setjmp"
- Previous message: TCMa: "Re: cannot modify files on DOS partition"
- Next in thread: Kasper Dupont: "Re: segmentation fault while using setjmp"
- Reply: Kasper Dupont: "Re: segmentation fault while using setjmp"
- Reply: Larry I Smith: "Re: segmentation fault while using setjmp"
- Reply: mjt: "Re: segmentation fault while using setjmp"
- Reply: Iwo Mergler: "Re: segmentation fault while using setjmp"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Jun 2004 21:14:54 -0700
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 <stdio.h>
#include <setjmp.h>
#include <unistd.h>
#include <signal.h>
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();
}
- Next message: Kasper Dupont: "Re: segmentation fault while using setjmp"
- Previous message: TCMa: "Re: cannot modify files on DOS partition"
- Next in thread: Kasper Dupont: "Re: segmentation fault while using setjmp"
- Reply: Kasper Dupont: "Re: segmentation fault while using setjmp"
- Reply: Larry I Smith: "Re: segmentation fault while using setjmp"
- Reply: mjt: "Re: segmentation fault while using setjmp"
- Reply: Iwo Mergler: "Re: segmentation fault while using setjmp"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|