Next Previous Contents

15. pipe

pipe is one of the inter-process communications. pipe is very useful between parent process and child process. Parent process can send a byte stream to the child process through a pipe. The child process can read them from its standard input. Similarly, parent process can receive a byte stream from the child process through a pipe. The child process writes them to its standard output or its standard error output.

Most importantly, child process program doesn't care about pipe. It just reads/writes through its standard intput/output/error. On the other hand, from the parent's perspective, pipe is seen as a file object. Accordingly, parent process just calls apr_file_read() or apr_file_write() to send to/receive data from its pipe.

To handle pipe, we call apr_procattr_io_set() to set process attribute object.

/* excerpted from apr_thread_proc.h */

APR_DECLARE(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr, 
                                             apr_int32_t in, apr_int32_t out,
                                             apr_int32_t err);

apr_procattr_io_set() has four arguments. The first is an apr_procattr_t object to set. The other arguments are related to standard intput/output/error. The following code is excerpted from pipe-sample.c:

/* excerpted from pipe-sample.h, but I omitted error checks */

apr_procattr_t *pattr;
apr_procattr_create(&pattr, mp);
apr_procattr_io_set(pattr, APR_NO_PIPE, APR_FULL_BLOCK, APR_NO_PIPE);

This means the parent process cares only the child process's standard output. The parent will receive a byte stream from the child's standard output. As you see in pipe-sample.c, the parent process calls apr_file_read() to read data from the child process. The data are passed through pipe between two processes. In addition, specifying APR_FULL_BLOCK indicates the parent will block until the child writes something to its standard output or child's termination.


Next Previous Contents