It returns a status code via an integer pointer argument, from which you can extract information about how the child process exited.
- For instance, the WEXITSTATUS macro extracts the child process's exit code.
- You can use the WIFEXITED macro to determine from a child process's exit status whether that process exited normally (via the exit function or returning from main) or died from an unhandled signal.
int main ()
{
int child_status;
/* The argument list to pass to the "ls" command. */
char* arg_list[] = {
"ls", /* argv[0], the name of the program. */
"-l",
"/",
NULL /* The argument list must end with a NULL. */
};
/* Spawn a child process running the "ls" command. Ignore the
returned child process ID. */
spawn ("ls", arg_list);
/* Wait for the child process to complete. */
wait (&child_status);
if (WIFEXITED (child_status))
printf ("the child process exited normally, with exit code %d\n",
WEXITSTATUS (child_status));
else
printf ("the child process exited abnormally\n");
return 0;
}