dup (system call)
|
|
This article needs additional citations for verification. (March 2012) |
In Unix-like operating systems, dup and dup2 system calls create a copy of a given file descriptor. This new descriptor actually does not not behave like a copy, but like an alias of the old one.
Contents |
C library POSIX definition [edit]
These dup and dup2 calls are standardized by the POSIX specification
int dup (int oldfd); int dup2 (int oldfd, int newfd);
The former allocates the first available descriptor, just like open() behaves; an alternative way to duplicate a file descriptor to an unspecified place is the fcntl system call with F_DUPFD command.
The latter places a copy to a specified location (index), closing a file descriptor if it already exists.
Importance of dup2 for Unix shells [edit]
Unix shells use dup2 for input/output redirection. Along with pipe(), it is a tool on which Unix pipes rely.
| This section requires expansion. (March 2012) |
Use in Perl language [edit]
Perl does not provide these system calls directly. But dup2 to redefine one of standard streams may be invoked via "pipe opens" technique, using the vertical bar character in the file path for the open() call. Namely,
open FILE, "| shell command"
performs pipe(), fork(), dup2(…,STDIN) and then executes a specified command. Likewise,
open FILE, "shell command |"
performs pipe(), fork(), dup2(…,STDOUT) and then executes a specified command.
| This section requires expansion. (March 2012) |
See also [edit]
- File descriptor – how it works and other functions related to open
References [edit]
- Advanced Programming in the UNIX Environment by W. Richard Stevens ISBN 81-7808-096-6