Skip to content
Advertisement

How does the fork() know whether it is in child process and in parent process? [closed]

When a fork() system call gets executed , the processor turns into kernel mode.

Thus at the end of the fork call a new process has spawned with a copy of almost all the constructs of the calling process (like the stack , user data and user programs etc.)

It is also clear from many very well answered explanations that how a process (a programmer) can identify the difference between a child process and a parent process. As per the many documented answers on SO and elsewhere:

if a call to fork() returns 0 it means that it is the child process that called the fork(). While if the return value of fork() is a positive integer, it means that the fork() was executed in the parent process and the returned positive integer is a PID of the child process. Ignoring the case of negative value returns for the sake of brevity.

Hopefully my understanding above is correct :fingers_crossed:

However, the below is what I am trying to understand better and hopefully in more details/depth:

(please pardon my ignorance on the subject.I am just curious to understand this better from an internal perspective. This topic and my want to get a clearer understanding around it has literally started hijacking my dreams now 😛 )

How does the system call (that gets invoked when fork() is called) know that now it is being executed as part of the child process and hence should return 0?

Alternatively put, how does the system call know that it is being executed in the context of the parent process and hence it should now return the child process’ PID this time and not 0?

Or is it such that out of the 2 processes, since both are exact replicas, one of them randomly gets the return value as 0 and the other as the PID and then based on the which one got what return value, it is considered parent and child accordingly? :thinking_face:

I’d be happy to be guided in the direction with any resources that I should go through. I would really appreciate any help in this direction actually. :pray:

Advertisement

Answer

When fork() is called, a process spawns a child process with shared or duplicated process segments. What that means is that in a program, before fork() is called, there is only one process, or one unit of execution. After fork() returns, there are two processes running concurrently. Since both processes have the same call stack at this point, it looks to each processes as if it had just called fork(). In the parent process, the return value of fork() is the PID of the child process. In the child process, the return value of fork() is 0.

You can see this with a really simple demonstration:

#include <unistd.h>
#include <stdio.h>

int main(){
    pid_t parent = getpid();
    pid_t child = fork();

    if(child != 0){
        printf("I am the parent process. My PID is %dn", parent);
        printf("Parent process terminatingn");
    }
    else{
        printf("I am the child process. My PID is %dn", getpid());
        printf("    My parent's PID is %dn", parent);
        printf("Child process terminatingn");
    }
    return 0;
}

Here’s a sample run on my laptop:

$ gcc -o fork fork.c
$ ./fork
I am the parent process. My PID is 16048
Parent process terminating
I am the child process. My PID is 16049
    My parent's PID is 16048
Child process terminating
$

Note that when you run this, the PIDs will be different. Also, the output is subject to a race condition, so sometimes, the parent process will return to the shell before the child process finished printing, so it might look like this:

$ ./fork
I am the parent process. My PID is 16265
Parent process terminating
$ I am the child process. My PID is 16266
    My parent's PID is 16265
Child process terminating

What’s important to understand is that fork() causes the single process of execution to split into two independent units. Since each process is still spawned from the same program (or source code), the behaviour is the same for both processes. The reason their output differs is only due to the fact that fork() returns different values for parent or children processes.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement