home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / pipes / pchild.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-20  |  2.0 KB  |  74 lines

  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.  * Created by Microsoft Corp. 1987
  9.  */
  10. #define INCL_DOSFILEMGR
  11. #define INCL_DOSMEMMGR
  12.  
  13. #include <os2def.h>
  14. #include <stdio.h>
  15. #include <bsedos.h>
  16.  
  17. typedef struct {
  18.     SHANDLE read_handle;           /* pipe read handle */
  19.     SHANDLE write_handle;           /* pipe write handle */
  20. } SharedData;
  21.  
  22.  
  23. main()
  24. {
  25.  
  26.     static char pname[] = "\\SHAREMEM\\public";   /* shared mem seg name */
  27.     char *written = "Writing to the child";          /* string in pipe */
  28.     char readin[21];        /* DosRead input buffer */
  29.     int retcode;            /* holds return code from call */
  30.     SharedData far *fp;        /* pointer to shared memory */
  31.     SEL mem_handle;         /* selector of the allocated segment */
  32.     USHORT buflen = 21;        /* DosRead buffer length */
  33.     USHORT read;            /* number bytes read by DosRead */
  34.  
  35.  
  36.     /* access shared memory 'public' */
  37.     printf("Accessing shared memory\n");
  38.     retcode = DosGetShrSeg((PSZ)pname, &mem_handle);
  39.     
  40.     /* create pointer to shared memory segment */
  41.     fp = (SharedData far *)MAKEP(mem_handle,0);
  42.  
  43.         /* read from the pipe */
  44.     printf("Reading from pipe\n");
  45.     if( retcode = DosRead( fp->read_handle, readin, buflen,
  46.             &read)) {
  47.                 printf("Read from pipe handle %d failed, retcode %d\n", 
  48.             fp->read_handle, retcode);
  49.         }
  50.     else {
  51.         printf("DosRead read %d bytes from handle %d, retcode %d\n", 
  52.             read, fp->read_handle, retcode);
  53.  
  54.         /* verify the string */
  55.         if ( retcode = strcmp( written, readin ) ) {
  56.             printf("The child didn't read pipe data correctly");
  57.             printf(", retcode %d\n", retcode);
  58.             printf("read string %s\n", readin);
  59.             printf("expected string %s\n", written);
  60.         }
  61.         else
  62.             printf("Read pipe data ok\n");
  63.     }
  64.  
  65.     /* free the segment and return to parent */
  66.     DosFreeSeg( mem_handle );
  67.  
  68.     printf("Exiting child\n");
  69.  
  70.     /* Exit without terminating other children */
  71.  
  72.     DosExit(EXIT_THREAD,0);
  73. }
  74.