views:

36

answers:

3

After creating a child process and exiting it immediately (_exit()), I want to perform a wait and check the status. Now I wonder if in the 'else' branch of the if/else construct I also need to check for WIFSIGNALED. As far as I understand, if I perform a wait, a) an error could have occured (-1), the child could have terminated normally by an (exit() or _exit()), or it could have been terminated by a signal, so the check could be omitted, right?

//remainder omitted

int status;

pid_t t_pid = wait(&status);

if (t_pid == -1) {
    perror("wait");
    exit(EXIT_FAILURE);
}

if (WIFEXITED(status)) {
    printf("child terminated normally, status = %d\n",
           WEXITSTATUS(status)
    );
} else { // <-- do it have to check for WIFSIGNALED() here?
    printf("child was terminated by a signal, signum = %d\n",
           WTERMSIG(status)
    );
}
+1  A: 

I don't know.

But you could make your child die "abnormally". kill(getpid()) in the child?

http://publib.boulder.ibm.com/infocenter/tpfhelp/current/index.jsp?topic=/com.ibm.ztpf-ztpfdf.doc_put.cur/gtpc2/cpp_wifsignaled.html

From the sound of the words in the docs I'd say you are doing it correctly.

Ronny
+1 Nice link by the way ^^
Helper Method
+1  A: 

Yes, you are correct - if wait succeeded then either WIFEXITED() or WIFSIGNALED() will be true.

caf
+1  A: 

Yes. POSIX.1 states:

If the information pointed to by stat_loc was stored by a call to waitpid() that did not specify the WUNTRACED or WCONTINUED flags, or by a call to the wait() function, exactly one of the macros WIFEXITED(*stat_loc) and WIFSIGNALED(*stat_loc) shall evaluate to a non-zero value.

Using waitpid() with WUNTRACED or WCONTINUED, it is possible to get a status where WIFSTOPPED(*stat_loc) or WIFCONTINUED(*stat_loc) is true instead.

mark4o
+1 Very nice explanation, thx
Helper Method