1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ Named Pipes

Discussion in 'C++' started by georgea, Jun 24, 2010.

  1. georgea

    georgea New Member

    Jun 24, 2010
    Likes Received:
    Trophy Points:
    This all started with my need to work with named pipes to have a C++ app communicate with a Java app running on the same file system. BTW, this is will be running on a Solaris box. In thsi context the C++ app would write to the pipe while the Java app would read from the pipe.

    Another requirement was that I needed the pipes to be non blocking as to avoid any hangs if the reader was not up. I coded thsi up and it works as I expect but I sometimes get weird behavior. It presents in the form where I only read partial data off of the pipe. I am concerned that my producer is producing messages faster than they are being consumed and that as i reach the capacity of the pipe I only write partial data. I do look EAGAIN and EBUSY and go to sleep for a short while if those conditions are encountered.

    I was hoping you guys could take a look at how i create and write to my pipe and see if something is wrong. The below code is partial.

    (Apologise in advance, i don't know how to add code snippets, looked at FAQ briefly but was in a rush to get this out there)
      // Create the pipe the stream is going to output data to.
      int ret_val = mkfifo( (char*)sDataFile.c_str(), 0666 );
      //Get a file descriptor  sPath is the named pipe
      int nFD = open( (char*)file->sPath.c_str(), O_CREAT | O_WRONLY | O_NONBLOCK | O_TRUNC,  S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
      //Get a FILE object from fiel descriptor
      FILE* out = fdopen( nFD, "a" );
      while( have data to send ){
         //sData is the data to write to pipe
         fprintf( out, "%s\n", (char*)sData.c_str() );
         fflush( out );
          //Retries if EAGAIN or EBUSY
      //Close the file using
      fclose( out );
    Should I be using write instead of fprintf? If the pipe is somewhat full and the next piece of data will overflow it, is that data only partially written or is the write atomic (either its all written or none of it is written).
  2. NewsBot

    NewsBot New Member

    Dec 2, 2008
    Likes Received:
    Trophy Points:

Share This Page