Search code examples
cunixsystems-programming

Checking status after wait()


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)
    );
}

Solution

  • 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.