dup (system call)

Source: Wikipedia, the free encyclopedia.

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

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.[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

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 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)
		die("pipe()");
 
	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)
        		die("dup()");
        	
        	/* now stdout and pdes[1] are equivalent (dup returns lowest free descriptor) */

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

		_exit(EXIT_SUCCESS);
	} else {
        	/* parent process */

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

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

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

		exit(EXIT_SUCCESS);
	}
 
	return 0;
}

void die(const char *msg) {
	perror(msg);
	exit(EXIT_FAILURE);
}

See also

References

  1. ^ "dup, dup2". opengroup.org.
  2. ^ "Linux Man Pages".
  • Advanced Programming in the UNIX Environment by W. Richard Stevens