21

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?

  • 5
    man fork is sure enough to answer your question, btw – alex Nov 28 '10 at 8:21
23

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)

13

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.

  • 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 '10 at 12:32
  • 1
    On multi-core/multi-cpu systems, both programs can actually run at the same time. – jlliagre Nov 28 '10 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 '10 at 14:40
  • 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. – jlliagre Nov 28 '10 at 21:14
  • 8
    Can I just comment to say those diagonal arrows are fantastic. – JBirch Nov 29 '10 at 10:30
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...

Your Answer

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