title: IPC: Using of named pipes in c++ between two programs
body: <p>I'm trying to realise a IPC between two different programs running on the same machine (in my case its a CentOS7). To have just a kind of loose coupling I decided to use a named pipe for the IPC. Therefore I'm was playing with the following example and ran into different problems.</p>
  
  <p>Creating and writing into the pipe:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/select.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdlib.h&gt;
  #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;stdio.h&gt;
  #include &lt;string.h&gt;
  
  using namespace std;
  
  main()  {
   int fd;
   char * myfifo = new char [12];
   strcpy(myfifo, "./tmp/myfifo1");
  
   /* create the FIFO (named pipe) */
   mkfifo(myfifo, 0666);
   /* write "Hi" to the FIFO */
   fd = open("./tmp/myfifo1", O_WRONLY ); //open(myfifo, O_WRONLY | O_NONBLOCK);
   if (fd == -1) {
       perror("open");
       return EXIT_FAILURE;
   } 
   printf("File open\n");
   write(fd, "entry [1]", sizeof("entry [1]"));
   sleep(1);
   write(fd, "entry [2]", sizeof("entry [2]"));
   sleep(2);
   write(fd, "entry [3]", sizeof("entry [3]"));
   printf("Content written\n");
   close(fd);
   printf("Connection closed\n");
   /* remove the FIFO */
   unlink(myfifo);
   return 0;
  }
  </code></pre>
  
  <p>Reading the pipe:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/select.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdlib.h&gt;
  #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;string.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  main()  {
   int fd; 
   fd_set set_a;
  
   char * myfifo = new char [12];
   strcpy(myfifo, "./tmp/myfifo1");
   char buffer[1024];
  
   fd = open("./tmp/myfifo1", O_RDONLY | O_NONBLOCK);
   if (fd == -1) {
       perror("open");
       return EXIT_FAILURE;
   } 
   ssize_t bytes;
   size_t total_bytes = 0;
   printf("\nDropped into read pipe\n");
   while(1){
       if((bytes = read(fd, buffer, sizeof(buffer))) &gt; 0){
           std::string message(&amp;buffer[22]);
           total_bytes += (size_t)bytes;
           printf("%i", bytes);
  
           printf("Message: %s\n", message.c_str());
           memset(&amp;buffer[0], 0, sizeof(buffer));
       }else{
           if (errno == EWOULDBLOCK) {
               printf("\ndone reading (%d bytes)\n", (int)total_bytes);
               //break;
           }
           printf("No message\n");
           sleep(2);
       } 
   }
   return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I feel like named pipes are pretty unflexible in its behavior I figured out with my test programs. First of all if no reading process is attached to the fifo pipe all messages except of the last one written to the pipe get lost (or generally speaking only the last message can be read after the reading process is attached to the pipe). If you write multiple messages into the pipe all messages betweem the reading (e.g. polled) will be interpreted as one single message (I'm aware that they can be splitted by \0).   </p>
  
  <p>The main goals for the named pipe is a) system logs and b) kind of user authentication. The asynchronous of named pipes fits perfectly to my need. But anyway, I'm not sure if named pipes are the best solution for IPC between different programs. Also I'm not sure if the behavior descripted above is normal or if I'm using named pipe in a wrong way. I also thought about sockets but then I will run into huge blocking problems.</p>
  
  <p>Thanks for your help.</p>