创建太多进程并且似乎只终止父进程

时间:2016-10-15 21:33:32

标签: c fork

我创建了一个为父进程创建两个子进程的程序。该程序将输出显示其进程ID的父进程,然后输出显示其ID和父进程ID的两个子进程。父进程应该在程序退出并打印输出后使用wait()函数捕获子进程。

但是,我的程序不断创建父进程并为这些进程提供子进程。我只想为两个子进程创建一个父进程。 while循环内部是wait()函数,它应该检查子进程的更改状态并打印“Child'xxx'进程终止”。相反,它正在终止一些父进程和其他随机进程。

#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);

}

我的输出显示了这个

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

3 个答案:

答案 0 :(得分:3)

一旦fork(),如果您只想在父进程中调用fork(),则子进程和父进程再次调用fork(),请在再次分叉之前检查返回值。 / p>

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

fork()返回时,它会返回父级中的子级PID和子级中的0

答案 1 :(得分:2)

首先阅读fork手册页以及getppid / getpid手册页。

来自fork的文档:

  

成功时,子进程的PID将在父进程中返回   执行的线程,并在子的线程中返回0   执行。失败时,将在父上下文中返回-1,   将不会创建子进程,并且将正确设置errno。

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

答案 2 :(得分:1)

fork()的第一个孩子正在分娩第二个孩子,而且您还没有为第一个孩子处理其他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);
          }
   }

你正在做的方式,第一个孩子正在分叉它自己的孩子,它为返回的PID!= 0的情况定义了一个处理程序。所以你将产生两个第二个孩子和处理父案件两次。