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

  1. /*
  2.  * DosMakePipe example (pp.c, needs related program pc.c).
  3.  *
  4.  * Pipes are a byte stream oriented IPC mechanism which uses
  5.  * standard DosRead and DosWrite file system calls. Since file
  6.  * handles are used to access the pipe, each process using the
  7.  * pipe needs to know the handles. There are two general ways
  8.  * this is done:
  9.  *
  10.  *    1. Have very closely cooperating processes which notify
  11.  *       each other of the file handles via some private method
  12.  *       such as shared memory or command line arguments.
  13.  *
  14.  *    2. With unrelated processes, a process which reads and writes
  15.  *       from STDIN (handle 0) and STDOUT (handle 1) can be attached
  16.  *       to a pipe without being aware of it.
  17.  *
  18.  * This example demonstrates case #2 since that is the harder of the
  19.  * two to set up. This process, pp.exe, creates a pipe and exec's a
  20.  * child process pc.exe whose stdin is attached to the pipe. The parent
  21.  * process writes a lower case message repeatedly into the pipe, and
  22.  * the child process translates it into upper case and prints it on
  23.  * the screen.
  24.  *
  25.  * Compile as: cl -AL -G2 -Lp pp.c   (parent process)
  26.  *             cl -AL -G2 -Lp pc.c   (child process)
  27.  *
  28.  * Created by Microsoft Corp. 1987
  29.  */
  30. #define INCL_DOSQUEUES
  31. #define INCL_DOSFILEMGR
  32. #define INCL_DOSPROCESS
  33.  
  34. #include <os2def.h>
  35. #include <bsedos.h>
  36. #include <stdio.h>
  37.  
  38. #define PSIZE 256            /* pipe buffer size */
  39.  
  40. char msg[] = "hello there!\n";        /* message to write down pipe */
  41. char exec_buf[100];            /* buffer for DosExecPgm ObjName */
  42. char *pgmname = "pc.exe";        /* name of child program */
  43.  
  44. main()
  45. {
  46.     HFILE pread;            /* pipe read handle */
  47.     HFILE pwrite;            /* pipe write handle */
  48.     HFILE newstdin, newstdout;    /* stdin as a pipe handle */
  49.     USHORT FileHandlState;        /* Used to modify handle inheritance */
  50.     int i;
  51.     USHORT bytecount;        /* bytes written result */
  52.     RESULTCODES rc2;           /* double return codes for EXEC */
  53.  
  54.     DosMakePipe(&pread, &pwrite, PSIZE);    /* create the pipe */
  55.  
  56.     /*
  57.      * Now close our stdin which is attached to the console,
  58.      * and make it refer to the pipe instead.
  59.      */
  60.     DosClose(0);            /* close stdin */
  61.     newstdin = 0;
  62.     DosDupHandle(pread, &newstdin); /* make pipe = stdin */
  63.     /*
  64.      * Since the child process will normaly inherit the handles of
  65.      * the parent we need to close the input of the pipe.  If we
  66.      * don't then the child will hang on the output of the pipe. (since
  67.      * it still has an open handle to the input).
  68.      *
  69.      * We don't really close the input, simply make it non-inheritable
  70.      * to the child.  Thus the only input to the pipe is from the parent.
  71.      * When the parent closes the pipe, any outstanding READs from the
  72.      * child will return a length of 0 (EOF).
  73.      */
  74.     i = DosQFHandState(pwrite, &FileHandlState);
  75.     if (i) printf("Query of pwrite failed\n");
  76.     FileHandlState &= 0x7F88;    /* Mask bits offensive to the call */
  77.     FileHandlState |= 0x080;    /* Deny inheritance to child */
  78.     i = DosSetFHandState(pwrite, FileHandlState);
  79.     if (i) printf("Set of pwrite failed, i = %x\n",i);
  80. #ifdef NOCODE
  81.     i = DosQFHandState(pread, &FileHandlState);
  82.     if (i) printf("Query of pread failed\n");
  83.     FileHandlState &= 0x7F88;    /* Mask bits offensive to the call */
  84.     FileHandlState |= 0x080;    /* Deny inheritance to child */
  85.     i = DosSetFHandState(pread, FileHandlState);
  86.     if (i) printf("Set of pread failed, i = %x\n",i);
  87. #endif
  88.     /* exec child program, which will inherit new stdin */
  89.     DosExecPgm(exec_buf, sizeof(exec_buf), EXEC_ASYNC, (PSZ)0L,
  90.             (PSZ)0L, &rc2, pgmname);
  91.  
  92.     /* write 20 messages down the pipe */
  93.     printf("Writing messages to the pipe\n");
  94.     for(i = 0; i < 20; i++)
  95.         DosWrite(pwrite, msg, sizeof(msg) - 1, &bytecount);
  96.  
  97.     DosClose(pwrite);
  98.     DosClose(pread);
  99.     printf("Parent exiting\n");
  100.     DosExit(EXIT_PROCESS,0); /* Terminate, kill any lingering children */
  101. }
  102.