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

  1. /***    pipes.c - parent program
  2.  *
  3.  *    Example of DosMakePipe usage in parent/child communication
  4.  *
  5.  *    This program gets some shared memory, makes a pipe, writes
  6.  *    a string into it, and then execs a child.  The child gets
  7.  *    the shared memory segment, then reads from the pipe using
  8.  *    the handle passed in the shared memory segment.
  9.  *
  10.  * Created by Microsoft Corp. 1987
  11.  */
  12.  
  13. #define INCL_DOSPROCESS
  14. #define INCL_DOSFILEMGR
  15. #define INCL_DOSMEMMGR
  16. #define INCL_DOSQUEUES
  17.  
  18. #include <os2def.h>
  19. #include <stdio.h>
  20. #include <bsedos.h>
  21.  
  22. typedef struct {            /* structure of shared mem segment */
  23.     SHANDLE  read_handle;        /* pipe read handle */
  24.     SHANDLE  write_handle;        /* pipe write handle */
  25. } SharedData;
  26.  
  27. SharedData far *fp;            /* pointer to shared memory */
  28. SEL mem_handle;             /* selector of the allocated segment */
  29.  
  30.  
  31. main()
  32. {
  33.  
  34.     static char pname[] = "\\SHAREMEM\\public";       /* shared mem name */
  35.     static char writeout[] = "Writing to the child";   /* pipe string */
  36.     char exec_buf[100];        /* buffer for DosExecPgm ObjName */
  37.     char *pgmname = "pchild.exe";    /* name of child program */
  38.     int i;
  39.     int retcode;            /* holds return code from call */
  40.     RESULTCODES       tcodes;    /* termination codes from DosExecPgm */
  41.     unsigned far *fpinit;        /* pointer to shared memory */
  42.     USHORT buflen;            /* DosWrite buffer length */
  43.     USHORT memsiz = 1024;        /* size of memory segment requested */
  44.     USHORT pipe_size;        /* size to reserve for the pipe */
  45.     USHORT written;         /* number bytes written by DosWrite */
  46.  
  47.     /* allocate 1k shared memory segment and initialize it */
  48.     printf("Getting shared memory segment\n");
  49.     retcode = DosAllocShrSeg( memsiz, (PSZ)pname,
  50.             (PSEL)&mem_handle);
  51.  
  52.     /* create pointers to shared memory segment */
  53.     fp = (SharedData far *)MAKEP(mem_handle,0);
  54.     fpinit = (unsigned far *)MAKEP(mem_handle,0);
  55.  
  56.     /* zero initialize the segment */
  57.     for (i = 0; i < memsiz / 2; i++) {
  58.         *fpinit = 0;
  59.         fpinit++;
  60.     }
  61.  
  62.     /* make the pipe */
  63.     pipe_size = 0;            /* use default size */
  64.     printf("Making the pipe\n");
  65.     retcode = DosMakePipe((PHFILE)&fp->read_handle,
  66.             (PHFILE)&fp->write_handle, pipe_size);
  67.  
  68.     if ( retcode ) {
  69.         printf("DosMakePipe returned error %d, aborting\n", retcode);
  70.         exit(2);
  71.     }
  72.     else
  73.         printf("DosMakePipe retcode %d, read_handle %d, write_handle %d\n",
  74.         retcode, fp->read_handle, fp->write_handle);
  75.  
  76.         /* write string to pipe */
  77.     buflen = strlen(writeout) + 1;
  78.     if( retcode = DosWrite( fp->write_handle, (PCHAR)writeout,
  79.             buflen, &written)) {
  80.                 printf("Write to pipe failed, retcode %d\n", retcode);
  81.                 exit(2);
  82.         }
  83.     else 
  84.         printf("Write to write_handle %d, bytes written %d\n",
  85.             fp->write_handle, written);
  86.         
  87.         /* create the child */
  88.     printf("Creating child\n");
  89.     retcode = DosExecPgm( exec_buf, 100, EXEC_SYNC,
  90.             (PSZ)0L, (PSZ)0L,
  91.             &tcodes, pgmname );
  92.  
  93.     if ( retcode )
  94.                 printf("DosExecPgm of child error %d\n", retcode);
  95.     else 
  96.         printf("Back in parent\n");
  97.     
  98.     /* close the pipe */
  99.     DosClose( fp->read_handle );
  100.     DosClose( fp->write_handle );
  101.  
  102.     DosExit(EXIT_PROCESS,0);   /* Terminate, kill any dangling children */
  103.  
  104. }
  105.