Annotation of os2sdk/demos/examples/pipes/pipes.c, revision 1.1.1.2

1.1       root        1: /***   pipes.c - parent program
                      2:  *
1.1.1.2 ! root        3:  *     Example of DosMakePipe usage in parent/child communication
1.1       root        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.
1.1.1.2 ! root        9:  *
        !            10:  * Created by Microsoft Corp. 1987
1.1       root       11:  */
1.1.1.2 ! root       12: 
        !            13: #define INCL_DOSPROCESS
        !            14: #define INCL_DOSFILEMGR
        !            15: #define INCL_DOSMEMMGR
        !            16: #define INCL_DOSQUEUES
        !            17: 
        !            18: #include <os2def.h>
1.1       root       19: #include <stdio.h>
1.1.1.2 ! root       20: #include <bsedos.h>
1.1       root       21: 
                     22: typedef struct {                       /* structure of shared mem segment */
1.1.1.2 ! root       23:        SHANDLE  read_handle;           /* pipe read handle */
        !            24:        SHANDLE  write_handle;          /* pipe write handle */
1.1       root       25: } SharedData;
                     26: 
                     27: SharedData far *fp;                    /* pointer to shared memory */
1.1.1.2 ! root       28: SEL mem_handle;                        /* selector of the allocated segment */
1.1       root       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 */
1.1.1.2 ! root       40:        RESULTCODES        tcodes;      /* termination codes from DosExecPgm */
1.1       root       41:        unsigned far *fpinit;           /* pointer to shared memory */
1.1.1.2 ! root       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 */
1.1       root       46: 
                     47:        /* allocate 1k shared memory segment and initialize it */
                     48:        printf("Getting shared memory segment\n");
1.1.1.2 ! root       49:        retcode = DosAllocShrSeg( memsiz, (PSZ)pname,
        !            50:                        (PSEL)&mem_handle);
1.1       root       51: 
                     52:        /* create pointers to shared memory segment */
1.1.1.2 ! root       53:        fp = (SharedData far *)MAKEP(mem_handle,0);
        !            54:        fpinit = (unsigned far *)MAKEP(mem_handle,0);
1.1       root       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");
1.1.1.2 ! root       65:        retcode = DosMakePipe((PHFILE)&fp->read_handle,
        !            66:                        (PHFILE)&fp->write_handle, pipe_size);
1.1       root       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;
1.1.1.2 ! root       78:        if( retcode = DosWrite( fp->write_handle, (PCHAR)writeout,
        !            79:                        buflen, &written)) {
1.1       root       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");
1.1.1.2 ! root       89:        retcode = DosExecPgm( exec_buf, 100, EXEC_SYNC,
        !            90:                        (PSZ)0L, (PSZ)0L,
        !            91:                        &tcodes, pgmname );
1.1       root       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 */
1.1.1.2 ! root       99:        DosClose( fp->read_handle );
        !           100:        DosClose( fp->write_handle );
1.1       root      101: 
1.1.1.2 ! root      102:        DosExit(EXIT_PROCESS,0);   /* Terminate, kill any dangling children */
1.1       root      103: 
                    104: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.