mino
mino

Reputation: 7298

Child Process Creation through fork() in C

I'm completely new to C and learning about processes. I'm a little confused as to what the code below is actually doing, it's taken from Wikipedia but I've seen it in several books and am unsure as to why, for example, we do pid_t pid; then pid = fork();. My reading seem to suggest the child process returns a pid of 0, however, I thought the very original parent process will maintain the pid of 0 after seeing a tree with the root as pid 0.

What does, for example, pid = fork(); do to the parent? As doesn't it do the same thing for the child? And doesn't pid = fork(); put it into a loop as it will do this for each child?

Basically, could someone explain each step to me as if I were, say, five? Maybe younger? Thanks!

#include <stdio.h>   /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h>  /* _exit, fork */
#include <stdlib.h>  /* exit */
#include <errno.h>   /* errno */

int main(void)
{
   pid_t  pid;

   /* Output from both the child and the parent process
    * will be written to the standard output,
    * as they both run at the same time.
    */
   pid = fork();

   if (pid == -1)
   {
      /* Error:
       * When fork() returns -1, an error happened
       * (for example, number of processes reached the limit).
       */
      fprintf(stderr, "can't fork, error %d\n", errno);
      exit(EXIT_FAILURE);
   }
   else if (pid == 0)
   {
      /* Child process:
       * When fork() returns 0, we are in
       * the child process.
       */
      int  j;
      for (j = 0; j < 10; j++)
      {
         printf("child: %d\n", j);
         sleep(1);
      }
      _exit(0);  /* Note that we do not use exit() */
   }
   else
   {
      /* When fork() returns a positive number, we are in the parent process
       * (the fork return value is the PID of the newly created child process)
       * Again we count up to ten.
       */
      int  i;
      for (i = 0; i < 10; i++)
      {
         printf("parent: %d\n", i);
         sleep(1);
      }
      exit(0);
   }
   return 0;
}

Upvotes: 2

Views: 33432

Answers (3)

Bechir
Bechir

Reputation: 1051

Upon successful completion, fork() (source):

  • shall return 0 to the child process
  • and shall return the process ID of the child process to the parent process.

The example you gave is well explained. However, I would like to precise that Both processes (parent and child) shall continue to execute from the fork() function.

if you would like to know the PID of the child (from the code of the child), use getpid API.

Upvotes: 2

user2194434
user2194434

Reputation: 81

After executing the fork() function, you have two processes, which both continue executing after the fork call. The only difference between the two processes is the return value of fork(). In the original process, the "parent", the return value is the process id (pid) of the child. In the new cloned process, the "child", the return value is 0.

If you wouldn't test the return value of fork(), both processes would be doing exactly the same.

NB: to understand why the fork() function is useful, you need to read what the exec() function is doing. This function loads a new process from disk, and replaces the caller process with the new process. The combination of fork() and exec() is actually the way to start a different process.

Upvotes: 3

Femaref
Femaref

Reputation: 61497

fork is a function that returns twice - once for the parent, once for the child.

For the child, it returns 0, for the parent the pid of the child, any positive number; for both processes, the execution continues after the fork.

The child process will run through the else if (pid == 0) block, while the parent will run the else block.

Upvotes: 1

Related Questions