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

1.1       root        1: /*
1.1.1.2 ! root        2:  * DosMakePipe example (pp.c, needs related program pc.c).
1.1       root        3:  *
                      4:  * Pipes are a byte stream oriented IPC mechanism which uses
1.1.1.2 ! root        5:  * standard DosRead and DosWrite file system calls. Since file
1.1       root        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:  *
1.1.1.2 ! root       28:  * Created by Microsoft Corp. 1987
1.1       root       29:  */
1.1.1.2 ! root       30: #define INCL_DOSQUEUES
        !            31: #define INCL_DOSFILEMGR
        !            32: #define INCL_DOSPROCESS
        !            33: 
        !            34: #include <os2def.h>
        !            35: #include <bsedos.h>
1.1       root       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: {
1.1.1.2 ! root       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 */
1.1       root       50:        int i;
1.1.1.2 ! root       51:        USHORT bytecount;               /* bytes written result */
        !            52:        RESULTCODES rc2;               /* double return codes for EXEC */
1.1       root       53: 
1.1.1.2 ! root       54:        DosMakePipe(&pread, &pwrite, PSIZE);    /* create the pipe */
1.1       root       55: 
                     56:        /*
                     57:         * Now close our stdin which is attached to the console,
                     58:         * and make it refer to the pipe instead.
                     59:         */
1.1.1.2 ! root       60:        DosClose(0);                    /* close stdin */
1.1       root       61:        newstdin = 0;
1.1.1.2 ! root       62:        DosDupHandle(pread, &newstdin); /* make pipe = stdin */
1.1       root       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:         */
1.1.1.2 ! root       74:        i = DosQFHandState(pwrite, &FileHandlState);
1.1       root       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 */
1.1.1.2 ! root       78:        i = DosSetFHandState(pwrite, FileHandlState);
1.1       root       79:        if (i) printf("Set of pwrite failed, i = %x\n",i);
                     80: #ifdef NOCODE
1.1.1.2 ! root       81:        i = DosQFHandState(pread, &FileHandlState);
1.1       root       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 */
1.1.1.2 ! root       85:        i = DosSetFHandState(pread, FileHandlState);
1.1       root       86:        if (i) printf("Set of pread failed, i = %x\n",i);
                     87: #endif
                     88:        /* exec child program, which will inherit new stdin */
1.1.1.2 ! root       89:        DosExecPgm(exec_buf, sizeof(exec_buf), EXEC_ASYNC, (PSZ)0L,
        !            90:                        (PSZ)0L, &rc2, pgmname);
1.1       root       91: 
                     92:        /* write 20 messages down the pipe */
                     93:        printf("Writing messages to the pipe\n");
                     94:        for(i = 0; i < 20; i++)
1.1.1.2 ! root       95:                DosWrite(pwrite, msg, sizeof(msg) - 1, &bytecount);
1.1       root       96: 
1.1.1.2 ! root       97:        DosClose(pwrite);
        !            98:        DosClose(pread);
1.1       root       99:        printf("Parent exiting\n");
1.1.1.2 ! root      100:        DosExit(EXIT_PROCESS,0); /* Terminate, kill any lingering children */
1.1       root      101: }

unix.superglobalmegacorp.com

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