What happens on the lower levels after calling the plug system?

I know what fork () does at a higher level. I would like to know this -

  • As soon as the fork call comes up, trap instructions and control jumps follow to execute the fork handler. Now, how does this handler that creates the child process, by duplicating the parent process by creating another address space and the process control block, return 2 values, one for each process?

  • At what point in time does fork return 2 values?

In short, can anyone explain the step-by-step events that occur at a lower level after the fork call?

+6
system-calls fork
source share
2 answers

Not so complicated: half of the fork () syscall kernel can determine the difference between two processes through the process control unit, as you mentioned, but you don’t even have to. So, the pseudo code looks like this:

int fork() { int orig_pid = getpid(); int new_pid = kernel_do_fork(); // Now there two processes // Remember, orig_pid is the same in both procs if (orig_pid == getpid()) { return new_pid; } // Must be the child return 0; } 

Edit: The naive version works the way you describe - it creates a new process context, copies all the associated stream contexts, copies all pages and file associations, and the new process is placed on the "ready to start" list.

I think that the part that you confuse is that when these processes resume (that is, when the parent returns from kernel_do_fork and the child is assigned for the first time), it starts in the middle of the function (i.e. the execution of this first " if a" ). This is an exact copy - both processes will perform the second half of the function.

+1
source share

The value returned to each process is different. The parent / source thread receives the PID of the child process, and the child process receives 0.

The Linux kernel achieves this on x86 by changing the value in the eax register, since it copies the current thread in the parent process.

+1
source share

All Articles