Dup (system Call)
   HOME

TheInfoList



OR:

In
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating systems, (short for "duplicate") and
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
s create a copy of a given
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically have ...
. This new descriptor actually does not behave like a copy, but like an
alias Alias may refer to: * Pseudonym * Pen name * Nickname Arts and entertainment Film and television * ''Alias'' (2013 film), a 2013 Canadian documentary film * ''Alias'' (TV series), an American action thriller series 2001–2006 * ''Alias the ...
of the old one.


C library POSIX definition

The dup and dup2 calls are standardized by the
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
specification. Similar(following the logic) to pointers, the new file description is merely an alias to the old one, with both file descriptors being capable of being used interchangeably. Both file descriptors in a dup() system call refer to the same open file description which means they share file offset and file status flags; Similar but not identical to the logic used in pointers, shallow or deep copying or
references Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''name'' ...
, changes to the offset on one of the
file descriptors In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (Handle (computing), handle) for a file (computing), file or other input/output System resource, resource, such as a pip ...
changes it for the other file descriptor. When using dup(), the two file descriptors don't share the same file descriptor flags. In the calling process the lowest numbered unused file descriptor will be used for the new file descriptor number. When using the dup2()
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
it performs the same task as dup() with the exception of using the file descriptor number specified in the newfd variable of the call, in that newfd is adjusted to refer to the oldff file description. The last system call in this family of functions is dup3(); Which is the same as dup2() except that if oldfd equals newfd the system call fails with error EINVAL and the caller can force the close-on-exec flag to be set by specifying O_CLOEXEC in flags. dup3() was formally added to
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
kernel Kernel may refer to: Computing * Kernel (operating system), the central component of most operating systems * Kernel (image processing), a matrix used for image convolution * Compute kernel, in GPGPU programming * Kernel method, in machine learnin ...
version 2.6.27 (glibc support is available on version 2.9 and above). 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 In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (Handle (computing), handle) for a file (computing), file or other input/output System resource, resource, such as a pip ...
system call with F_DUPFD command. The latter places the copy into newfd. If newfd is open, it is
closed Closed may refer to: Mathematics * Closure (mathematics), a set, along with operations, for which applying those operations on members always results in a member of the set * Closed set, a set which contains all its limit points * Closed interval, ...
first.


dup2 for input/output redirection

Unix shell A Unix shell is a command-line Interpreter (computing), interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting langua ...
s use for input/output redirection. Along with pipe(), it is a tool on which
Unix pipe In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (''stdou ...
s rely. The following example uses pipe() and dup() in order to connect two separate processes (''program1'' and ''program2'') using
Unix pipe In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (''stdou ...
s: #include #include #include /* function prototypes */ void die(const char*); int main(int argc, char **argv) void die(const char *msg)


See also

*
File descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically have ...
 – how it works and other functions related to open


References

* Advanced Programming in the UNIX Environment by W. Richard Stevens {{POSIX system calls C POSIX library System calls