dup (system call)
Template:Refimprove In Unix-like operating systems, Page Template:Mono/styles.css has no content.dup (short for "duplicate") and Page Template:Mono/styles.css has no content.dup2 system calls create a copy of a given file descriptor. This new descriptor actually does not behave like a copy, but like an alias of the old one.
C library POSIX definition
The Page Template:Mono/styles.css has no content.dup() and Page Template:Mono/styles.css has no content.dup2() calls are standardized by the POSIX specification.[1] 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 Page Template:Mono/styles.css has no content.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, changes to the offset on one of the file descriptors changes it for the other file descriptor. When using Page Template:Mono/styles.css has no content.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 Page Template:Mono/styles.css has no content.dup2() system call it performs the same task as Page Template:Mono/styles.css has no content.dup() with the exception of using the file descriptor number specified in the Page Template:Mono/styles.css has no content.newfd variable of the call, in that Page Template:Mono/styles.css has no content.newfd is adjusted to refer to the Page Template:Mono/styles.css has no content.oldfd file description. The last system call in this family of functions is Page Template:Mono/styles.css has no content.dup3(), which is the same as Page Template:Mono/styles.css has no content.dup2() except that if Page Template:Mono/styles.css has no content.oldfd equals Page Template:Mono/styles.css has no content.newfd the system call fails with error Page Template:Mono/styles.css has no content.EINVAL and the caller can force the close-on-exec flag to be set by specifying Page Template:Mono/styles.css has no content.O_CLOEXEC in flags.[2] dup3() was formally added to Linux kernel 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 Page Template:Mono/styles.css has no content.open() behaves; an alternative way to duplicate a file descriptor to an unspecified place is the Page Template:Mono/styles.css has no content.fcntl system call with F_DUPFD command.
The latter places the copy into Page Template:Mono/styles.css has no content.newfd. If Page Template:Mono/styles.css has no content.newfd is open, it is closed first.
dup2 for input/output redirection
Page Module:Message box/ambox.css has no content.
| [icon] | This section needs expansion. You can help by Template:Protected page maintenance message. (March 2012) |
Unix shells use Page Template:Mono/styles.css has no content.dup2 for input/output redirection. Along with pipe(), it is a tool on which Unix pipes rely.
The following example uses pipe() and dup() in order to connect two separate processes (Page Template:Mono/styles.css has no content.program1 and Page Template:Mono/styles.css has no content.program2) using Unix pipes:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void die(const char msg[]) {
perror(msg);
exit(EXIT_FAILURE);
}
int main() {
int pdes[2];
if (pipe(pdes) == -1) {
die("pipe()");
}
pid_t child = fork();
if (child == (pid_t)(-1)) {
die("fork() failed");
}
if (child == (pid_t)0) {
// child process
close(STDOUT_FILENO); // close stdout
if (dup(pdes[1]) == -1) {
die("dup() failed");
}
// now stdout and pdes[1] are equivalent:
// dup returns lowest free descriptor
if ((execlp("program1", "program1", "arg1", NULL)) == -1) {
die("execlp() failed");
}
} else {
// parent process
close(STDIN_FILENO); // close stdin
if (dup(pdes[0]) == -1) {
die("dup() failed");
}
// now stdin and pdes[0] are equivalent:
// dup returns lowest free descriptor
if ((execlp("program2", "program2", "arg1", NULL)) == -1) {
die("execlp() failed");
}
}
return 0;
}
See also
- File descriptor – how it works and other functions related to open
References
Page Template:Reflist/styles.css has no content.
- ^ Page Module:Citation/CS1/styles.css has no content."dup, dup2". opengroup.org.
- ^ Page Module:Citation/CS1/styles.css has no content."Linux Man Pages".
- Advanced Programming in the UNIX Environment by W. Richard Stevens Template:ISBN