1 #include "thd_iochan.h"
2
3 #ifdef SPARKY
4 #undef _POSIX_SOURCE
5 #endif
6
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <sys/wait.h>
10
11 #define NBUF 2048
12 #define DELAY 1
13
main(int argc,char * argv[])14 int main( int argc , char * argv[] )
15 {
16 pid_t child_pid ;
17
18 child_pid = fork() ;
19 if( child_pid == (pid_t)(-1) ){fprintf(stderr,"Cannot fork!\n");exit(1);}
20
21 if( child_pid == 0 ){ /* I'm the child */
22 FILE * fp ;
23 char buf[NBUF] = "\0" ;
24 int nbuf=0 , jj ;
25 IOCHAN * ioc ;
26
27 fp = popen("rsh 3T60 -l scan ./wrap" , "r") ;
28 if( fp == NULL ){fprintf(stderr,"Cannot popen!\n");exit(1);}
29
30 while( fgets(buf+nbuf,NBUF-nbuf,fp) != NULL ){
31 nbuf = strlen(buf) ;
32 }
33 pclose(fp) ; fprintf(stderr,"Child has data\n") ;
34
35 ioc = iochan_init( "shm:fred:4K" , "w" ) ;
36 if( ioc == NULL ){fprintf(stderr,"Child cannot iochan_init!\n");exit(1);}
37
38 while(1){
39 jj = iochan_writecheck(ioc,DELAY) ; /* check if ready */
40 if( jj > 0 ) break ;
41 if( jj < 0 ){fprintf(stderr,"Child IOCHAN is now bad!\n");exit(1);}
42 fprintf(stderr,".") ; fflush(stderr) ;
43 }
44 iochan_sendall( ioc , buf , nbuf ) ;
45 while( ! iochan_clearcheck(ioc,DELAY) ){fprintf(stderr,"c"); fflush(stderr);}
46 fprintf(stderr,"Child exiting\n") ;
47 IOCHAN_CLOSE(ioc) ;
48
49 } else { /* I'm the parent */
50 IOCHAN * ioc ;
51 int jj , nbuf ;
52 char buf[NBUF] = "\0" ;
53 double ct ;
54
55 ct = COX_clock_time() ;
56 fprintf(stderr,"*** Parent waiting ***\n") ;
57 ioc = iochan_init( "shm:fred:4K" , "r" ) ;
58 if( ioc == NULL ){fprintf(stderr,"Parent cannot iochan_init!\n");exit(1);}
59
60 while(1){
61 jj = iochan_readcheck(ioc,DELAY) ; /* check if ready */
62 if( jj > 0 ) break ;
63 if( jj < 0 ){fprintf(stderr,"Parent IOCHAN is now bad!\n");exit(1);}
64 }
65 fprintf(stderr,"Parent receiving\n") ;
66 jj = iochan_recv( ioc , buf , NBUF ) ;
67 IOCHAN_CLOSE(ioc) ;
68 ct = COX_clock_time() - ct ;
69 fprintf(stderr,"Parent got %d bytes in %6.2f seconds:\n%s",jj,ct,buf) ;
70 (void) wait(NULL) ;
71 }
72
73 exit(0) ;
74 }
75