<story><title>Fork() can fail: this is important (2014)</title><url>https://rachelbythebay.com/w/2014/08/19/fork/</url></story><parent_chain></parent_chain><comment><author>bluejekyll</author><text>This is why separating return values from error codes is important.&lt;p&gt;For example, in Rust, you’d never get into this situation, because a decent fork ffi function would immediately convert -1 into a Result carrying an error, and properly check errno. Java and C++ would throw an exception, etc.&lt;p&gt;Thus preventing all sorts of bad behavior up the stack.</text></comment>
<story><title>Fork() can fail: this is important (2014)</title><url>https://rachelbythebay.com/w/2014/08/19/fork/</url></story><parent_chain></parent_chain><comment><author>usefulcat</author><text>When I first learned of this behavior, I immediately replaced all calls to fork() with calls to the following throughout our entire codebase:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    int checked_fork() {
        const int pid = fork();
        if (pid &amp;lt; 0) {
            throw std::runtime_error(&amp;quot;fork failed&amp;quot;);
        }
        return pid;
    }&lt;/code&gt;&lt;/pre&gt;</text></comment>