title: Handling processes in C
body: <p>I have a problem when trying to handle a SIGUSR1 signal sent from a father process to all of his child's process. The handler on the child's does nothing. I checked for the result of the kill command and it returns 0 meaning that the message sent was ok . Can anyone help with this? Below is the code of the child process. I use execl to differ the childs code from the father. Note that the handler works well for alarm calls</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  /*Global declerations*/
  
  int alarmflag=0;
  double result=0;
  int fd[2];
  
  /*Handler for the alarm and SIGUSR1 signal*/
  void signal_handler (int sig)
  {
  printf("******************");
  if(sig==SIGALRM)
  {
  printf("Im child with pid:%d im going to die my value is %lf \n",getpid(),result);
  alarmflag=1;
  }
  
  if(sig==SIGUSR1)
  {
  printf("gotit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  }
  
  }
  
  
  double p_calculation ()
  {
   int i=2;
   result=3;
   double prosimo=-1;
  
   while(!alarmflag)
      {
   prosimo=prosimo*(-1);
   result=result+(prosimo*(4/((double)i*((double)i+1)*((double)i+2))));
   i=i+2;
      }
  
  }
  
  main(int argc,char *argv[])
  {
  int fd[2];
  
  /*handling signals*/
  signal(SIGALRM,signal_handler);
  signal(SIGUSR1,signal_handler);
  
  /*Notify for execution time*/
  printf("PID : %d with PPID : %d executing for %d seconds \n",getpid(),getppid(),atoi(argv[1]));
  
  /*end this after the value passed as argument*/
  alarm(atoi(argv[1]));
  p_calculation();
  
  /*Notify for finish*/
  printf("Done!!!\n");
  
  }
  </code></pre>
  
  <p>The code for the father follows : </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  
  pid_t *childs; //array for storing childs pids
  int number_of_childs;//variable for the number of childs
  int count_controls=0;
  
  /*Handler for the SIGINT signal*/
  
  void control_handler(int sig)
  {
  int j;
  
  for (j=0;j&lt;number_of_childs;j++)
      {
      kill(childs[j],SIGUSR1);
      }
  
  }
  
  main (int argc,char *argv[]){
  
  int i,child_status;
  int fd[2];
  char cast[512];
  int pid;
  number_of_childs=atoi(argv[1]);
  signal(SIGINT,control_handler);
  childs=malloc(number_of_childs*sizeof (pid_t));
  
  if(pipe(fd)==-1)
      {
      perror("pipe");exit(1);
      }
  
  for (i=0;i&lt;number_of_childs;i++){
  pid=fork();
      /*Create pipes to communicate with all children*/
  
  
  
      /*Fathers code goes here*/
  
      if(pid!=0)
          {
          printf("Parent process: PID= %d,PPID=%d, CPID=%d \n",getpid(),getppid(),pid);
          childs[i]=pid; // Keep all your childs in an array
          printf("Child:%d\n",childs[i]); 
          }
  
      /*If you are a child*/
  
          else 
          {
          /*Change the code for the childs and set the time of execution*/
          sprintf(cast,"%d",i+1);
          execl("./Child.out","",cast,NULL);
          }
      }   
          /*Father should never terminate*/               
          while (1);
  
  
  }
  </code></pre>