5

要するに、この疑似例が説明する行に沿って、ストリームクラスからのパイプからバッファリングされた読み取りを行うことは可能ですか?

表示されるペダンティックな問題 (エラーをチェックしないなど) は無視してください。私は実際のコードでそれをすべて行っています。これは私の質問を理解するための単なる疑似例です。

#include <iostream> // or istream, ifstream, strstream, etc; whatever stream could pull this off
#include <unistd.h>
#include <stdlib.h>
#include <sstream>

void myFunc() {
  int pipefd[2][2] = {{0,0},{0,0}};
  
  pipe2( pipefd[0], O_NONBLOCK );
  pipe2( pipefd[1], O_NONBLOCK );

  if( 0 == fork() ) {
    close( pipefd[0][1] );
    close( pipefd[1][1] );
    dup2( pipefd[0][0], stdout );
    dup2( pipefd[1][0], stderr );
    execv( /* some arbitrary program */ );
  } else {
    close( pipefd[0][0] );
    close( pipefd[1][0] );

    /* cloudy bubble here for the 'right thing to do'.
     * Obviously this is faulty code; look at the intent,
     * not the implementation.
     */
#ifdef RIGHT_THING_TO_DO
    for( int ii = 0; ii < 2; ++ii ) {
      cin.tie( pipefd[ii][1] );
      do {
        cin.readline( /* ... */ );
      } while( /* ... */ );
    }
#else
    // This is what I'm doing now; it works, but I'm
    // curious whether it can be done more concisely
    do {
      do {
        select( /* ... */ );
        for( int ii = 0; ii < 2; ++ii ) {
          if( FD_SET( fd[ii][1], &rfds ) ) {
            read( fd[ii][1], buff, 4096 );
            if( /* read returned a value > 0 */ ) {
              myStringStream << buff;
            } else {
              FD_CLR( fd[ii][1], &rfds );
            }
          }
        }
      } while( /* select returned a value > 0 */ );
    } while( 0 == waitpid( -1, 0, WNOHANG ) );
#endif
  }
}

編集

boost::file_descriptorこれは、パイプを操作するために使用する方法の簡単な例です。ソケットでも動作するはずですが、テストしませんでした。

これは私がそれをコンパイルした方法です:

g++ -m32 -DBOOST_IOSTREAMS_NO_LIB -isystem ${BOOST_PATH}/include \
  ${BOOST_SRC_PATH}/libs/iostreams/src/file_descriptor.cpp blah.cc -o blah

次に例を示します。

#include <fcntl.h>
#include <stdio.h>

#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>

int main( int argc, char* argv[] ) {
  // if you just do 'using namespace...', there's a
  // namespace collision with the global 'write'
  // function used in the child
  namespace io = boost::iostreams;

  int pipefd[] = {0,0};
  pipe( pipefd, 0 );  // If you use O_NONBLOCK, you'll have to
                      // add some extra checks to the loop so
                      // it will wait until the child is finished.

  if( 0 == fork() ) {
    // child
    close( pipefd[0] ); // read handle
    dup2( pipefd[1], FILENO_STDOUT );
    printf( "This\nis\na\ntest\nto\nmake sure that\nit\nis\working as expected.\n" );
    return 0; // ya ya, shoot me ;p
  }

  // parent

  close( pipefd[1] ); // write handle

  char *buff = new char[1024];
  memset( buff, 0, 1024 );

  io::stream<io::file_descriptor_source> fds(
    io::file_descriptor_source( pipefd[0], io::never_close_handle ) );

  // this should work with std::getline as well
  while(   fds.getline( buff, 1024 )
        && fds.gcount() > 0 // this condition is not enough if you use
                            // O_NONBLOCK; it should only bail if this
                            // is false AND the child has exited
       ) {
    printf( "%s,", buff );
  }

  printf( "\n" );
}
4

2 に答える 2

5

確かにあります。ファイル記述子 (pipe() から取得したものなど) をラップする std::streambuf の作成方法については、書籍「The C++ Standard Library: a Tutorial and Reference」の例があります。その上にストリームを作成することは簡単です。

編集: ここに本があります: http://www.josuttis.com/libbook/

ファイル記述子を使用した出力バッファの例を次に示します: http://www.josuttis.com/libbook/io/outbuf2.hpp.html

また、ここに入力バッファの例があります: http://www.josuttis.com/libbook/io/inbuf1.hpp.html

于 2012-08-24T17:58:28.423 に答える
1

既存のファイル記述子で作成できるストリーム、またはパイプ自体を作成するストリームが必要です。残念ながら、そのような標準のストリーム タイプはありません。

独自に作成するか、たとえばboost::iostreams::file_descriptorを使用できます。

独自に記述するには、basic_streambuf のサブクラスを作成してから、basic_i/ostream の非常に単純なサブクラスを作成する必要があります。このサブクラスは、streambuf クラスを保持し、便利なコンストラクターを提供するだけです。

于 2012-08-24T17:56:55.477 に答える