📄 apr-tutorial-15.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.21"> <TITLE>libapr(apache portable runtime) programming tutorial: pipe</TITLE> <LINK HREF="apr-tutorial-16.html" REL=next> <LINK HREF="apr-tutorial-14.html" REL=previous> <LINK HREF="apr-tutorial.html#toc15" REL=contents></HEAD><BODY><A HREF="apr-tutorial-16.html">Next</A><A HREF="apr-tutorial-14.html">Previous</A><A HREF="apr-tutorial.html#toc15">Contents</A><HR><H2><A NAME="s15">15.</A> <A HREF="apr-tutorial.html#toc15">pipe</A></H2><P>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.</P><P>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.</P><P>To handle pipe, we call apr_procattr_io_set() to set process attribute object.</P><P>/* excerpted from apr_thread_proc.h */<BLOCKQUOTE><CODE><PRE>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);</PRE></CODE></BLOCKQUOTE></P><P>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 <A HREF="../sample/pipe-sample.c">pipe-sample.c</A>:</P><P>/* excerpted from pipe-sample.h, but I omitted error checks */<BLOCKQUOTE><CODE><PRE>apr_procattr_t *pattr;apr_procattr_create(&pattr, mp);apr_procattr_io_set(pattr, APR_NO_PIPE, APR_FULL_BLOCK, APR_NO_PIPE);</PRE></CODE></BLOCKQUOTE></P><P>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 <A HREF="../sample/pipe-sample.c">pipe-sample.c</A>, 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. </P><HR><A HREF="apr-tutorial-16.html">Next</A><A HREF="apr-tutorial-14.html">Previous</A><A HREF="apr-tutorial.html#toc15">Contents</A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -