title: How to use FIFO with dup2?
body: <p>I have 2 processes, one reads from the input a filename, this filename is then given to the child process.The child process determinates in which directories the given file exits and the child procces gives all the directory names back to the parent who then prints these directory names.I need to do this using FIFO(named pipes).I put the filename directly to systemcall to check if the FIFO would work with it but it doesn't work and I cant't figure out how to do this could someone please help?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include&lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;dirent.h&gt;
  //#include &lt;string.h&gt;
  extern int errno;
  
  #define FIFO &quot;/tmp/fifo0003.1&quot;
  #define FIFO2 &quot;/tmp/fifo0004.1&quot;
  int main (void)
  {   
  
  int r_fifo, w_fifo, r_fifo2, w_fifo2;
     char filename[100];
     printf(&quot;enter filename:&quot;);
     scanf(&quot;%s&quot;,filename);
     char buf[100];
     char buf2[100];
     int fd;
     pid_t pid;
     int stdoutCopy=dup(1);
    if ((mkfifo (FIFO, S_IRUSR | S_IWUSR)) == -1) {
      if(errno == EEXIST)
  
   perror (&quot;mkfifo()&quot;);
   else {
  perror(&quot;mkfifo()&quot;);
      exit (EXIT_FAILURE);
   }   }
  
  
  if ((mkfifo (FIFO2, S_IRUSR | S_IWUSR)) == -1) {
      if(errno == EEXIST)
  
   perror (&quot;mkfifo()&quot;);
   else {
  perror(&quot;mkfifo()&quot;);
      exit (EXIT_FAILURE);
   }   }
  
   pid = fork ();
    if (pid == -1)
   {      perror (&quot;fork()&quot;);
    exit (EXIT_FAILURE);
  }
  else if (pid &gt; 0) {
  /*parent writes to fifo */
   if ((w_fifo = open (FIFO, O_WRONLY)) &lt; 0) {
   perror (&quot;open()&quot;);
   exit (EXIT_FAILURE);      }
   write (w_fifo, filename, strlen (filename));
  
  /*wait for child */
   while (wait (NULL) != pid);
    /*read FIFO2 */
   if ((r_fifo2 = open (FIFO2, O_RDONLY)) &lt; 0) {
   perror (&quot;open()&quot;);
   exit (EXIT_FAILURE);      }
  //redo stdout
  dup2(stdoutCopy,1);
  close(stdoutCopy);
  read(r_fifo2,buf,strlen(buf));
   buf[strlen(buf)] = '\0';
  printf(&quot;%s\n&quot;,buf);
   else {
       /*child procces */
    if ((r_fifo = open (FIFO, O_RDONLY)) &lt; 0) {
   perror (&quot;open()&quot;);
   exit (EXIT_FAILURE);      }
   /*read FIFO */
     read (r_fifo, filename, strlen (filename));
   if ((w_fifo2 = open (FIFO2, O_WRONLY)) &lt; 0) {
   perror (&quot;open()&quot;);
   exit (EXIT_FAILURE);      }
  
    dup2(w_fifo2,1);
    system(&quot;find -name file1&gt;w_fifo2&quot;);
    close(w_fifo2);
     /* EOF */
   exit (EXIT_SUCCESS);}
   return EXIT_SUCCESS;}
  </code></pre>