Where does code Execution start in a child process?

c, fork, linux, process

Solution

It starts the execution of the child in the return of the fork function. Not in the start of the code. The fork returns the pid of the child in the parent process, and return 0 in the child process.

Problem

Consider the code: ``` #include <stdio.h> #include <errno.h> #include <sys/types.h> #include <unistd.h> /* main --- do the work */ int main(int argc, char **argv) { pid_t child; if ((child = fork()) < 0) { fprintf(stderr, "%s: fork of child failed: %s\n", argv[0], strerror(errno)); exit(1); } else if (child == 0) { // do something in child } } else { // do something in parent } } ``` My question is from where does in the code the child process starts executing, i.e. which line is executed first?? If it executes the whole code, it will also create its own child process and thing will go on happening continuously which does not happen for sure!!! If it starts after the fork() command, how does it goes in if statement at first??

Original source