I'm testing code that is designed to detect when a child process has segfaulted. Imagine my surprised when this code does not always segfault:
#include <stdio.h>
int main() {
char *p = (char *)(unsigned long)0;
putchar(*p);
return 0;
}
I'm running under a Debian Linux 2.6.26 kernel; my shell is the AT&T ksh93
from the Debian ksh
package, Version M 93s+ 2008-01-31. Sometimes this program segfault but otherwise it simply terminates silently with a nonzero exit status but no message. My signal-detecting program reports the following:
segfault terminated by signal 11: Segmentation fault
segfault terminated by signal 53: Real-time signal 19
segfault terminated by signal 11: Segmentation fault
segfault terminated by signal 53: Real-time signal 19
segfault terminated by signal 53: Real-time signal 19
segfault terminated by signal 53: Real-time signal 19
segfault terminated by signal 53: Real-time signal 19
Running under pure ksh
shows that the segfault is also rare:
Running...
Running...
Running...
Running...
Running...
Running... Memory fault
Running...
Interestingly, bash
correctly detects the segfault every time.
I have two questions:
Can anyone explain this behavior?
Can anyone suggest a simple C program that will segfault reliably on every execution? I have also tried
kill(getpid(), SIGSEGV)
, but I get similar results.
EDIT: jbcreix has the answer: my segfault detector was broken. I was fooled because ksh
has the same problem. I tried with bash
and bash
gets it right every time.
My error was that I was passing WNOHANG
to waitpid()
, where I should have been passing zero. I don't know what I could have been thinking! One wonders what is the matter with ksh
, but that's a separate question.