practical examples use dup or dup2

101,234

Solution 1

One example use would be I/O redirection. For this you fork a child process and close the stdin or stdout file descriptors (0 and 1) and then you do a dup() on another filedescriptor of your choice which will now be mapped to the lowest available file descriptor, which is in this case 0 or 1.

Using this you can now exec any child process which is possibly unaware of your application and whenever the child writes on the stdout (or reads from stdin, whatever you configured) the data gets written on the provided filedescriptor instead.

Shells use this to implement commands with pipes, e.g. /bin/ls | more by connecting the stdout of one process to the stdin of the other.

Solution 2

The best scenario to understand dup and dup2 is redirection.
First thing we need to know is that the system has 3 default file ids(or variables indicating output or input sources) that deals with the input and output. They are stdin, stdout, stderr, in integers they are 0,1,2. Most of the functions like fprintf or cout are directly output to stdout.
If we want to redirect the output, one way is give, for example, fprintf function more arguments indicating in and out.
However, there is a more elegant way: we can overwrite the default file ids to make them pointing to the file we want to receive the output. dup and dup2 exactly work in this situation.
Let's start with one simple example now: suppose we want to redirect the output of fprintf to a txt file named "chinaisbetter.txt". First of all we need to open this file

int fw=open("chinaisbetter.txt", O_APPEND|O_WRONLY);

Then we want stdout to point to "chinaisbetter.txt" by using dup function:

dup2(fw,1);

Now stdout(1) points to the descriptor of "chinaisbetter.txt" even though it's still 1, but the output is redirected now.
Then you can use printf as normal, but the results will be in the txt file instead of showing directly on the screen:

printf("Are you kidding me? \n");

PS:

  1. This just gives a intuitive explanation, you may need to check the manpage or detailed information. Actually, we say "copy" here, they are not copying everything.

  2. The file id here is referring to the handler of the file. The file descriptor mentioned above is a struct the records file's information.

Solution 3

When you are curious about POSIX functions, especially those that seem to duplicate themselves, it's generally good to check the standard itself. At the bottom you will usually see examples, as well as reasoning behind the implementation (and existence) of both.

In this case:

The following sections are informative.

Examples

Redirecting Standard Output to a File

The following example closes standard output for the current processes, re-assigns standard output to go to the file referenced by pfd, and closes the original file descriptor to clean up.

#include <unistd.h>
...
int pfd;
...
close(1);
dup(pfd);
close(pfd);
...

Redirecting Error Messages

The following example redirects messages from stderr to stdout.

#include <unistd.h>
...
dup2(2, 1); // 2-stderr; 1-stdout
...

Application Usage

None.

Rationale

The dup() and dup2() functions are redundant. Their services are also provided by the fcntl() function. They have been included in this volume of IEEE Std 1003.1-2001 primarily for historical reasons, since many existing applications use them.

While the brief code segment shown is very similar in behavior to dup2(), a conforming implementation based on other functions defined in this volume of IEEE Std 1003.1-2001 is significantly more complex. Least obvious is the possible effect of a signal-catching function that could be invoked between steps and allocate or deallocate file descriptors. This could be avoided by blocking signals.

The dup2() function is not marked obsolescent because it presents a type-safe version of functionality provided in a type-unsafe version by fcntl(). It is used in the POSIX Ada binding.

The dup2() function is not intended for use in critical regions as a synchronization mechanism.

In the description of [EBADF], the case of fildes being out of range is covered by the given case of fildes not being valid. The descriptions for fildes and fildes2 are different because the only kind of invalidity that is relevant for fildes2 is whether it is out of range; that is, it does not matter whether fildes2 refers to an open file when the dup2() call is made.

Future Directions

None.

See also

close(), fcntl(), open(), the Base Definitions volume of IEEE Std 1003.1-2001, <unistd.h>

Change History

First released in Issue 1. Derived from Issue 1 of the SVID.

Solution 4

One practical example is redirecting output messages to some other stream like some log file. Here is a sample code for I/O redirection.
Please refer to original post here

#include <stdio.h>

main()
{
    int    fd;
    fpos_t pos;

    printf("stdout, ");

    fflush(stdout);
    fgetpos(stdout, &pos);
    fd = dup(fileno(stdout));
    freopen("stdout.out", "w", stdout);

    f();

    fflush(stdout);
    dup2(fd, fileno(stdout));
    close(fd);
    clearerr(stdout);
    fsetpos(stdout, &pos);        /* for C9X */

    printf("stdout again\n");
}

f()
{
printf("stdout in f()");
}

Solution 5

I/O redirection in the shell would most likely be implemented using dup2/fcnlt system calls.

We can easily emulate the $program 2>&1 > logfile.log type of redirection using the dup2 function.

The program below redirects both stdout and stderr .i.e emulates behavior of $program 2>&1 > output using the dup2.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int
main(void){
    int close_this_fd;
    dup2(close_this_fd = open("output", O_WRONLY), 1);
    dup2(1,2);
    close(close_this_fd);
    fprintf(stdout, "standard output\n");
    fprintf(stderr, "standard error\n");
    fflush(stdout);
    sleep(100); //sleep to examine the filedes in /proc/pid/fd level.
    return;
}

vagrant@precise64:/vagrant/advC$ ./a.out
^Z
[2]+  Stopped                 ./a.out
vagrant@precise64:/vagrant/advC$ cat output
standard error
standard output
vagrant@precise64:/vagrant/advC$ ll /proc/2761/fd
total 0
dr-x------ 2 vagrant vagrant  0 Jun 20 22:07 ./
dr-xr-xr-x 8 vagrant vagrant  0 Jun 20 22:07 ../
lrwx------ 1 vagrant vagrant 64 Jun 20 22:07 0 -> /dev/pts/0
l-wx------ 1 vagrant vagrant 64 Jun 20 22:07 1 -> /vagrant/advC/output
l-wx------ 1 vagrant vagrant 64 Jun 20 22:07 2 -> /vagrant/advC/output
Share:
101,234
pierrotlefou
Author by

pierrotlefou

Software Developer

Updated on July 05, 2022

Comments

  • pierrotlefou
    pierrotlefou almost 2 years

    I know what dup / dup2 does, but I have no idea when it would be used.

    Any practical examples?

    Thanks.

  • Kevin
    Kevin about 9 years
    @nshy: You can use regular dup() to copy stdout to another file descriptor before redirecting it. That way, you can unredirect it later. If you used dup2(), you would risk overwriting an existing descriptor.
  • Bionix1441
    Bionix1441 over 7 years
    What is the definition of redirection ? Everything you write to fd1 will be also written to fd2
  • Andrew
    Andrew over 6 years
    This is unhelpful. You might as well have said RTFM.
  • harry
    harry over 6 years
    This answer need a good s/fprintf/printf/g. fprintf writes to the explicitly named file descriptor. printf writes to stdout.
  • user1071847
    user1071847 over 5 years
    @krusty: See this answer to a different S.O. question.