dup2 / dup - why would I need to duplicate a file descriptor? dup2 / dup - why would I need to duplicate a file descriptor? c c

dup2 / dup - why would I need to duplicate a file descriptor?


The dup system call duplicates an existing file descriptor, returning a new one thatrefers to the same underlying I/O object.

Dup allows shells to implement commands like this:

ls existing-file non-existing-file > tmp1  2>&1

The 2>&1 tells the shell to give the command a file descriptor 2 that is a duplicate of descriptor 1. (i.e stderr & stdout point to same fd).
Now the error message for calling ls on non-existing file and the correct output of ls on existing file show up in tmp1 file.

The following example code runs the program wc with standard input connectedto the read end of a pipe.

int p[2];char *argv[2];argv[0] = "wc";argv[1] = 0;pipe(p);if(fork() == 0) {    close(STDIN); //CHILD CLOSING stdin    dup(p[STDIN]); // copies the fd of read end of pipe into its fd i.e 0 (STDIN)    close(p[STDIN]);    close(p[STDOUT]);    exec("/bin/wc", argv);} else {    write(p[STDOUT], "hello world\n", 12);    close(p[STDIN]);    close(p[STDOUT]);}

The child dups the read end onto file descriptor 0, closes the file descriptors in p, and execs wc. When wc reads from its standard input, it reads from thepipe.
This is how pipes are implemented using dup, well that one use of dup now you use pipe to build something else, that's the beauty of system calls,you build one thing after another using tools which are already there , these tool were inturn built using something else so on ..At the end system calls are the most basic tools you get in kernel

Cheers :)


Another reason for duplicating a file descriptor is using it with fdopen. fclose closes the file descriptor that was passed to fdopen, so if you don't want the original file descriptor to be closed, you have to duplicate it with dup first.


dup is used to be able to redirect the output from a process.

For example, if you want to save the output from a process, you duplicate the output (fd=1), you redirect the duplicated fd to a file, then fork and execute the process, and when the process finishes, you redirect again the saved fd to output.