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

1.1       root        1: /***   pchild.c - child program
                      2:  *
1.1.1.2 ! root        3:  *     Example of DosMakePipe usage in parent/child communication
1.1       root        4:  *
                      5:  *     This is the child program which read the data sent down
                      6:  *     from the parent in the pipe.
1.1.1.2 ! root        7:  *
        !             8:  * Created by Microsoft Corp. 1987
1.1       root        9:  */
1.1.1.2 ! root       10: #define INCL_DOSFILEMGR
        !            11: #define INCL_DOSMEMMGR
        !            12: 
        !            13: #include <os2def.h>
1.1       root       14: #include <stdio.h>
1.1.1.2 ! root       15: #include <bsedos.h>
1.1       root       16: 
                     17: typedef struct {
1.1.1.2 ! root       18:        SHANDLE read_handle;           /* pipe read handle */
        !            19:        SHANDLE write_handle;          /* pipe write handle */
1.1       root       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 */
1.1.1.2 ! root       31:        SEL mem_handle;                 /* selector of the allocated segment */
        !            32:        USHORT buflen = 21;             /* DosRead buffer length */
        !            33:        USHORT read;                    /* number bytes read by DosRead */
1.1       root       34: 
                     35: 
                     36:        /* access shared memory 'public' */
                     37:        printf("Accessing shared memory\n");
1.1.1.2 ! root       38:        retcode = DosGetShrSeg((PSZ)pname, &mem_handle);
1.1       root       39:        
                     40:        /* create pointer to shared memory segment */
1.1.1.2 ! root       41:        fp = (SharedData far *)MAKEP(mem_handle,0);
1.1       root       42: 
                     43:         /* read from the pipe */
                     44:        printf("Reading from pipe\n");
1.1.1.2 ! root       45:        if( retcode = DosRead( fp->read_handle, readin, buflen,
        !            46:                        &read)) {
1.1       root       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 */
1.1.1.2 ! root       66:        DosFreeSeg( mem_handle );
1.1       root       67: 
                     68:        printf("Exiting child\n");
                     69: 
                     70:        /* Exit without terminating other children */
                     71: 
1.1.1.2 ! root       72:        DosExit(EXIT_THREAD,0);
1.1       root       73: }

unix.superglobalmegacorp.com

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