title: FIFO files and read/write
body: <p>I've been trying to get some insight on FIFO and this low level I/O before my laboratories on monday and I came to this situation I don't quite understand.</p>
  
  <p><strong>The program should:</strong></p>
  
  <p>Server:</p>
  
  <ul>
  <li>creates FIFOs,</li>
  <li>sends 5 messages: "Message #i", with interval of 5 sec,</li>
  <li>deletes FIFOs,</li>
  </ul>
  
  <p>Client:</p>
  
  <ul>
  <li>reads from FIFO and displays the message,</li>
  <li>terminates if there was no msg for 6 seconds,</li>
  </ul>
  
  <p>And it does communicate however, client is displaying not exactly what I did send him, and more importantly, seems to be reading from the beginning every time a new msg arrives. I've been trying to figure out, for quite a while, and it just doesn't seem to match what the documentation said... Please help! :(</p>
  
  <p><strong>server</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;  
  #include &lt;string.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char* argv[])
  {       
      int s2c, c2s, i; 
      char fifo_name1[] = "/tmp/fifo1";
      char fifo_name2[] = "/tmp/fifo2";
      char msg[80], buf[10];
      struct stat st;
  
      // if no fifos, create 'em
      if (stat(fifo_name1, &amp;st) != 0)
          mkfifo(fifo_name1, 0666);
      if (stat(fifo_name2, &amp;st) != 0)
          mkfifo(fifo_name2, 0666);
  
      s2c= open(fifo_name1, O_WRONLY);
      c2s= open(fifo_name2, O_RDONLY);
  
      // start sending messages, with 5s interval
      for (i=0; i&lt;5; i++)
      {
          printf("Message #%d \n", i);
  
          strcat(msg, "Message #"); 
          strcat(msg, itoa(i, buf, 10));
          strcat(msg, "\0"); 
  
          write(s2c, msg, strlen(msg)+1);
  
          sleep(5);
      }
  
      // delete fifos
      unlink(fifo_name1);
      unlink(fifo_name2);
      printf("server exit successfully");
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p><strong>client</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char* argv[])
  {
      int c2s, s2c, c=0;
      char buf[10];
  
      char fifo_name1[] = "/tmp/fifo1";
      char fifo_name2[] = "/tmp/fifo2";
      s2c= open(fifo_name1, O_RDONLY);
      c2s= open(fifo_name2, O_WRONLY);
  
      // receive messages
      while (1)
      {
          if (read(s2c, &amp;buf, sizeof(char)*10) &gt; 0)
          {
              printf("%s \n", buf);
              c=0;
          }
          sleep(1);
          c++;    
          if (c&gt;6) 
              break;
      }
  
      printf("client exit successfully");
      return EXIT_SUCCESS;
  }       
  </code></pre>