'C program-fork command in a for loop

I am writing a C program that uses a fork command and loops 10 times, at the same time, the process ID will be displayed in each loop.

Following are my codes:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

main ()
{ int x;
  for(x=0;x<10;x++)
  {
   fork();
   printf("The process ID (PID): %d \n",getpid());
  }
}

My codes generate numerous of process ID,is there anything wrong in the program?



Solution 1:[1]

check my exemple

  pid_t pID = fork();
       if (pID == 0)                // child
       {
          // Code only executed by child process

          sIdentifier = "Child Process: ";
          globalVariable++;
          iStackVariable++;
    }
        else if (pID < 0)            // failed to fork
        {
            cerr << "Failed to fork" << endl;
            exit(1);
            // Throw exception
        }
        else                                   // parent
        {
          // Code only executed by parent process

          sIdentifier = "Parent Process:";
        }

        // Code executed by both parent and child.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user1990