pipe.html
来自「posix标准英文,html格式」· HTML 代码 · 共 188 行
HTML
188 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>pipe</title></head><body bgcolor="white"><basefont size="3"> <a name="pipe"></a> <a name="tag_03_418"></a><!-- pipe --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_418_01"></a>NAME</h4><blockquote>pipe - create an interprocess channel</blockquote><h4><a name="tag_03_418_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/unistd.h.html">unistd.h</a>><br><br> int pipe(int</tt> <i>fildes</i><tt>[2]);<br></tt></code></p></blockquote><h4><a name="tag_03_418_03"></a>DESCRIPTION</h4><blockquote><p>The <i>pipe</i>() function shall create a pipe and place two file descriptors, one each into the arguments <i>fildes</i>[0] and<i>fildes</i>[1], that refer to the open file descriptions for the read and write ends of the pipe. Their integer values shall bethe two lowest available at the time of the <i>pipe</i>() call. The O_NONBLOCK and FD_CLOEXEC flags shall be clear on both filedescriptors. (The <a href="../functions/fcntl.html"><i>fcntl</i>()</a> function can be used to set both these flags.)</p><p>Data can be written to the file descriptor <i>fildes</i>[1] and read from the file descriptor <i>fildes</i>[0]. A read on thefile descriptor <i>fildes</i>[0] shall access data written to the file descriptor <i>fildes</i>[1] on a first-in-first-out basis.It is unspecified whether <i>fildes</i>[0] is also open for writing and whether <i>fildes</i>[1] is also open for reading.</p><p>A process has the pipe open for reading (correspondingly writing) if it has a file descriptor open that refers to the read end,<i>fildes</i>[0] (write end, <i>fildes</i>[1]).</p><p>Upon successful completion, <i>pipe</i>() shall mark for update the <i>st_atime</i>, <i>st_ctime</i>, and <i>st_mtime</i> fieldsof the pipe.</p></blockquote><h4><a name="tag_03_418_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, 0 shall be returned; otherwise, -1 shall be returned and <i>errno</i> set to indicate the error.</p></blockquote><h4><a name="tag_03_418_05"></a>ERRORS</h4><blockquote><p>The <i>pipe</i>() function shall fail if:</p><dl compact><dt>[EMFILE]</dt><dd>More than {OPEN_MAX} minus two file descriptors are already in use by this process.</dd><dt>[ENFILE]</dt><dd>The number of simultaneously open files in the system would exceed a system-imposed limit.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_418_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_418_06_01"></a>Using a Pipe to Pass Data Between a Parent Process and a Child Process</h5><p>The following example demonstrates the use of a pipe to transfer data between a parent process and a child process. Errorhandling is excluded, but otherwise this code demonstrates good practice when using pipes: after the <a href="../functions/fork.html"><i>fork</i>()</a> the two processes close the unused ends of the pipe before they commence transferringdata.</p><pre><tt>#include <stdlib.h>#include <unistd.h>...<br>int fildes[2];const int BSIZE = 100;char buf[BSIZE];ssize_t nbytes;int status;<br>status = pipe(fildes);if (status == -1 ) { /* an error occurred */ ...}<br>switch (fork()) {case -1: /* Handle error */ break;<br>case 0: /* Child - reads from pipe */ close(fildes[1]); /* Write end is unused */ nbytes = read(fildes[0], buf, BSIZE); /* Get data from pipe */ /* At this point, a further read would see end of file ... */ close(fildes[0]); /* Finished with pipe */ exit(EXIT_SUCCESS);<br>default: /* Parent - writes to pipe */ close(fildes[0]); /* Read end is unused */ write(fildes[1], "Hello world\n", 12); /* Write data on pipe */ close(fildes[1]); /* Child will see EOF */ exit(EXIT_SUCCESS);}</tt></pre></blockquote><h4><a name="tag_03_418_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_418_08"></a>RATIONALE</h4><blockquote><p>The wording carefully avoids using the verb "to open" in order to avoid any implication of use of <a href="../functions/open.html"><i>open</i>()</a>; see also <a href="write.html"><i>write</i>()</a>.</p></blockquote><h4><a name="tag_03_418_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_418_10"></a>SEE ALSO</h4><blockquote><p><a href="fcntl.html"><i>fcntl</i>()</a>, <a href="read.html"><i>read</i>()</a>, <a href="write.html"><i>write</i>()</a>, theBase Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>, <a href="../basedefs/unistd.h.html"><i><unistd.h></i></a></p></blockquote><h4><a name="tag_03_418_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_418_12"></a>Issue 6</h4><blockquote><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The DESCRIPTION is updated to indicate that certain dispositions of <i>fildes</i>[0] and <i>fildes</i>[1] are unspecified.</p></li></ul><p>IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/65 is applied, adding the example to the EXAMPLES section.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?