title: How to send SIGINT to background processes only in C
body: <p>I am writing a simple shell program, where I can execute some background processes. I want using a specific command like "killbg" be able to SIGINT every background processes I have executed. I have tried:</p>
  
  <ol>
  <li>Make a process group consisting of only background processes using <code>setpgid(0,0)</code>
  in the clause where <code>pid=fork()</code> is 0, i.e. when I'm going to execute the child process.</li>
  <li>The process group 'pgid' I get after the <code>setpgid(0,0)</code> statement, I use in kill function, like <code>kill(pgid, 2)</code>.</li>
  </ol>
  
  <p>Unfortunately, the whole program gets interrupted, and background tasks are still running.</p>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>while(1){
    fgets.. //user input for command
  
    // Kill background processes      
          if(strcmp(execArgs[0], "killbg") == 0){    //execArgs argument array for execve
              kill(pgid, 2); 
              perror("");
          }
          pid=fork();
  
          // Error in fork
          if(pid &lt;= -1){
              perror("Error with fork.\n");
           exit(EXIT_FAILURE);
          }       
          // Child process
          else if(pid == 0){
              if(execBG == TRUE){    // execBG true If user wants to run command in background
                  setpgid(0,0);
                  pgid = getpgid(pid);
              }
              execve(execArgs[0], execArgs, NULL); 
              fprintf(stderr, "Not a valid command! Remember the full path.\n");  
  
          }
          // Parent process       
          else{
              if(execBG==FALSE){
                  waitpid(-1, &amp;status, NULL);
              }
              else{
                  ;    // if background process, don't wait for the child
              }
          }               
      }  
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>