I know that fork() returns differently for the child and parent processes, but I\'m unable to find information on how this happens. How does the child process receive the re
I will try to answer from the process memory layout point of view. Guys, please correct me if anything wrong or inaccurate.
fork() is the only system call for process creation (except the very beginning process 0), so the question is actually what happens with process creation in kernel. There are two kernel data structures related with process, struct proc array (aka process table) and struct user (aka u area).
To create a new process, these two data structures have to be properly created or parameterized. The straight-forward way is to align with the creater's (or parent's) proc & u area. Most data are duplicated between parent & child (e.g., the code segment), except the values in the return register (e.g. EAX in 80x86), for which parent is with child's pid and child is 0. Since then, you have two processes (existing one & new one) run by the scheduler, and upon the scheduling, each will return their values respectively.