Search code examples
coperating-systemforksystem-callskill

Kill while redirecting output in C doesn't really kill


I have a parent process in which I have a while loop. In that while loop I create children(count times). If an error occurs in any child, I want nothing else to be written to the standart output, only the first error message.

When an error occurs, I want the program to shut down all children and the parent, how do I do that. I am trying:

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

but it's not working, it prints the error multiple times

I want to terminate/kill the program when a single error happens so that no other message is printed

count is 5, command1 and command 2 are "ls"


Solution

  • The answer was to give the processes a way to communicate with the parent and on failure in ANY child have the parent print an error.