Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You terminate the child (by calling <a href="http://linux.die.net/man/2/_exit" rel="nofollow noreferrer">_exit()</a>) and then the parent can notice this (through e.g. <a href="http://linux.die.net/man/2/waitpid" rel="nofollow noreferrer">waitpid()</a>). For instance, your child could exit with an exit status of -1 to indicate failure to exec. One caveat with this is that it is impossible to tell from your parent whether the child in its original state (i.e. before exec) returned -1 or if it was the newly executed process. </p> <p>As suggested in the comments below, using an "unusual" return code would be appropiate to make it easier to distinguish between your specific error and one from the exec()'ed program. Common ones are 1, 2, 3 etc. while higher numbers 99, 100, etc. are more unusual. You should keep your numbers below 255 (unsigned) or 127 (signed) to increase portability.</p> <p>Since waitpid blocks your application (or rather, the thread calling it) you will either need to put it on a background thread or use the signalling mechanism in POSIX to get information about child process termination. See the SIGCHLD signal and the <a href="http://linux.die.net/man/2/sigaction" rel="nofollow noreferrer">sigaction</a> function to hook up a listener.</p> <p>You could also do some error checking before forking, such as making sure the executable exists. </p> <p>If you use something like <a href="http://library.gnome.org/devel/glib/" rel="nofollow noreferrer">Glib</a>, there are utility functions to do this, and they come with pretty good error reporting. Take a look at the "<a href="http://library.gnome.org/devel/glib/2.22/glib-Spawning-Processes.html" rel="nofollow noreferrer">spawning processes</a>" section of the manual. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload