07-24-2023, 04:12 AM
Take the following example:
int main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
So correct me if I am wrong, once fork() executes a child process is created. Now going by this [answer][1] fork() returns twice. That is once for the parent process and once for the child process.
Which means that two separate processes come into existence DURING the fork call and not after it ending.
Now I don't get it how it understands how to return 0 for the child process and the correct PID for the parent process.
This where it gets really confusing. This [answer][2] states that fork() works by copying the context information of the process and manually setting the return value to 0.
First am I right in saying that the return to any function is placed in a single register? **Since in a single processor environment a process can call only one subroutine that returns only one value (correct me if I am wrong here).**
**Let's say I call a function foo() inside a routine and that function returns a value, that value will be stored in a register say BAR. Each time a function wants to return a value it will use a particular processor register.** So if I am able to manually change the return value in the process block I am able to change the value returned to the function right?
***So am I correct in thinking that is how fork() works?***
[1]:
int main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
So correct me if I am wrong, once fork() executes a child process is created. Now going by this [answer][1] fork() returns twice. That is once for the parent process and once for the child process.
Which means that two separate processes come into existence DURING the fork call and not after it ending.
Now I don't get it how it understands how to return 0 for the child process and the correct PID for the parent process.
This where it gets really confusing. This [answer][2] states that fork() works by copying the context information of the process and manually setting the return value to 0.
First am I right in saying that the return to any function is placed in a single register? **Since in a single processor environment a process can call only one subroutine that returns only one value (correct me if I am wrong here).**
**Let's say I call a function foo() inside a routine and that function returns a value, that value will be stored in a register say BAR. Each time a function wants to return a value it will use a particular processor register.** So if I am able to manually change the return value in the process block I am able to change the value returned to the function right?
***So am I correct in thinking that is how fork() works?***
[1]:
[To see links please register here]
[2]:[To see links please register here]