📄 pclose.html
字号:
<!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-2003 The Open Group, All Rights Reserved --><title>pclose</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="pclose"></a> <a name="tag_03_416"></a><!-- pclose --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_416_01"></a>NAME</h4><blockquote>pclose - close a pipe stream to or from a process</blockquote><h4><a name="tag_03_416_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/stdio.h.html">stdio.h</a>><br><br> int pclose(FILE *</tt><i>stream</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_416_03"></a>DESCRIPTION</h4><blockquote><p>The <i>pclose</i>() function shall close a stream that was opened by <a href="../functions/popen.html"><i>popen</i>()</a>, waitfor the command to terminate, and return the termination status of the process that was running the command language interpreter.However, if a call caused the termination status to be unavailable to <i>pclose</i>(), then <i>pclose</i>() shall return -1 with<i>errno</i> set to [ECHILD] to report this situation. This can happen if the application calls one of the following functions:</p><ul><li><p><a href="../functions/wait.html"><i>wait</i>()</a></p></li><li><p><a href="../functions/waitpid.html"><i>waitpid</i>()</a> with a <i>pid</i> argument less than or equal to 0 or equal to theprocess ID of the command line interpreter</p></li><li><p>Any other function not defined in this volume of IEEE Std 1003.1-2001 that could do one of the above</p></li></ul><p>In any case, <i>pclose</i>() shall not return before the child process created by <a href="../functions/popen.html"><i>popen</i>()</a> has terminated.</p><p>If the command language interpreter cannot be executed, the child termination status returned by <i>pclose</i>() shall be as ifthe command language interpreter terminated using <i>exit</i>(127) or <i>_exit</i>(127).</p><p>The <i>pclose</i>() function shall not affect the termination status of any child of the calling process other than the onecreated by <a href="../functions/popen.html"><i>popen</i>()</a> for the associated stream.</p><p>If the argument <i>stream</i> to <i>pclose</i>() is not a pointer to a stream created by <a href="../functions/popen.html"><i>popen</i>()</a>, the result of <i>pclose</i>() is undefined.</p></blockquote><h4><a name="tag_03_416_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful return, <i>pclose</i>() shall return the termination status of the command language interpreter. Otherwise,<i>pclose</i>() shall return -1 and set <i>errno</i> to indicate the error.</p></blockquote><h4><a name="tag_03_416_05"></a>ERRORS</h4><blockquote><p>The <i>pclose</i>() function shall fail if:</p><dl compact><dt>[ECHILD]</dt><dd>The status of the child process could not be obtained, as described above.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_416_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_416_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_416_08"></a>RATIONALE</h4><blockquote><p>There is a requirement that <i>pclose</i>() not return before the child process terminates. This is intended to disallowimplementations that return [EINTR] if a signal is received while waiting. If <i>pclose</i>() returned before the child terminated,there would be no way for the application to discover which child used to be associated with the stream, and it could not do thecleanup itself.</p><p>If the stream pointed to by <i>stream</i> was not created by <a href="../functions/popen.html"><i>popen</i>()</a>, historicalimplementations of <i>pclose</i>() return -1 without setting <i>errno</i>. To avoid requiring <i>pclose</i>() to set <i>errno</i>in this case, IEEE Std 1003.1-2001 makes the behavior unspecified. An application should not use <i>pclose</i>() to closeany stream that was not created by <a href="../functions/popen.html"><i>popen</i>()</a>.</p><p>Some historical implementations of <i>pclose</i>() either block or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waitingfor the child process to terminate. Since this behavior is not described for the <i>pclose</i>() function inIEEE Std 1003.1-2001, such implementations are not conforming. Also, some historical implementations return [EINTR] if asignal is received, even though the child process has not terminated. Such implementations are also considered non-conforming.</p><p>Consider, for example, an application that uses:</p><pre><tt>popen("command", "r")</tt></pre><p>to start <i>command</i>, which is part of the same application. The parent writes a prompt to its standard output (presumablythe terminal) and then reads from the <a href="../functions/popen.html"><i>popen</i>()</a>ed stream. The child reads the responsefrom the user, does some transformation on the response (pathname expansion, perhaps) and writes the result to its standard output.The parent process reads the result from the pipe, does something with it, and prints another prompt. The cycle repeats. Assumingthat both processes do appropriate buffer flushing, this would be expected to work.</p><p>To conform to IEEE Std 1003.1-2001, <i>pclose</i>() must use <a href="../functions/waitpid.html"><i>waitpid</i>()</a>,or some similar function, instead of <a href="../functions/wait.html"><i>wait</i>()</a>.</p><p>The code sample below illustrates how the <i>pclose</i>() function might be implemented on a system conforming toIEEE Std 1003.1-2001.</p><pre><tt>int pclose(FILE *stream){ int stat; pid_t pid;<br> pid = <pid for process created for stream by popen()> (void) fclose(stream); while (waitpid(pid, &stat, 0) == -1) { if (errno != EINTR){ stat = -1; break; } } return(stat);}</tt></pre></blockquote><h4><a name="tag_03_416_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_416_10"></a>SEE ALSO</h4><blockquote><p><a href="fork.html"><i>fork</i>()</a> , <a href="popen.html"><i>popen</i>()</a> , <a href="waitpid.html"><i>waitpid</i>()</a> ,the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/stdio.h.html"><i><stdio.h></i></a></p></blockquote><h4><a name="tag_03_416_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -