Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

practical examples use dup or dup2

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

Any practical examples?

Thanks.

like image 659
pierrotlefou Avatar asked Nov 12 '09 07:11

pierrotlefou


People also ask

What is dup2 used for?

The dup2() system function is used to create a copy of an existing file descriptor. In Linux, there are 3 standard file descriptors. They are: stdin: This is the standard input file descriptor.

What is the difference between dup () and dup2 ()?

The difference between dup and dup2 is that dup assigns the lowest available file descriptor number, while dup2 lets you choose the file descriptor number that will be assigned and atomically closes and replaces it if it's already taken.

What does dup2 () do in C?

The dup2() function duplicates an open file descriptor. Specifically, it provides an alternate interface to the service provided by the fcntl() function using the F_DUPFD constant command value, with fildes2 for its third argument. The duplicated file descriptor shares any locks with the original.

Which of the following functions are useful for duplicating file descriptors?

Which of the following system call is used for duplicating file descriptor? Explanation: The dup system call supplicates the file descriptor fields and returns the lowest number available for allocation.


2 Answers

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.

like image 122
Alfonso Avatar answered Sep 21 '22 05:09

Alfonso


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.

like image 40
ZijunLost Avatar answered Sep 24 '22 05:09

ZijunLost