在 C 中重定向输出时杀死并没有真正杀死

Kill while redirecting output in C doesn't really kill

我有一个 parent 进程,其中有一个 while 循环。在那个 while 循环中,我创建了 children(count times)。如果在任何 child 中发生错误,我不希望将任何其他内容写入标准输出,只希望写入第一条错误消息。

当出现错误时,我想让程序关闭所有children和parent,我该怎么做。我正在尝试:

while(count--) {
    pid = fork();
    if(pid > 0){
      wait(NULL); 
      if(execlp(command1,command2,(char *)NULL) < 0){ // error occured
        char str[1024] = "errr";
        perror(str); 
        sprintf(str, "kill -SIGINT %d >> /dev/null 2>&1",getppid());  
        system(str); 
      }
  }

但它不起作用,多次打印错误

我想 terminate/kill 程序在发生单个错误时不打印其他消息

count为5,command1和command 2为"ls"

答案是为进程提供一种与父进程通信的方式,并在任何子进程失败时让父进程打印错误。