Q:

does cout << fork() acctully call fork()?

I have a problem with fork function. Is
cout << fork();

acctully call fork() ?
I am trying to make process tree but still don't know how to manage the fork() function.
I wrote simple code to figure it, but it explain nothing.
int mainPID = getpid();
cout << "Main process: " << mainPID << endl << endl;

cout << fork() << endl;
cout << getpid() << endl;
cout << getppid() << endl;

It returns:
118,
119,
120
programme ends and get another values on screen 0, 118, 1
Is cout << fork() acctualy do fork() ?
I thought I get only values related to my mainPID.

A:

fork() returns twice: once in the original parent process, and also in the child process. In the parent process it returns the PID of the child. In the child process it returns 0.
In both processes, the return value is then printed by cout <<. So you get multiple outputs.
If the parent process finishes quickly, it will exit before the child process calls getppid(). The child process will then be inherited by the init process, which is PID 1. So the child prints 1 when it gets to cout << getppid() << endl;.

