/*************************************************************************** * Copyright (C) 2001 by Rick L. Vinyard, Jr. * * rvinyard@cs.nmsu.edu * * * * This file is part of the conexus library. * * * * The conexus library is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License version 3 * * as published by the Free Software Foundation. * * * * The conexus library 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 General Public License * * along with this software. If not see <http://www.gnu.org/licenses/>. * ***************************************************************************/ #include <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[] ) { const char* data = "Hello World!!!"; Conexus::init(); Conexus::Pipe::pointer pipe = Conexus::Pipe::create(); Conexus::Data d; pid_t cpid; if ( argc > 1 ) data = argv[1]; 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[0]; } catch ( Conexus::exception::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 ( data, strlen ( data ) ); pipe->close(); /* Reader will see EOF */ wait ( NULL ); /* Wait for child */ exit ( EXIT_SUCCESS ); } }