dup

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

dup, dup2, dup3 - ファイルディスクリプターを複製する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <unistd.h>

int dup(int oldfd);
int dup2(int oldfd, int newfd);

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Definition of O_* constants */
#include <unistd.h>

int dup3(int oldfd, int newfd, int flags);
 

説明

The dup() system call allocates a new file descriptor that refers to the same open file description as the descriptor oldfd. (For an explanation of open file descriptions, see open(2).) The new file descriptor number is guaranteed to be the lowest-numbered file descriptor that was unused in the calling process.

After a successful return, the old and new file descriptors may be used interchangeably. Since the two file descriptors refer to the same open file description, they share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the file descriptors, the offset is also changed for the other file descriptor.

2つのファイルディスクリプターはファイルディスクリプターフラグ (close-on-exec flag) を共有しない。複製されたディスクリプターの close-on-exec flag (fcntl(2) 参照) は off となる。  

dup2()

The dup2() system call performs the same task as dup(), but instead of using the lowest-numbered unused file descriptor, it uses the file descriptor number specified in newfd. In other words, the file descriptor newfd is adjusted so that it now refers to the same open file description as oldfd.

If the file descriptor newfd was previously open, it is closed before being reused; the close is performed silently (i.e., any errors during the close are not reported by dup2()).

ファイルディスクリプター newfd をクローズして再利用する処理は アトミック(不可分)に実行される。これは重要な点である。 なぜなら、 等価な機能を close(2) と dup() を使って実装しようとすると、 2 つの処理の間に newfd が再利用されてしまうという、 競合状態にさらされることになるからだ。 このような再利用が起こるのは、 メインプログラムがファイルディスクリプターを割り当てる シグナルハンドラーにより割り込まれたり、並行動作するスレッドが ファイルディスクリプターを割り当てたりすることがあるからだ。

以下の点について注意すること:

•
oldfd が有効なファイルディスクリプターでない場合、その呼び出しは失敗し、 newfd はクローズされない。
•
oldfd が有効なファイルディスクリプターで、 newfd が oldfd と同じ値の場合、 dup2() は何もせず、 newfd を返す。
 

dup3()

dup3() は dup2() と同じだが、以下の点が異なる。
•
呼び出し元が、新しいファイルディスクリプターに対して close-on-exec フラグを強制的に設定することができる。 これを行うには、 flags に O_CLOEXEC を指定する。 このフラグが役に立つ理由については、 open(2) の O_CLOEXEC フラグの説明を参照のこと。
•
oldfd が newfd と同じ場合、 dup3() は EINVAL エラーで失敗する。
 

返り値

On success, these system calls return the new file descriptor. On error, -1 is returned, and errno is set to indicate the error.  

エラー

EBADF
oldfd がオープンされたファイルディスクリプターではない。
EBADF
newfd がファイルディスクリプターとして許可されている範囲ではない (getrlimit(2) の RLIMIT_NOFILE の議論を参照)。
EBUSY
(Linux のみ) open(2) や dup() との競合状態の場合に、 dup2() や dup3() はこのエラーを返すかもしれない。
EINTR
dup2() や dup3() の呼び出しがシグナルにより割り込まれた。 signal(7) 参照。
EINVAL
(dup3()) flags に無効な値が入っている。
EINVAL
(dup3()) oldfd が newfd と同じであった。
EMFILE
The per-process limit on the number of open file descriptors has been reached (see the discussion of RLIMIT_NOFILE in getrlimit(2)).
ENOMEM
カーネルに十分なメモリーがない。
 

標準規格

POSIX.1-2024.  

歴史

dup()
dup2()
4.3BSD, SVr4, POSIX.1-1988.
dup3()
POSIX.1-2024. Linux 2.6.27, glibc 2.9.
 

注意

newfd が範囲を超えた時に返されるエラーは、 dup2() と fcntl(..., F_DUPFD, ...) では異っている。 dup2() が F_DUPFD と同じように EINVAL を返すシステムもある。

If newfd was open, any errors that would have been reported at close(2) time are lost. If this is of concern, then---unless the program is single-threaded and does not allocate file descriptors in signal handlers---the correct approach is not to close newfd before calling dup2(), because of the race condition described above. Instead, code something like the following could be used:

/* Obtain a duplicate of 'newfd' that can subsequently
   be used to check for close() errors; an EBADF error
   means that 'newfd' was not open.  */

tmpfd = dup(newfd);
if (tmpfd == -1 && errno != EBADF) {
    /* Handle unexpected dup() error.  */
}

/* Atomically duplicate 'oldfd' on 'newfd'.  */

if (dup2(oldfd, newfd) == -1) {
    /* Handle dup2() error.  */
}

/* Now check for close() errors on the file originally
   referred to by 'newfd'.  */

if (tmpfd != -1) {
    if (close(tmpfd) == -1) {
        /* Handle errors from close.  */
    }
}
 

関連項目

close(2), fcntl(2), open(2), pidfd_getfd(2)


 

Index

名前
ライブラリ
書式
説明
dup2()
dup3()
返り値
エラー
標準規格
歴史
注意
関連項目

This document was created by man2html, using the manual pages.