|
|
1.1 ! root 1: /*** pchild.c - child program ! 2: * ! 3: * Example of DOSMAKEPIPE usage in parent/child communication ! 4: * ! 5: * This is the child program which read the data sent down ! 6: * from the parent in the pipe. ! 7: */ ! 8: #include <stdio.h> ! 9: #include <doscalls.h> ! 10: ! 11: typedef struct { ! 12: unsigned read_handle; /* pipe read handle */ ! 13: unsigned write_handle; /* pipe write handle */ ! 14: } SharedData; ! 15: ! 16: ! 17: main() ! 18: { ! 19: ! 20: static char pname[] = "\\SHAREMEM\\public"; /* shared mem seg name */ ! 21: char *written = "Writing to the child"; /* string in pipe */ ! 22: char readin[21]; /* DosRead input buffer */ ! 23: int retcode; /* holds return code from call */ ! 24: SharedData far *fp; /* pointer to shared memory */ ! 25: unsigned mem_handle; /* selector of the allocated segment */ ! 26: unsigned buflen = 21; /* DosRead buffer length */ ! 27: unsigned read; /* number bytes read by DosRead */ ! 28: ! 29: ! 30: /* access shared memory 'public' */ ! 31: printf("Accessing shared memory\n"); ! 32: retcode = DOSGETSHRSEG((char far *)pname, (unsigned far *)&mem_handle); ! 33: ! 34: /* create pointer to shared memory segment */ ! 35: fp = (SharedData far *)((unsigned long)mem_handle << 16); ! 36: ! 37: /* read from the pipe */ ! 38: printf("Reading from pipe\n"); ! 39: if( retcode = DOSREAD( fp->read_handle, readin, buflen, ! 40: (unsigned far *)&read)) { ! 41: printf("Read from pipe handle %d failed, retcode %d\n", ! 42: fp->read_handle, retcode); ! 43: } ! 44: else { ! 45: printf("DosRead read %d bytes from handle %d, retcode %d\n", ! 46: read, fp->read_handle, retcode); ! 47: ! 48: /* verify the string */ ! 49: if ( retcode = strcmp( written, readin ) ) { ! 50: printf("The child didn't read pipe data correctly"); ! 51: printf(", retcode %d\n", retcode); ! 52: printf("read string %s\n", readin); ! 53: printf("expected string %s\n", written); ! 54: } ! 55: else ! 56: printf("Read pipe data ok\n"); ! 57: } ! 58: ! 59: /* free the segment and return to parent */ ! 60: DOSFREESEG( mem_handle ); ! 61: ! 62: printf("Exiting child\n"); ! 63: ! 64: /* Exit without terminating other children */ ! 65: ! 66: DOSEXIT(0,0); ! 67: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.