I wrote a program to intentionally let the child process to trigger SIGSEGV. However, I want the child process to terminate abnormally. Right now I use exit() in the signal handler. But exit() call will always generate a normal termination. Which function should I use to achieve that? Another question is how can I let the parent process know that the child process terminates because of SIGSEGV?
extern char etext, edata, end;
void handler() {
pid_t pid;
pid = getpid();
char buf[100];
snprintf(buf, 100, "child %d terminated by signal 11", (int)pid);
psignal(SIGSEGV, buf);
exit(-1);
}
int main(int argc, char **argv) {
if (signal(SIGSEGV, handler) == SIG_ERR) {
perror("signal error");
}
printf("First address beyond:\n");
printf(" program text segment(etext) %10p\n", &etext);
printf(" initialized data segment(edata) %10p\n", &edata);
printf(" uninitialized data segment (end) %10p\n", &end);
if (fork() == 0) {
printf("from Child before\n");
etext = 'a';
printf("from Child after\n");
} else {
pid_t pid;
int status;
if ((pid == waitpid(-1, &status, 0)) >= 0) {
printf("Handler reaped child %d\n", (int)pid);
}
printf("child WIFEXITED = %d, WEXITSTATUS = %d\n", WIFEXITED(status), WEXITSTATUS(status));
if (WEXITSTATUS(status) == -3) {
printf("Capture the exit code\n");
}
}
cc sigsegv.c(after saving it tosigsegv.c), with gcc 4.7.2. Can you please fix the code so that it compiles? - userabort()orkill()if you wantSIGSEGV. - Stargateur