/*************************************************************************** * 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 void print_data(const Conexus::Data d); int main(int argc, char *argv[]) { const char* data = "Hello World!!!"; if ( argc > 1 ) data = argv[1]; Conexus::init(); Conexus::Pipe::pointer pipe = Conexus::Pipe::create(); pid_t cpid; 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(data, strlen(data)); pipe->write("\n", 2); sleep(3); pipe->write(data, strlen(data)); pipe->write("\n", 2); pipe->close(); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } void print_data( const Conexus::Data d ) { for ( unsigned i = 0; i < d.size(); i++ ) std::cout << d[i]; std::cout << std::flush; }