Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you are curious about POSIX functions, especially those that seem to duplicate themselves, it's generally good to <a href="http://www.opengroup.org/onlinepubs/009695399/functions/dup.html" rel="nofollow noreferrer">check the standard itself</a>. At the bottom you will usually see examples, as well as reasoning behind the implementation (and existence) of both.</p> <p>In this case:</p> <p>The following sections are informative.</p> <h3>Examples</h3> <p>Redirecting Standard Output to a File</p> <p>The following example closes standard output for the current processes, re-assigns standard output to go to the file referenced by <code>pfd</code>, and closes the original file descriptor to clean up.</p> <pre><code>#include &lt;unistd.h&gt; ... int pfd; ... close(1); dup(pfd); close(pfd); ... </code></pre> <h3>Redirecting Error Messages</h3> <p>The following example redirects messages from <code>stderr</code> to <code>stdout</code>.</p> <pre><code>#include &lt;unistd.h&gt; ... dup2(2, 1); // 2-stderr; 1-stdout ... </code></pre> <h3>Application Usage</h3> <p>None.</p> <h2>Rationale</h2> <p>The <code>dup()</code> and <code>dup2()</code> functions are redundant. Their services are also provided by the <code>fcntl()</code> function. They have been included in this volume of IEEE Std 1003.1-2001 primarily for historical reasons, since many existing applications use them.</p> <p>While the brief code segment shown is very similar in behavior to <code>dup2()</code>, a conforming implementation based on other functions defined in this volume of IEEE Std 1003.1-2001 is significantly more complex. Least obvious is the possible effect of a signal-catching function that could be invoked between steps and allocate or deallocate file descriptors. This could be avoided by blocking signals.</p> <p>The <code>dup2()</code> function is not marked obsolescent because it presents a type-safe version of functionality provided in a type-unsafe version by <code>fcntl()</code>. It is used in the POSIX Ada binding.</p> <p>The <code>dup2()</code> function is not intended for use in critical regions as a synchronization mechanism.</p> <p>In the description of [EBADF], the case of fildes being out of range is covered by the given case of fildes not being valid. The descriptions for <code>fildes</code> and <code>fildes2</code> are different because the only kind of invalidity that is relevant for <code>fildes2</code> is whether it is out of range; that is, it does not matter whether <code>fildes2</code> refers to an open file when the <code>dup2()</code> call is made.</p> <h3>Future Directions</h3> <p>None.</p> <h3>See also</h3> <p><code>close()</code>, <code>fcntl()</code>, <code>open()</code>, the Base Definitions volume of IEEE Std 1003.1-2001, <code>&lt;unistd.h&gt;</code></p> <h3>Change History</h3> <p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload