title: headache with named pipes and forks
body: <p>I need to write program that have construction like this:</p>
  
  <p>Parent makes fifo, then <code>fork()</code></p>
  
  <ul>
  <li>child 1 reads message from stdin and writes it to named pipe (FIFO)</li>
  <li>then in parent process I need to create pipe (unnamed) and another <code>fork()</code></li>
  <li>child number 2 reades from FIFO, counts length of message and send number to parent via pipe(unnamed).</li>
  </ul>
  
  <p>I created a simple program with one fork where child can communicate with parent:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  
  #define FIFO "/tmp/my_fifo"
  
  int main()
  {
  pid_t fork_result;
  int pipe_fd;
  int res;
  char writer[3];
  char reader[3];
  
  res = mkfifo(FIFO,0777);
  if (res == 0)
  {
      printf("FIFO created!\n");
  
      fork_result = fork();
      if (fork_result == -1)
      {
          fprintf(stderr, "fork error");
          exit(EXIT_FAILURE);
      }       
      if (fork_result == 0)
      {
          printf("CHILD 1\n");
          pipe_fd = open(FIFO, O_WRONLY | O_NONBLOCK);
          scanf("%s", writer);
          res = write(pipe_fd,writer,3);
          if (res == -1)
          {
              fprintf(stderr,"error writing fifo\n");
              exit(EXIT_FAILURE);
          } 
          (void)close(pipe_fd);
          exit(EXIT_SUCCESS);
      }
      else
      {
          printf("PARENT\n");
          pipe_fd = open(FIFO, O_RDONLY);
          res = read(pipe_fd, reader, 3);
          printf("reader: 0: %c\n",reader[0]);
          printf("reader: 1: %c\n",reader[1]);
          printf("reader: 2: %c\n",reader[2]);
          (void)close(res);
      }         
  }
  else
  {
      printf("deleting fifo... run program again!\n");
      unlink(FIFO);
  }
  exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>and it is working very well. So I created code that have architecture described above:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  
  #define FIFO "/tmp/my_fifo"
  
  int main()
  {
  pid_t fork_result;
  pid_t fork_result2;
  int pipe_fd;
  int res;
  char writer[3];
  char reader[3];
  
  res = mkfifo(FIFO,0777);
  if (res == 0)
  {
      printf("FIFO created!\n");
      fork_result = fork();
  
      if (fork_result == -1)
      {
          fprintf(stderr, "fork error");
          exit(EXIT_FAILURE);
      }     
  
      if (fork_result == 0)
      {
          printf("CHILD 1\n");
          pipe_fd = open(FIFO, O_WRONLY | O_NONBLOCK);
          scanf("%s", writer);
          res = write(pipe_fd,writer,3);
          if (res == -1)
          {
              fprintf(stderr,"error writing to fifo\n");
              exit(EXIT_FAILURE);
          } 
          (void)close(pipe_fd);
      exit(EXIT_SUCCESS);
      }
      else
      {
          printf("PARENt 1\n");
          //don't forget pipe!
  
          fork_result = fork();
          pipe_fd = open(FIFO, O_RDONLY);
          if (fork_result == 0)
          {
             printf("CHILD 2\n");
             res = read(pipe_fd, reader, 3);
             printf("Odczytano: 0: %c\n",reader[0]);
             printf("Odczytano: 1: %c\n",reader[1]);
             printf("Odczytano: 2: %c\n",reader[2]);
             (void)close(res);
          } 
      }         
  }
  else
  {
      printf("deleting fifo\n");
      unlink(FIFO);
  }
  exit(EXIT_SUCCESS);
  } 
  </code></pre>
  
  <p>Running sequence is like this:</p>
  
  <pre><code>PARENT 1
  CHILD 1
  CHILD 2
  </code></pre>
  
  <p>so in Parent 1 I'm opening FIFO to read, in child 1 I'm writing to FIFO and child 2 should read it. I mean in code because when I run it I can't even write anything to FIFO. In blocks in <code>scanf("%s", writer);</code> which worked in first program.</p>
  
  <p>Am I using <code>open()</code> correctly? Do I need to use <code>getpid()</code> somewhere? Why it's blocking when I try to write to fifo.</p>