Creating Unnamed Pipes

DosCreatePipe creates an unnamed pipe. Two handles are returned: one for read access to the pipe and one for write access. The pipe size specified is advisory; its actual size is dependent on the amount of available memory. If the size parameter is 0, the pipe is created with the default size, which is 512 bytes.

This example creates an unnamed pipe. The current process can use the unnamed pipe for communication between itself and a child process.

    #define INCL_BASE    /* Queue values */
    #include <os2.h>
    #include <stdio.h>

    HFILE    hfReadHandle;    /* Pointer to the read handle      */
    HFILE    hfWriteHandle;   /* Pointer to the write handle     */
    ULONG    ulPipeSize;      /* Pipe size                       */
    APIRET   ulrc;            /* Return code                     */

    ulPipeSize = 4096;        /* Ask for 4KB of internal storage */
                              /* for the pipe                    */

    ulrc = DosCreatePipe(&hfReadHandle,
                         &hfWriteHandle,
                         ulPipeSize);

    if (ulrc != 0) {
        printf("DosCreatePipe error: return code = %ld",
               ulrc);
        return;
    }

On successful return, the ReadHandle variable contains the read handle for the pipe, and the WriteHandle variable contains the write handle for the pipe.

After a process creates a pipe, any child process started with DosExecPgm inherits the pipe handles. Using shared memory, the parent process can pass one of the pipe handles to the child process; thus, one process can store data in the pipe and the other can retrieve it.


[Back: Using Unnamed Pipes]
[Next: Reading from and Writing to Unnamed Pipes]