Doxygen Source Code Documentation
wrap.c File Reference
#include "thd_iochan.h"
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
Go to the source code of this file.
Defines | |
#define | NBUF 2048 |
#define | DELAY 1 |
Functions | |
int | main (int argc, char *argv[]) |
Define Documentation
|
Definition at line 12 of file wrap.c. Referenced by main(). |
|
Definition at line 11 of file wrap.c. Referenced by main(). |
Function Documentation
|
Definition at line 14 of file wrap.c. References argc, COX_clock_time(), DELAY, iochan_clearcheck(), IOCHAN_CLOSE, iochan_init(), iochan_readcheck(), iochan_recv(), iochan_sendall(), iochan_writecheck(), NBUF, pclose, and popen.
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 ){ /* I'm the child */ 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) ; /* check if ready */ 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 { /* I'm the parent */ 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) ; /* check if ready */ 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 } |