eric eric
eric eric

Reputation: 55

Creating too many processes and seems to only terminate parent processes

I have created a program that creates two child process for a parent process. The program is to output the parent's process showing its process ID and then the two child processes showing their IDs and the ID of the parent. The parent process is supposed to capture the child process using the wait() function after the program exits and print an output.

However, my program keeps creating parent processes and giving children to those processes. I only want one parent process for the two child processes. Inside the while loop is the wait() function that is supposed to check for the changed state of the children process and print " Child 'xxx' process terminated". Instead, it is terminating some of the parent processes and other random processes.

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

int main()
{
   pid_t cpid, cpid2, wpid;
   int child = fork();
   int child2 = fork();
   int status;

   if ((child = fork()) == 0){
      child = cpid;
   }
   if((child2 = fork()) == 0){
      child2 = cpid2;
   }
   else{
      printf("I am the parent %d\n", getppid());
      printf("I am the process %d created by %d\n", cpid, getppid());
      printf("I am the process %d created by %d\n", cpid2, getppid());

      while ((wpid = wait(&status)) > 0){
      printf("Child process %d terminated\n", wpid);
      }

   }

   return (0);

}

My output is showing me this

I am the parent 5764
I am the process 2 created by 5764
I am the process 6411548 created by 5764
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 23612
I am the process 2 created by 23612
I am the process 6411548 created by 23612
I am the parent 15096
I am the process 2 created by 15096
I am the process 6411548 created by 15096
I am the parent 24276
I am the process 2 created by 24276
I am the process 6411548 created by 24276
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 5764
I am the process 2 created by 5764
I am the process 6411548 created by 5764
Child process 17016 terminated
Child process 18584 terminated
Child process 13984 terminated
Child process 8480 terminated
Child process 10816 terminated
Child process 21968 terminated
Child process 23388 terminated
Child process 11452 terminated
Child process 2776 terminated
Child process 19328 terminated
Child process 17116 terminated
Child process 18352 terminated
Child process 24276 terminated
Child process 15096 terminated
Child process 5764 terminated

Upvotes: 1

Views: 209

Answers (3)

Vedant
Vedant

Reputation: 468

Start by reading the fork man page as well as the getppid / getpid man pages.

From fork's documentation:

On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately.

if ((child = fork()) == 0){
    printf(" %u and %u", getpid(), getppid());
} else{ /* avoids error checking*/
    printf("Parent - %u ", getpid());
}

Upvotes: 2

Michael Nealon
Michael Nealon

Reputation: 76

The first child you fork() is forking the second child and also you're not handling else for the first fork()

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

#include <stdlib.h>

int main()
{
   pid_t cpid, cpid2, wpid;
   int child = fork();
   int child2 = fork();
   int status;

   if ((child = fork()) == 0){
      child = cpid;
   }
   else {
       if((child2 = fork()) == 0){
          child2 = cpid2;
       }
       else{
          printf("I am the parent %d\n", getppid());
          printf("I am the process %d created by %d\n", cpid, getppid());
          printf("I am the process %d created by %d\n", cpid2, getppid());

          while ((wpid = wait(&status)) > 0){
              printf("Child process %d terminated\n", wpid);
          }
   }

The way you're doing it, the first child is forking it's own child which defines a handler for the case where the returned PID != 0. So you will spawn two of the second child and handle the parent case twice.

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

Once you fork(), both the child and the parent are calling fork() again if you want to call fork() in the parent process only, check the return value before forking again.

int child = fork();
// This will be called by both, the child and the parent
int child2 = fork();

when fork() returns it returns the child PID in the parent and 0 in the child.

Upvotes: 3

Related Questions