title: FIFO: One process never reads from pipe
body: <p>I'm following <a href="https://www.tutorialspoint.com/inter_process_communication/inter_process_communication_named_pipes.htm" rel="nofollow noreferrer">THIS</a> TutorialsPoint guide to Linux Piping, and I specifically need to use FIFOs.</p>
  
  <p>However, the code doesn't work at all for the server side.</p>
  
  <p>The server file either hangs indefinitely or it reads nothing, while the client instead writes on the FIFO and immediately reads it has just written.</p>
  
  <p>Here's the full code for both files in case you don't want to go through TutorialsPoint:</p>
  
  <p><strong>fifoserver_twoway.cpp</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  #define FIFO_FILE "/tmp/fifo_twoway"
  
  void reverse_string(char *);
  int main() {
     int fd;
     char readbuf[80];
     char end[10];
     int to_end;
     int read_bytes;
  
     /* Create the FIFO if it does not exist */
     mkfifo(FIFO_FILE, S_IFIFO|0640);
     strcpy(end, "end");
     fd = open(FIFO_FILE, O_RDWR);
     while(1) {
        read_bytes = read(fd, readbuf, sizeof(readbuf));
        readbuf[read_bytes] = '\0';
        printf("FIFOSERVER: Received string: \"%s\" and length is %d\n", readbuf, (int)strlen(readbuf));
        to_end = strcmp(readbuf, end);
  
        if (to_end == 0) {
           close(fd);
           break;
        }
        reverse_string(readbuf);
        printf("FIFOSERVER: Sending Reversed String: \"%s\" and length is %d\n", readbuf, (int) strlen(readbuf));
        write(fd, readbuf, strlen(readbuf));
        /*
        sleep - This is to make sure other process reads this, otherwise this
        process would retrieve the message
        */
        sleep(2);
     }
     return 0;
  }
  
  void reverse_string(char *str) {
     int last, limit, first;
     char temp;
     last = strlen(str) - 1;
     limit = last/2;
     first = 0;
  
     while (first &lt; last) {
        temp = str[first];
        str[first] = str[last];
        str[last] = temp;
        first++;
        last--;
     }
     return;
  }
  </code></pre>
  
  <p><strong>fifoclient_twoway.cpp</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  #define FIFO_FILE "/tmp/fifo_twoway"
  int main() {
     int fd;
     int end_process;
     int stringlen;
     int read_bytes;
     char readbuf[80];
     char end_str[5];
     printf("FIFO_CLIENT: Send messages, infinitely, to end enter \"end\"\n");
     fd = open(FIFO_FILE, O_CREAT|O_RDWR);
     strcpy(end_str, "end");
  
     while (1) {
        printf("Enter string: ");
        fgets(readbuf, sizeof(readbuf), stdin);
        stringlen = strlen(readbuf);
        readbuf[stringlen - 1] = '\0';
        end_process = strcmp(readbuf, end_str);
  
        //printf("end_process is %d\n", end_process);
        if (end_process != 0) {
           write(fd, readbuf, strlen(readbuf));
           printf("FIFOCLIENT: Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
           read_bytes = read(fd, readbuf, sizeof(readbuf));
           readbuf[read_bytes] = '\0';
           printf("FIFOCLIENT: Received string: \"%s\" and length is %d\n", readbuf, (int)strlen(readbuf));
        } else {
           write(fd, readbuf, strlen(readbuf));
           printf("FIFOCLIENT: Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
           close(fd);
           break;
        }
     }
     return 0;
  }
  </code></pre>
  
  <p>When I run both processes, this is what I get:</p>
  
  <p>./fifoserver_twoway</p>
  
  <pre><code>FIFOSERVER: Received string: "" and length is 0
  FIFOSERVER: Sending Reversed String: "" and length is 0
  FIFOSERVER: Received string: "" and length is 0
  FIFOSERVER: Sending Reversed String: "" and length is 0
  </code></pre>
  
  <p>./fifoclient_twoway</p>
  
  <pre><code>FIFOCLIENT: Sent string: "ciao" and string length is 4
  FIFOCLIENT: Received string: "ciao" and length is 4
  Enter string: why won't you reverse?
  FIFOCLIENT: Sent string: "why won't you reverse?" and string length is 29
  FIFOCLIENT: Received string: "why won't you reverse?" and length is 29
  </code></pre>
  
  <p>It's also worth noting that before starting to write this question, the server behaviour was completely different: instead of receiving nothing and printing like you see here, it would hang indefinitely after the "read" (and I haven't changed the code one bit, except for changing the FIFO_FILE path)</p>