Question: I know that fork() creates a duplicate process (clone) meaning two identical copies of address spaces are created - one for the parent and one for the child. This process becomes child process of the caller. However, I am confused as to what is inside fork\_rv (see comment in code below)

```
include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

main()
{
int fork_rv;
printf("Before: my pid is %d\n",getpid());
fork_rv=fork();
if (fork_rv == -1)
perror("fork");
else if (fork_rv == 0)
printf ("I am the child. my pid=%d\n",getpid());
else
printf ("I am the parent. my child is %d\n",fork_rv); /* What is inside fork_rv What gets printed exactly? The address of the child?) */
}

```

Answer: 
Quoting from the Linux manual page for `fork`:

> 
> 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.
> 
> 
>
While @Brian's answer is already correct, maybe explaining the logic behind these return values makes it easier to understand:

* -1 for error should be clear.
* 0 as value for the child process makes sense because the child process can always get its own pid (`getpid()`) as well as the pid of its parent process (`getppid()`).
* All values > 0 are the pid of the new child process that was created, returned to the parent process.  
 Since a process can have multiple child processes, a 'get child pid' function is not possible: The pid of which child should it return? And with a 'get children pids' function that would return a list of all child processes pids, it would be cumbersome to find the pid of the latest, new child process.