Case studies use dup or dup2

I know what dup or dup2 does, but I have no idea when it will be used. Any practical examples? Thank.

+56
c unix file dup
Nov 12 '09 at 7:33
source share
4 answers

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

Using this, you can now execute any child process that may not know about your application and whenever a child writes to stdout (or reads from stdin, regardless of what you configured), the data is written to the provided filedescriptor.

Shells use this to implement pipe commandos, for example. /bin/ls | more /bin/ls | more by connecting the stdout of one process with the stdin of another.

+55
Nov 12 '09 at 7:44
source share

The best scenario for understanding dup and dup2 is redirection.
The first thing we need to know is that the system has 3 default file identifiers (or variables indicating output or input sources) that deal with input and output. They are stdin , stdout , stderr , in integers they are 0 , 1 , 2 . Most functions, such as fprintf or cout , are directly output to stdout .
If we want to redirect the output, one of the methods will give, for example, fprintf function of additional arguments indicating in and out .
However, there is a more elegant way: we can overwrite the default file IDs so that they point to the file we want to receive. dup and dup2 work exactly in this situation.
Let's start with a simple example: suppose we want to redirect the output of fprintf to a txt file called "chinaisbetter.txt". First of all, we need to open this file

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

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

 dup2(fw,1); 

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

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

PS

  • It just gives an intuitive explanation; you may need to check the man page or detailed information. In fact, we say "copy" here, they do not copy everything.

  • The file identifier here refers to a file handler. The file descriptor described above is a data structure of record files.

+12
Feb 12 '15 at 16:18
source share

When you are curious about the POSIX features, especially those that seem to duplicate themselves, it is usually good to check the standard itself . Below you will usually see examples, as well as discussions about 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 the standard output for current processes, reassigns standard output to go to the file referenced by pfd , and closes the original file descriptor for cleaning.

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

Error message redirection

The following example redirects messages from stderr to stdout .

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

Using the application

Absent.

Justification

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

While the short code segment shown is very similar in behavior to dup2() , the corresponding implementation based on other functions defined in this volume of IEEE Std 1003.1-2001 is much more complicated. The least obvious is the possible effect of the signal capture function, which can be called between steps, and allocate or free file descriptors. This can be avoided by blocking the signals.

The dup2() function is not deprecated since it is a safe version of the functionality provided in an unsafe version of the fcntl() . It is used in POSIX Ada bindings.

The dup2() function is not intended to be used in critical areas as a synchronization mechanism.

In the [EBADF] description, the case where fildes is out of range is covered by this case when fildes are invalid. The descriptions for fildes and fildes2 different because the only kind of invalidity that matters to fildes2 is out of range; that is, it does not matter if fildes2 to an open file when calling dup2() .

Future directions

Absent.

see also

close() , fcntl() , open() , IEEE Std 1003.1-2001 basic definitions volume, <unistd.h>

Change history

The first one was released in release 1. Derived from number 1 from SVID.

+9
Nov 12 '09 at 7:44
source share

One practical example is redirecting output messages to another stream, for example, to some log file. Here is sample code for redirecting I / O.
Please refer to the 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()"); } 
+3
Nov 12 '09 at 8:24
source share



All Articles