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

29,321

Solution 1

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)

Solution 2

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 process\n");  /* 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.

Share:
29,321

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 17, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 1 year

    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 process\n");
        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 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?

    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 %d\n", child_pid);
    else printf ("This is the child, pid is %d\n",getpid ());
    

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

    • Admin
      Admin over 13 years
      man fork is sure enough to answer your question, btw
  • badp
    badp over 13 years
    Note that 1 is not actually an instruction. Also note that the original and the forked programs don't actually run at the same time -- either one will have to wait for the other to yield/be preempted.
  • jlliagre
    jlliagre over 13 years
    On multi-core/multi-cpu systems, both programs can actually run at the same time.
  • badp
    badp over 13 years
    @jilliagre Multicore systems are about multithreading really. As for systems with multiple CPUs, I don't know if that's the case or not in practice. I'm no expert in this field -- and it just seems like such an unlikely scenario. If we agree the OS can run multiple processes at the same time (then how would it handle concurrency?), by the time the original program runs instruction 4 on a CPU, the other CPUs are likely busy running other processes anyway.
  • jlliagre
    jlliagre over 13 years
    I would say it is a very likely scenario, especially as there is an underlying system call with some I/O happening in step 5. Having all CPUs busy is actually not a common situation as the CPU is rarely the bottleneck with current machines. It seems too you are confusing multi-threading and multi-core.
  • Amit
    Amit about 6 years
    If forked or child process starts at line 5, who sets retval to 0 in the child process? I believe @Michael Mrozek rightly mentioned that it starts in the child with returning value from fork. csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html also says something similar: 'After a new child process is created, both processes will execute the next instruction following the fork() system call.'. The next instruction is assignment to retval.
  • OrangeDog
    OrangeDog almost 6 years
    @Amit technically of course fork() isn't the system call. It's usually a few more functions into libc where the syscall (or int 0x80) instruction actually occur.
  • ctrl-alt-delor
    ctrl-alt-delor almost 4 years
    All the code is copied. But execution starts with in the fork, so both return from fork (after the fork).