title: child-process signal handling in c
body: <p>I need your help in an exercise i have about signal handling between processes. I think that it's a trivial question but i can't find the answer anywhere. I want to print something from the parent in a file, send a signal from the parent to the child, the child has to print in a file and then send a signal from the child to the parent.</p>
  
  <p>The code i am using is this: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  
  #define WRITEF 123451  //Random number
  
  FILE *infile;
  
  void writef() {
      fprintf(infile, "Child Starting (%d)\n", getpid());
      printf("Child Starting (%d)\n", getpid());
  }
  
  int main() {
      pid_t pid;
  
      infile = fopen("pid_log.txt","w");
      pid = fork();
      signal(WRITEF, writef);
      if ( pid == 0 ) {
              pause();
              printf("enter child\n");
      }
      else {
              fprintf(infile, "Parent (%d)\n", getpid());
              printf("Parent (%d)\n", getpid());
              kill(pid, WRITEF);
              pause();
              wait((int*)1);
      }
  
      fclose(infile);
      return 1;
  }
  </code></pre>