捕获C中的信号并杀死所有儿童

捕获C中的信号并杀死所有儿童,c,signals,fork,kill-process,C,Signals,Fork,Kill Process,我需要捕获CTRL+C并完成子进程,主进程必须等到完成其内容,然后程序必须完成。 这是我的代码: void sigint_handler() { /*do something*/ printf("killing process %d\n",getpid()); exit(0); } int main () { signal(SIGINT, sigint_handler); printf ("This is the parent. PID=%d\n",get

我需要捕获CTRL+C并完成子进程,主进程必须等到完成其内容,然后程序必须完成。 这是我的代码:

 void sigint_handler()
{
    /*do something*/
    printf("killing process %d\n",getpid());
    exit(0);
}

int main ()
{
  signal(SIGINT, sigint_handler);


  printf ("This is the parent. PID=%d\n",getpid ());

  int num_children = 4;

  int i;

  while (1){

  for (i=0; i<num_children ;i++){

   if (fork()==0){

    printf ("This is children %d\n",getpid()); 

    sleep(1);

    exit(0);

   }

  }

  //Rest Parent code
  sleep (1);

  printf ("This is the parent again %d, children should have finished\n",getpid());

  //Do stuff

  }

}
我该怎么办?我不想杀父母,只想杀孩子,先谢谢你

在信号处理程序中,子进程仅处理。在父进程中,让子进程在退出自身之前退出。

进行此更改

  for (i=0; i<num_children ;i++){

   if (fork()==0){
signal(SIGINT, sigint_handler);
    printf ("This is children %d\n",getpid()); 

    sleep(1);

    exit(0);

   }

  }

for(i=0;i现在这样更好:

    void sigint_handler()
    {
        /*do something*/
        printf("killing process %d\n",getpid());
        exit(0);
    }

    int main ()
    {
        int num_children = 4;
        int i, pid, status;
        printf ("This is the parent. PID=%d\n", getpid());

        while (1) {
            for (i = 0; i < num_children; i++) {
                if ((pid = fork()) == 0) {
                    signal(SIGINT, sigint_handler);
                    printf("This is children %d\n", getpid()); 
                    sleep(1);
                    exit(0);
                }
            }

            // Rest Parent code
            sleep (1);
            waitpid(pid, &status, 0); 
            printf ("This is the parent again %d, children should have finished\n", getpid());
        }
    }
我想在最后打印:这是家长20048,孩子们应该完成,然后完成。
非常感谢

不要在信号处理程序中使用printf。它不是可重入的。我必须用另一个答案(就在下面)来回答你。一条评论是不够的
    void sigint_handler()
    {
        /*do something*/
        printf("killing process %d\n",getpid());
        exit(0);
    }

    int main ()
    {
        int num_children = 4;
        int i, pid, status;
        printf ("This is the parent. PID=%d\n", getpid());

        while (1) {
            for (i = 0; i < num_children; i++) {
                if ((pid = fork()) == 0) {
                    signal(SIGINT, sigint_handler);
                    printf("This is children %d\n", getpid()); 
                    sleep(1);
                    exit(0);
                }
            }

            // Rest Parent code
            sleep (1);
            waitpid(pid, &status, 0); 
            printf ("This is the parent again %d, children should have finished\n", getpid());
        }
    }
This is the parent. PID=20048
This is children 20049
This is children 20050
This is children 20051
This is children 20052
This is the parent again 20048, children should have finished
This is children 20053
This is children 20054
This is children 20056
This is children 20055
^Ckilling process 20056
killing process 20055
killing process 20053
killing process 20054