I have a simple program that use fork() to create a child process, the child process use execvp() function to execute a Unix method like: "ls" or "echo". while the child is working the parent process is waiting using the wait() function. After the child is done the parent check what cause the child to end, using the WIFEXITED() macro. If the child ended not normally the parent print a message on the screen. For debuging purposes I want to make the child terminate not normally, I have tried to run a program that divid by zero but still the line:
if(!WIFEXITED(status))
return true every time.
My code:
int main(){
pid_t p;
int status;
p = fork();
if (p == 0){//child process
execlp("/bin/ls", "ls", NULL);
exit(0);
}else{
wait(&status);
if (!WIFEXITED(status))//if the child process terminated not normally
fprintf(stderr, "\tError Occured!\n");//display error message
}
return 0;}
from the man page for wait():
WIFEXITED(status) returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
What is the right way to make WIFEXITED() return false?