Pipe Endpoints

Anonymous Pipe Example

/***************************************************************************
*   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
*   rvinyard@cs.nmsu.edu                                                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU Lesser General Public License as        *
*   published by the Free Software Foundation version 2.1.                *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU Lesser General Public      *
*   License along with this library; if not, write to the                 *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
***************************************************************************/

#include <conexus/conexus.h>
#include <iostream>

#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

// This is the canonical pipe example program found in man (2) pipe
// rewritten to illustrate pipes in the Conexus library

int
main(int argc, char *argv[])
{
  Conexus::init();
  
  Conexus::Pipe pipe;
  Conexus::Data d;
  pid_t cpid;

  assert(argc == 2);

  pipe.open();

  cpid = fork();
  if (cpid == -1)
    {
      perror("fork");
      exit(EXIT_FAILURE);
    }

  if (cpid == 0)
    { /* Child reads from pipe */
      pipe.close_write(); /* Close unused write end */
      try {
      while ( d = pipe.read(1) )
        std::cout << d.raw_data()[0];
      } catch ( Conexus::error::read::disconnected ) { }
      std::cout << std::endl;
      _exit(EXIT_SUCCESS);
    }
  else
    { /* Parent writes argv[1] to pipe */
      pipe.close_read(); /* Close unused read end */
      pipe.write(argv[1], strlen(argv[1]));
      pipe.close();          /* Reader will see EOF */
      wait(NULL);             /* Wait for child */
      exit(EXIT_SUCCESS);
    }
}

Anonymous Pipe Server Example

/***************************************************************************
*   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
*   rvinyard@cs.nmsu.edu                                                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU Lesser General Public License as        *
*   published by the Free Software Foundation version 2.1.                *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU Lesser General Public      *
*   License along with this library; if not, write to the                 *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
***************************************************************************/

#include <conexus/conexus.h>
#include <iostream>

#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

// This is the canonical pipe example program found in man (2) pipe
// rewritten to illustrate pipes in the Conexus library

void print_data(Conexus::Data d);

int
main(int argc, char *argv[])
{
  Conexus::init();
  
  Conexus::Pipe pipe;
  pid_t cpid;

  assert(argc == 2);

  pipe.open();

  cpid = fork();
  if (cpid == -1)
    {
      perror("fork");
      exit(EXIT_FAILURE);
    }

  if (cpid == 0)
    { /* Child reads from pipe */
      pipe.close_write(); /* Close unused write end */
      pipe.signal_data().connect( sigc::ptr_fun( &print_data ) );
      pipe.start();
      while ( pipe.is_running() )
        usleep(10000);

      _exit(EXIT_SUCCESS);
    }
  else
    { /* Parent writes argv[1] to pipe */
      pipe.close_read(); /* Close unused read end */

      /* Write the input string twice with a 3 second delay */
      pipe.write(argv[1], strlen(argv[1]));
      pipe.write("\n", 2);
      sleep(3);
      pipe.write(argv[1], strlen(argv[1]));
      pipe.write("\n", 2);

      pipe.close();          /* Reader will see EOF */
      wait(NULL);             /* Wait for child */
      exit(EXIT_SUCCESS);
    }
}

void print_data( Conexus::Data d ) {
  std::cout << "data: ";
  for ( unsigned i = 0; i < d.size(); i++ )
    std::cout << d.raw_data()[i];
  std::cout << std::flush;
}

Generated on Sat Aug 26 17:34:53 2006 by  doxygen 1.4.6