The above code creates a new process, when it makes a fork call, this process will be an almost exact copy of the original process. Both processes will continue to execute sepotratly in the form of a return, fork call, which raises the question "How do I know if im a new process or an old one?" since they are almost identical. To do this, the fork developers made the fork call return different things in each process, in the new process (child), the fork call returns 0, and the original (parent) fork returns the identifier of the new process so that the parent can interact with it.
Thus, in the code, calling fork creates a child process, both processes execute the if seporatly statement. In the parent, the return value is not zero, so the parent operator executes the if statement. In the child case, the return value is 0, so it executes the else statement. Hope this helps :-)
Pinkynoborain
source share