After fork(), where does the child begin its execution?

I’m trying to learn UNIX programming and came across a question regarding fork(). I understand that fork() creates an identical process of the currently running process, but where does it start? For example, if I have code

int main (int argc, char **argv)
{
    int retval;
    printf ("This is most definitely the parent processn");
    fflush (stdout);
    retval = fork ();
    printf ("Which process printed this?n");

    return (EXIT_SUCCESS);
}

The output is:

This is most definitely the parent process  
Which process printed this?  
Which process printed this?

I thought that fork() creates a same process, so I initially thought that in that program, the fork() call would be recursively called forever. I guess that new process created from fork() starts after the fork() call?

If I add the following code, to differentiate between a parent and child process,

if (child_pid = fork ())
    printf ("This is the parent, child pid is %dn", child_pid);
else
    printf ("This is the child, pid is %dn", getpid ());

after the fork() call, where does the child process begin its execution?

Asked By: thomas1234

||

The new process will be created within the fork() call, and will start by returning from it just like the parent. The return value (which you stored in retval) from fork() will be:

  • 0 in the child process
  • The PID of the child in the parent process
  • -1 in the parent if there was a failure (there is no child, naturally)

Your testing code works correctly; it stores the return value from fork() in child_pid and uses if to check if it’s 0 or not (although it doesn’t check for an error)

Answered By: Michael Mrozek

I thought that fork() creates a same process, so I initially that that in that program, the fork() call would be recursively called forever. I guess that new process created from fork() starts after the fork() call?

Yes. Let’s number the lines:

int main (int argc, char **argv)
{
    int retval;                                               /* 1 */
    printf ("This is most definitely the parent processn");  /* 2 */
    fflush (stdout);                                          /* 3 */
    retval = fork ();                                         /* 4 */
    printf ("Which process printed this?n");                 /* 5 */
    return (EXIT_SUCCESS);                                    /* 6 */
}

The execution flow is:

caller process     fork() → ...
                          ↘
original program            exec() → 2 → 3 → 4 → 5 → 6
                                               ↘
forked program                                   5 → 6

…which explains exactly the output you received.

If you want to know how the original and forked program can possibly behave differently, since they necessarily share the same code, see Michael Mrozek’s answer.

Answered By: badp

whatever the code just after the fork(), is copied into the child process,
and don’t mixup the parent and child process, they are two different entities, that have same (duplicated, not shared) environment.

Now see your output…

Answered By: user2670535

The real solution to this is

switch (fork()) {
    case -1 :
        fprintf (stderr, "fork failed (%s)n", strerror(errno));
        break;
    case 0 :  // child process comes here
        break;
    default : // parent process
        break;
}

// all continue here
Answered By: ott–

Very precisely the child process begins it’s execution while the fork() system call is being executed in the kernel:

There, after having created a new process (and a process control block) the registers of the child process are changed to make it return zero, while the registers of the parent process will make the system call return the PID of the child (assuming the syscall succeeded).

For practical reasons the user mode execution of each process continues right after returning from the system call, and that would be exactly the same instruction in both processes.

So it is essential to check the value returned from fork.

Answered By: U. Windl
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.