2

Is there a way to determine the entry point of a child process?

I am trying to create 3 processes where each process has its own function and communicates with the parent process.

I have been trying to find out the entry point for a child process (in C) and am unable to reach a conclusion..

Case 1:

It is the starting of the main() method. Contradiction: there would be infinite processes created as fork() would be called every time. Is there a way to handle this?

Case 2:

The entry point is right after the fork() system call(). Contradiction: The output of the following code:

int main() 
{ 
    printf("Enter ");
    for(int i=0;i<3;i++)
    { 
        if(fork() == 0) 
        { 
            printf("i = %d\n", i);
            printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid()); 
            exit(0); 
        } 
    } 
    for(int i=0;i<3;i++)
    {
        wait(NULL);
    } 
}

"Enter" is printed 4 times in this case.

Is there is no fixed entry point then how am I supposed to write implementation for a process.

Would be great if someone could clear my confusion.

harsh
  • 83
  • 8
  • 3
    Your Case 2 is the right one: it's the point at which `fork()` returns. The apparently contradictory behavior you're seeing is caused by buffering. Do `fflush(stdout);` before the `fork()` and it will make more sense. – Nate Eldredge Mar 04 '21 at 03:18

0 Answers0