Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork and returning twice

I am working on a project that requires implementation of a fork() in unix. I read freeBSD and openBSD source code but it is really hard to understand. Can someone please Explain the returning twice concept? I understand that one return is pid of a child, and that gets returned to parent and other one is zero and it gets returned to a child process. But I cannot wrap my head around how to implement this notion of returning twice... how can I return twice? Thanks everyone in advance.

like image 265
user977690 Avatar asked Oct 24 '25 04:10

user977690


1 Answers

When you call fork, it returns "twice" in that the fork spawns two processes, which each return.

So, if you're implementing fork, you have to create a second process without ending the first. Then the return-twice behavior will happen naturally: each of the two distinct processes will continue execution, only differing in the value they return (the child giving zero, and the parent giving the child's PID).

like image 165
Borealid Avatar answered Oct 25 '25 20:10

Borealid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!