Wed . 19 May 2019

dup (system call)

In Unix-like operating systems, dup and 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


  • 1 C library POSIX definition
  • 2 Importance of dup2 for Unix shells
  • 3 Use in Perl language
  • 4 See also
  • 5 References

C library POSIX definitionedit

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 shellsedit

Unix shells use 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 program1 and program2 using Unix pipes:

#include <stdioh> #include <stdlibh> #include <unistdh> / function prototypes / void dieconst char; int mainint argc, char argv else return 0; void dieconst char msg

Use in Perl languageedit

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

See alsoedit

  • File descriptor – how it works and other functions related to open


  • Advanced Programming in the UNIX Environment by W Richard Stevens ISBN 81-7808-096-6

dup (system call) Information about

dup (system call)

  • user icon

    dup (system call) beatiful post thanks!


dup (system call)
dup (system call)
dup (system call) viewing the topic.
dup (system call) what, dup (system call) who, dup (system call) explanation

There are excerpts from wikipedia on this article and video

Random Posts

Modern philosophy

Modern philosophy

Modern philosophy is a branch of philosophy that originated in Western Europe in the 17th century, a...
Tim Shadbolt

Tim Shadbolt

Timothy Richard "Tim" Shadbolt born 19 February 1947 is a New Zealand politician He is the Mayor of ...
HK Express

HK Express

Andrew Cowen Deputy CEO Website wwwhkexpresscom HK Express Traditional Chinese 香港快運航空...
List of shrinking cities in the United States

List of shrinking cities in the United States

The following municipalities in the United States have lost at least 20% of their population, from a...