title: Named pipe communication in C
body: <blockquote>
    <p>EDIT:  I just realised I've entered a double block (I've first opened
    fifoR on both, but fifoR is the oposite on each). corrected that and
    established communication! (CODE BELOW IS FIXED).</p>
  </blockquote>
  
  <p>I'm trying to create a client-server app, in which the server forks and the child uses named pipes as stdin and stdout to communicate with the client.
  It's my first program utilizing named pipes, so I'm probably missing something basic, but I can't tell what (possibly a bunch of stuff)</p>
  
  <p>Here are my codes (compile with no errors and a similar code but using TCP sockets instead of named pipes is running splendid).</p>
  
  <p>Server Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/wait.h&gt;
  
  #define FIFO_R "./tmp/myfifoR"
  #define FIFO_W "./tmp/myfifoW"
  
  void fatal(const char *msg);
  void child_process(int fdr,int fdw);
  
  int main()
  {
      int fdr,fdw;
      char *myfifor = FIFO_R;
      char *myfifow = FIFO_W;
      int status;
  
      /* create the FIFO (named pipe) */
      if ( mkfifo(myfifor, 0666) &lt; 0 ) 
      {
          fatal("fifoR");
      }
      if ( mkfifo(myfifow, 0666) &lt; 0 )
      {
          fatal("fifoW");
      }
      printf("openning fifoR\n");
      if( (fdr = open(myfifor, O_RDONLY)) &lt; 0 )
          fatal("opening fdr");
      printf("fifoR unblocked\n");
      printf("openning fifoW\n");
      if( (fdw = open(myfifow, O_WRONLY)) &lt; 0 )
          fatal("opening fdw");
      printf("fifoW unblocked\n");
      switch(fork())
      {
          case 0:
              child_process(fdr,fdw);
          case -1:
              fatal("child creation");
          default:    
              printf("parent\n");
              close(fdr);
              close(fdw);
              wait(&amp;status);
              unlink(myfifor);
              unlink(myfifow);
              printf("all clean, exiting\n");
      }
  
      return EXIT_SUCCESS;
  }
  
  void fatal(const char *msg)
  {
      printf("%s\n",msg);
      perror(msg);
      exit(-1);
  }
  
  
  #define APP "./calc/calc"
  void child_process(int fdr,int fdw)
  {
      printf("child\n");
      close(0);
      close(1);
      close(2);
      if( dup(fdr) != 0 || dup(fdw) != 1 || dup(fdw) != 2 )
          fatal("dup sockets");
      execl(APP,"",(char*)NULL);
      fatal("execl");
  }
  </code></pre>
  
  <p>Client Code (FIXED):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  
  #define FIFO_W "./tmp/myfifoR"
  #define FIFO_R "./tmp/myfifoW"
  #define EXIT_CHAR 'q'
  #define MAX_BUF 1024
  
  void fatal(const char *msg)
  {
      perror(msg);
      exit(-1);
  }
  
  int main()
  {   
      int fdr,fdw;
      char *myfifor = FIFO_R;
      char *myfifow = FIFO_W;
      char buff[MAX_BUF];
      char c;
  
  /* the order of this opens was inversed to the order in server, so it resulted in a deadlock */
      printf("openning fifoR\n");
      if( (fdw = open(myfifow, O_WRONLY)) &lt; 0 )
          fatal("opening fdw");
      printf("fifoR unblocked\n");
  /* */
  
      printf("openning fifoW\n");
      if( (fdr = open(myfifor, O_RDONLY)) &lt; 0 )
          fatal("opening fdr");
      printf("fifoW unblocked\n");
      /*  first strings   */
      memset(buff, 0, sizeof(buff));
      read(fdr,buff,sizeof(buff));
      printf("%s\n",buff);
      while( (c = getchar()) != EXIT_CHAR )
      {
          write(fdw,&amp;c,1);
          if(c == '\n')
          {
              memset(buff, 0, sizeof(buff));
              read(fdr,buff,sizeof(buff));
              printf("%s",buff);
          }
      };
      close(fdw);
      close(fdr);
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Thanks very much!</p>