//------------------------------------------------------------------- // trychannel.cpp // // programmer: ALLAN CRUSE // written on: 08 NOV 2004 //------------------------------------------------------------------- #include // for printf() #include // for open() #include // for exit() #include // for fork() #include // for wait() #include "channels.h" // for constants and functions int main( int argc, char **argv ) { // parent process acquires the key to a new channel int key = chan_make(); if ( key < 0 ) { printf( "chan_make\n" ); exit(1); } // parent process then forks a child process switch ( fork() ) { case -1: // fork failed printf( "fork\n" ); exit(1); case 0: // child process if ( chan_open( key ) < 0 ) { printf( "chan_open\n" ); exit(1); } else { char buf[ 80 ] = ""; int nbytes = chan_pend( key, buf, 80 ); if ( nbytes < 0 ) { printf( "chan_pend\n" ); exit(1); } printf( "%d bytes read ", nbytes ); printf( "from channel %d \n", key ); if ( nbytes > 0 ) write( 1, buf, nbytes ); chan_shut( key ); exit(0); } default: // parent process { char msg[] = "Hello from parent\n"; int nbytes = chan_post( key, msg, sizeof( msg ) ); if ( nbytes < 0 ) { printf( "chan_post\n" ); exit(1); } printf( "%d bytes written to channel %d \n", nbytes, key ); wait( NULL ); chan_kill( key ); exit(0); } } }