Tracing the execution of a process
<p>I have this code that gets its process ID and its parent process:</p>

<pre><code>#include &lt;sys/types.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdio.h&gt;

int main(){
int pid;
printf("I am the original process with PID %d and PPID %d. \n", getpid(), getppid());
pid = fork();

if (pid &gt;0){
    printf("I am the original process with PID %d and PPID %d \n", getpid(), getppid());
    printf("My child’s pid is %d \n" , pid);
}
else if(pid == 0) {
    printf ("I am the original process with PID %d and PPID %d \n", getpid(), getppid());
}
else if (pid == 1){
    printf ("Error – no child process was created \n");
}else{
    printf ("Error – system error \n");
}
    printf (" PID %d terminates \n", getpid()); /* both processes execute this instruction */

return 0;
}
</code></pre>

<p>OUTPUT</p>

<pre><code>I am the original process with PID 1009 and PPID 964. 
I am the original process with PID 1009 and PPID 964 
My child’s pid is 1010 
 PID 1009 terminates 
I am the original process with PID 1010 and PPID 1009 
 PID 1010 terminates 
</code></pre>

<p>Few questions that confuses me..
how does this code is executed?
In the output, you can see that it runs the code under under <code>if(pid == 0)</code> while the condition <code>if(pid &gt; 0)</code> is already executed. how come pid is equal to 0? while it was set already to greater than 0.
Lastly , what does <code>fork()</code> really do?</p>