title: How and why can fork() fail?
body: <p>I'm currently studying the fork() function in C. I understand what it does (I think). Why do we check it in the following program?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
    int pid;
    pid=fork();
  
    if(pid&lt;0) /* Why is this here? */
    {
      fprintf(stderr, &quot;Fork failed&quot;);
      exit(-1);
    }
    else if (pid == 0)
    {
      printf(&quot;Printed from the child process\n&quot;);
    }
    else
    {
      printf(&quot;Printed from the parent process\n&quot;);
      wait(pid);
    }
  }
  </code></pre>
  <p>In this program we check if the PID returned is &lt; 0, which would indicate a failure. Why can fork() fail?</p>