title: Read/write from/to FIFO
body: <p>I've been trying to implement fifo write and read, the scenario is like so, writer1 write 4 bytes to a fifo, reader1 read 2 bytes of them and reader2 read the next 2 bytes, below is what I have done,</p>
  
  <p>writer.c</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/stat.h&gt;
  #include&lt;string.h&gt;
  int main()
  {
  FILE *file;
  unsigned char message[] = {0x66,0x66,0x67,0x67};
  file = fopen("fifo1","wb");
  fwrite(&amp;message, 1,4,file);
  }
  </code></pre>
  
  <p>reader.c</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/stat.h&gt;
  #include&lt;string.h&gt;
  int main()
  {
  FILE *file;
  unsigned char buff[2];
  file = fopen("fifo1","rb");
  fread(&amp;buff, 1,2,file);
  
  printf("%c\n",buff[0]);printf("%c\n",buff[1]);
  }
  </code></pre>
  
  <p>Then I complied both of them and ran reader1 on first terminal and reader2 on the second terminal and writer on the third terminal.</p>
  
  <p>I thought that I would get the first two bytes (ff) in one of the readers and the second two bytes (gg) on the other, but it didn't work as I thought it would, can someone please let me know what I did wrong, please note that I don't care who read the first two bytes or the second two bytes, what matters here is that both readers read2 bytes at a time. I'm using Ubuntu, GCC mkfifo to create the fifo.</p>