Dup (system Call)

In Unix-like operating systems, dup (short for "duplicate") 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.

C library POSIX definition

The dup and dup2 calls are standardized by the POSIX specification.[1]

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 the copy into newfd. If newfd is open, it is closed first.

dup2 for input/output redirection

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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* function prototypes */
void die(const char*);
int main(int argc, char **argv) {
	int pdes[2];
	pid_t child;
	if(pipe(pdes) == -1)
	child = fork;
	if(child == (pid_t)(-1))
        	die("fork"); /* fork failed */
	if(child == (pid_t)0) {
        	/* child process */

        	close(1);       /* close stdout */
        	if(dup(pdes[1]) == -1)
        	/* now stdout and pdes[1] are equivalent (dup returns lowest free descriptor) */

        	if((execlp("program1", "program1", "arg1", NULL)) == -1)

	} else {
        	/* parent process */

        	close(0);       /* close stdin */
        	if(dup(pdes[0]) == -1)

        	/* now stdin and pdes[0] are equivalent (dup returns lowest free descriptor) */

        	if((execlp("program2", "program2", "arg1", NULL)) == -1)

	return 0;

void die(const char *msg) {

See also


  1. ^ "dup, dup2". opengroup.org.
  • Advanced Programming in the UNIX Environment by W. Richard Stevens ISBN 81-7808-096-6

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at defaultlogic.com. Create an account using LinkedIn to manage and organize your omni-channel knowledge. defaultlogic.com is like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us