Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Every calls to the unix process creation system call fork() returns twice. First it returns with the PID of the child to the parent(the process which called fork()). Second it returns to 0 to the newly created child.</p> <p>from man pages:</p> <p><strong>Return Value</strong></p> <p><strong>On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.</strong></p> <p>in your case </p> <pre><code>if (fork() &amp;&amp; !fork()) </code></pre> <p>The statement inside <code>if</code> , calls fork twice. So what will happen is following :</p> <pre><code>A |----------------B | | |---C | | | | </code></pre> <p>Now first call to <code>fork()</code> will return in both A and B. In A it will be nonzero and in B it will be zero.</p> <p>Second call to fork() will be evoked only from A. because first fork returned 0 to B, it will not Evoke a second <code>fork()</code>. <strong>its because <code>&amp;&amp;</code> short circuits the evaluation if first operand is found non zero.</strong> Thanks to Daniel for pointing this out.</p> <p>So we can make a table out of this:</p> <pre><code>PID fork()1 fork()2 ------------------------------ A &gt;0 &gt;0 B =0 &gt;0 C &gt;0 =0 </code></pre> <p>So from the chart, Process C's <code>if</code> will be evaluated to <strong>TRUE</strong></p> <p>Its important to remember, <code>fork()1</code> didn't returned to C . it got the copy of <strong>Already evaluated expression</strong> from its parent.</p> <p>I hope this explains your question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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