25

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?

1
  • 7
    man fork is sure enough to answer your question, btw
    – alex
    Nov 28, 2010 at 8:21

4 Answers 4

25

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)

14

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.

7
  • 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.
    – badp
    Nov 28, 2010 at 12:32
  • 1
    On multi-core/multi-cpu systems, both programs can actually run at the same time.
    – jlliagre
    Nov 28, 2010 at 14:07
  • @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.
    – badp
    Nov 28, 2010 at 14:40
  • 11
    Can I just comment to say those diagonal arrows are fantastic.
    – JBirch
    Nov 29, 2010 at 10:30
  • 1
    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.
    – Amit
    Mar 11, 2018 at 18:34
0

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

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

1
  • 1
    All the code is copied. But execution starts with in the fork, so both return from fork (after the fork). Aug 1, 2020 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy