Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens at the lower levels after a fork system call?

I know what the fork() does at the higher level. What I'd like to know is this -

  1. 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 ?

  2. 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 ?

like image 964
Sharat Chandra Avatar asked Feb 05 '10 05:02

Sharat Chandra


People also ask

What happens after fork system call?

Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

What does the fork call do in operating system?

In the computing field, fork() is the primary method of process creation on Unix-like operating systems. This function creates a new copy called the child out of the original process, that is called the parent.

When a process calls fork it makes a complete of itself?

When a process forks, a complete copy of the executing program is made into the new process. This new process is a child of the parent process, and has a new process identifier (PID). The fork() function returns the child's PID to the parent process. The fork() function returns 0 to the child process.

What happens when fork return?

RETURN VALUE Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.


2 Answers

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:

int fork()
{
    int orig_pid = getpid();

    int new_pid = kernel_do_fork();     // Now there's 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 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.

like image 173
Ana Betts Avatar answered Oct 18 '22 03:10

Ana Betts


The value returned to each process is different. The parent/original thread get's the PID of the child process and the child process get's 0.

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

like image 31
Robert Christie Avatar answered Oct 18 '22 04:10

Robert Christie