Why does this code using fork() work? : <p>I've this code that executes some code depending of if the active process is the parent or the child process in an infinite loop:</p>

<pre><code>pid_t childPID;

childPID=fork();

while (1)
{
    if (childPID &gt;=0)
    {
        if (childPID==0)
        {
            [do child process stuff]
        }   
        else
        {
            [do parent process stuff]
        }
    }
    else
    {
        printf("\n Fork failed, quitting!!!!!\n");
        return 1;   
    }
 }
</code></pre>

<p>Code is simple but there's one very big thing on it for me which I don't understand how it happens although I have a guess:</p>

<p>If not taking into consideration that we're creating 2 processes it looks like childPid is constantly being reasigned which I don't think makes any sense.</p>

<p>So my guess, is that fork creates a childPid for each process, returning a 0 to the parent process and the pid to the child process, even though this syntax makes me think it should only return only one result and assign it to chilPid.</p>

<p>Is my guess correct or is there some other thing involved?</p>

<p>Thank you.</p>
