Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
wrap.c
Go to the documentation of this file.00001 #include "thd_iochan.h"
00002
00003 #ifdef SPARKY
00004 #undef _POSIX_SOURCE
00005 #endif
00006
00007 #include <sys/types.h>
00008 #include <unistd.h>
00009 #include <sys/wait.h>
00010
00011 #define NBUF 2048
00012 #define DELAY 1
00013
00014 int main( int argc , char * argv[] )
00015 {
00016 pid_t child_pid ;
00017
00018 child_pid = fork() ;
00019 if( child_pid == (pid_t)(-1) ){fprintf(stderr,"Cannot fork!\n");exit(1);}
00020
00021 if( child_pid == 0 ){
00022 FILE * fp ;
00023 char buf[NBUF] = "\0" ;
00024 int nbuf=0 , jj ;
00025 IOCHAN * ioc ;
00026
00027 fp = popen("rsh 3T60 -l scan ./wrap" , "r") ;
00028 if( fp == NULL ){fprintf(stderr,"Cannot popen!\n");exit(1);}
00029
00030 while( fgets(buf+nbuf,NBUF-nbuf,fp) != NULL ){
00031 nbuf = strlen(buf) ;
00032 }
00033 pclose(fp) ; fprintf(stderr,"Child has data\n") ;
00034
00035 ioc = iochan_init( "shm:fred:4K" , "w" ) ;
00036 if( ioc == NULL ){fprintf(stderr,"Child cannot iochan_init!\n");exit(1);}
00037
00038 while(1){
00039 jj = iochan_writecheck(ioc,DELAY) ;
00040 if( jj > 0 ) break ;
00041 if( jj < 0 ){fprintf(stderr,"Child IOCHAN is now bad!\n");exit(1);}
00042 fprintf(stderr,".") ; fflush(stderr) ;
00043 }
00044 iochan_sendall( ioc , buf , nbuf ) ;
00045 while( ! iochan_clearcheck(ioc,DELAY) ){fprintf(stderr,"c"); fflush(stderr);}
00046 fprintf(stderr,"Child exiting\n") ;
00047 IOCHAN_CLOSE(ioc) ;
00048
00049 } else {
00050 IOCHAN * ioc ;
00051 int jj , nbuf ;
00052 char buf[NBUF] = "\0" ;
00053 double ct ;
00054
00055 ct = COX_clock_time() ;
00056 fprintf(stderr,"*** Parent waiting ***\n") ;
00057 ioc = iochan_init( "shm:fred:4K" , "r" ) ;
00058 if( ioc == NULL ){fprintf(stderr,"Parent cannot iochan_init!\n");exit(1);}
00059
00060 while(1){
00061 jj = iochan_readcheck(ioc,DELAY) ;
00062 if( jj > 0 ) break ;
00063 if( jj < 0 ){fprintf(stderr,"Parent IOCHAN is now bad!\n");exit(1);}
00064 }
00065 fprintf(stderr,"Parent receiving\n") ;
00066 jj = iochan_recv( ioc , buf , NBUF ) ;
00067 IOCHAN_CLOSE(ioc) ;
00068 ct = COX_clock_time() - ct ;
00069 fprintf(stderr,"Parent got %d bytes in %6.2f seconds:\n%s",jj,ct,buf) ;
00070 (void) wait(NULL) ;
00071 }
00072
00073 exit(0) ;
00074 }