I know what the fork() does at the higher level. What I’d like to know is this –
-
As soon as there is a fork call, a trap instruction follows and control jumps to execute the fork “handler” . Now,How does this handler , which creates the child process, by duplicating the parent process by creating another address space and process control block , return 2 values, one to each process ?
-
At what point of execution does the fork return 2 values ?
To put it in short, can anbody please explain the step-by-step events that take place at the lower level after a fork call ?
It’s not so hard right – the kernel half of the fork() syscall can tell the difference between the two processes via the Process Control Block as you mentioned, but you don’t even need to do that. So the pseudocode looks like:
Edit:
The naive version does just as you describe – it creates a new process context, copies all of the associated thread contexts, copies all of the pages and file mappings, and the new process is put into the “ready to run” list.
I think the part you’re getting confused on is, that when these processes resume (i.e. when the parent returns from kernel_do_fork, and the child is scheduled for the first time), it starts in the middle of the function (i.e. executing that first ‘if’). It’s an exact copy – both processes will execute the 2nd half of the function.