title: sending signal between two child process
body: <p>I create two children from the parent in the main program. The first and second child executes a program (signalSender) after their creation(alongside with the pid of the other child as an argument). signalSender has signal handler and is used for sending signal between process. The pid of the second child is given as zero as argument when first child executes signalShooter. The pid of the first child is given as argument when the second child executes sigShooter. </p>
  
  <p>1) I want to find the pid of the first child via signal handler after second child sends the signal to first child. I tried to save it to global variable pid_t pid2 but it does not work.</p>
  
  <p>2)I also have to send signal between these two children 100 times but i dont know where to use 'for loop' and ‘wait’ signal.</p>
  
  <pre><code>The main program:
  
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;signal.h&gt;
  
  int main()
  {
      pid_t pid1,pid2,wid;
    char *my_args[5];
    int aInt = 368
       char str[15];
     pid1 = fork();
         if (pid1 &lt; 0)
      {
        fprintf(stderr, ": fork failed: %s\n", strerror(errno));
  
        exit(1);
  
      }
      if(pid1 == 0)
      {
        my_args[0] = "sigperf1";
        my_args[1] = "0";
        my_args[2] = NULL;
        execv("signalSender",my_args);
        fprintf(stderr,"signalSender cannot be executed...");
        exit(-1);
      }
  
      pid2 = fork();
  
      if(pid2 &lt; 0)
      {
         fprintf(stderr, ": fork failed: %s\n", strerror(errno));
         exit(1);
      }
  
      if(pid2 == 0)
      {
        sprintf(str, "%d", pid1);
        my_args[0] = "sigperf1";
        my_args[1] = str;
        my_args[2] = NULL; 
       // printf("this is converted = %s\n",my_args[1]);
        execv(“signalSender",my_args);
        fprintf(stderr,"signalSender cannot be executed...");
        exit(-1);
      }
  wid = wait(NULL);
  
  }
  </code></pre>
  
  <p>The signalSender:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;ctype.h&gt;
  
  pid_t pid2;
  struct sigaction act;
  
  void sighandler(int signum, siginfo_t *info, void *ptr)
  {
      printf("Received signal %d\n", signum);
      printf("Signal originates from process %lu\n",
          (unsigned long)info-&gt;si_pid);
          pid2 = info-&gt;si_pid;
  }
  
  int main(int argc,char **argv)
  { 
      memset(&amp;act, 0, sizeof(act));
      act.sa_sigaction = sighandler;
      act.sa_flags = SA_SIGINFO;
      sigaction(SIGUSR1, &amp;act, NULL);
  
      pid_t current, pidOther;
      current = getpid();
      pidOther = atol(argv[1]);
  
      if(pidOther != 0) // we are in the second child
      {
          kill(pidOther,SIGUSR1); //sending signal from second child to first 
      }
  
      if(pidOther == 0) // we are in the first child 
      {
          kill(pid2,SIGUSR1);
      }
  
      return 0;
  }
  </code></pre>