The child process begins on the first line when created (using fork)

Why does my child process begin on the first line of the main? When I run my program, "Hello There" will print twice. The child process should start on the line after the fork, am I right?

int main(){
       printf("Hello There!");
       pid_t PID;
       PID = fork();

       if(PID == 0){
          //Child
       }
       else{
          //parent
       }
}

This is a huge problem for me, because in my real program I am making a private file with mmap before fork. And because of this, what happens to me, the parent and the child have different personal files.

+4
source share
1 answer

Calling NOT twice ... This is an optical illusion! :)

... STDOUT , \n, , , , ( fork ( )) .

\n, printf :

printf("Hello There!\n");

.

:

printf("Hello There!");
fflush(stdout);
+6

Source: https://habr.com/ru/post/1613041/


All Articles