title: Trouble sending signal between parent and child process in C
body: <p>I am doing project for my studies according to guidelines and I have a problem. I have to do communication using signals and pipes as follows:</p>
  
  <ul>
  <li>process 2 receive signal and send it to parent</li>
  <li>parent process save value of signal in pipe and send signal to process 1 to read pipe</li>
  <li>process 1 read pipe and send signal to process 2 to read pipe</li>
  <li>process 2 read pipe and send signal to process 3 to read pipe</li>
  <li>process 3 read pipe</li>
  </ul>
  
  <p>Unfortunately when I send one of the supported signals (e. g. 20) to process 2 signals are looped and program is terminated by SIGUSR1.</p>
  
  <pre><code>int stopm=0;
  int stop1=0;
  int stop2=0;
  int stop3=0;
  int termm=1;
  int term1=1;
  int term2=1;
  int term3=1;
  
  void obslugam(int sig){
      signal(15,obslugam);
      signal(18,obslugam);
      signal(20,obslugam);
  
      sigset_t mask_set;
      sigfillset(&amp;mask_set);
      sigdelset(&amp;mask_set,SIGUSR1);
      sigdelset(&amp;mask_set,SIGTERM);
      sigdelset(&amp;mask_set,SIGCONT);
      sigdelset(&amp;mask_set,SIGTSTP);
      sigprocmask(SIG_BLOCK,&amp;mask_set,NULL);
      printf("Jestem M i odebralem sygnal %d\n",sig);
      if(sig==15){
          write(pm1[1],"15",2);
          write(pm2[1],"15",2);
          write(pm3[1],"15",2);
      }
      if(sig==18){
          write(pm1[1],"18",2);
          write(pm2[1],"18",2);
          write(pm3[1],"18",2);
      }
      if(sig==20){
          write(pm1[1],"20",2);
          write(pm2[1],"20",2);
          write(pm3[1],"20",2);
      }
      kill(PID1,10);
  
  }
  void obsluga1(int sig){
      signal(10,obsluga1);
  
      sigset_t mask_set;
      sigfillset(&amp;mask_set);
      sigdelset(&amp;mask_set,SIGUSR1);
      sigprocmask(SIG_BLOCK,&amp;mask_set,NULL);
      printf("Jestem 1 i odebralem sygnal %d\n",sig);
      char bufor_p1[2]={0};   
      if(read(pm1[0],&amp;bufor_p1,2)==-1)perror("PIPE1\n");
      if(strchr(bufor_p1,53)!=NULL) printf("Jestem 1 i dostałem 15\n");
      else if(strchr(bufor_p1,56)!=NULL) printf("Jestem 1 i dostałem 18\n");
      else if(strchr(bufor_p1,48)!=NULL) printf("Jestem 1 i dostałem 20\n");
      kill(PID2,10);
  }
  void obsluga2(int sig){
      signal(10,obsluga2);
      signal(15,obsluga2);
      signal(18,obsluga2);
      signal(20,obsluga2);
  
      sigset_t mask_set;
      sigfillset(&amp;mask_set);
      sigdelset(&amp;mask_set,SIGUSR1);
      sigdelset(&amp;mask_set,SIGTERM);
      sigdelset(&amp;mask_set,SIGCONT);
      sigdelset(&amp;mask_set,SIGTSTP);
      sigprocmask(SIG_BLOCK,&amp;mask_set,NULL);
      printf("Jestem 2 i odebralem sygnal %d\n",sig);
      if(sig==10){
          char bufor_p2[2]={0};
          if(read(pm2[0],&amp;bufor_p2,2)==-1)perror("PIPE2\n");
          if(strchr(bufor_p2,53)!=NULL) printf("Jestem 2 i dostałem 15\n");
          else if(strchr(bufor_p2,56)!=NULL) printf("Jestem 2 i dostałem 18\n");
          else if(strchr(bufor_p2,48)!=NULL) printf("Jestem 2 i dostałem 20\n");  
          kill(PID3,10);
      }
      else if(sig==15) kill(getppid(),15);
      else if(sig==18) kill(getppid(),18);
      else if(sig==20) kill(getppid(),20);
  }
  void obsluga3(int sig){
      signal(10,obsluga3);
  
      sigset_t mask_set;
      sigfillset(&amp;mask_set);
      sigdelset(&amp;mask_set,SIGUSR1);
      sigprocmask(SIG_BLOCK,&amp;mask_set,NULL);
      printf("Jestem 3 i odebralem sygnal %d\n",sig);
      char bufor_p3[2]={0};
      if(read(pm3[0],&amp;bufor_p3,2)==-1)perror("PIPE3\n");
      if(strchr(bufor_p3,53)!=NULL) printf("Jestem 3 i dostałem 15\n");
      else if(strchr(bufor_p3,56)!=NULL) printf("Jestem 3 i dostałem 18\n");
      else if(strchr(bufor_p3,48)!=NULL) printf("Jestem 3 i dostałem 20\n");
  }
  
  int main(){
  
      (...)
  
      pipe(pm1);
      pipe(pm2);
      pipe(pm3);
      signal(15,obslugam);
      signal(18,obslugam);
      signal(20,obslugam);
  
      if((PID1=fork())==0) {
          close(pm1[1]);
          signal(10,obsluga1);
      }
      else if((PID2=fork())==0) {
          close(pm2[1]);
          signal(10,obsluga2);
          signal(15,obsluga2);
          signal(18,obsluga2);
          signal(20,obsluga2);
      }
      else if((PID3=fork())==0){
          printf("\nJestem procesem nr 2. Oto moj PID: %d\n\n",PID2);
          close(pm3[1]);
          signal(10,obsluga3);
      }
      else{
          close(pm1[0]);
          close(pm2[0]);  
          close(pm3[0]);
      }
  
      (...)
  
  }
  </code></pre>
  
  <p>I try but I can't find a place where I made a mistake. I will be grateful for your help.</p>