Dup2 / dup - why should I duplicate a file descriptor?

I am trying to understand the use of dup2 and dup .

On the man page:

 DESCRIPTION dup and dup2 create a copy of the file descriptor oldfd. After successful return of dup or dup2, the old and new descriptors may be used interchangeably. They share locks, file position pointers and flags; for example, if the file position is modified by using lseek on one of the descriptors, the position is also changed for the other. The two descriptors do not share the close-on-exec flag, however. dup uses the lowest-numbered unused descriptor for the new descriptor. dup2 makes newfd be the copy of oldfd, closing newfd first if necessary. RETURN VALUE dup and dup2 return the new descriptor, or -1 if an error occurred (in which case, errno is set appropriately). 

Why do I need this system call? What is file descriptor duplication?

If I have a file descriptor, why should I copy it?

I would be grateful if you could explain and give me an example where dup2 / dup is required.

thank

+54
c linux system-calls operating-system
Jul 24 '12 at 16:24
source share
4 answers

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

Dup allows shells to implement such commands:

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

2> & 1 tells the shell to give the command to file descriptor 2, which is a duplicate of descriptor 1. (that is, stderr and stdout point to the same fd).
Now the error message for calling ls on a nonexistent file and the correct ls output in the existing file are displayed in the tmp1 file.

The following code example starts the wc program with a standard connection to the read end of the 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 ie 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 duplicates the end of reading on file descriptor 0, closes the file de scripts in p and execs wc. When wc reads from its standard input, it reads from the pipe.
This is how pipes are implemented using dup, so using a dup, you use a channel to build something else, that the beauty of system calls you build one thing after another using existing tools, this tool was built using something something else. In the end, system calls are the most basic tools you get in the kernel.

Greetings :)

+28
Jul 24. '12 at 17:20
source share

Another reason for duplicating a file descriptor is to use fdopen . fclose closes the file descriptor that was passed to fdopen , so if you do not want the original file descriptor to be closed, you must first duplicate it with dup .

+12
Jul 24 '12 at 17:53
source share

Some points related to dup / dup2 can be noted, please

dup / dup2. Technically, the goal is to split a single file table with a record inside a single process by different descriptors. (If we are looking for a descriptor, by default we duplicate in the child process, and the record in the file table is also split).

This means that we can have more than one file descriptor, which may have different attributes for one separate open file table entry using the dup / dup2 function.

(Although currently only the FD_CLOEXEC flag is the only attribute for the file descriptor).

http://www.gnu.org/software/libc/manual/html_node/Descriptor-Flags.html

 dup(fd) is equivalent to fcntl(fd, F_DUPFD, 0); dup2(fildes, fildes2); is equivalent to close(fildes2); fcntl(fildes, F_DUPFD, fildes2); 

Differences (for the latter) - In addition to some errno value, starting with dup2 and fcntl close, followed by fcntl, can increase the conditions of the race, as two function calls are involved.

Details can be checked from http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html

Usage Example -

One interesting example when implementing job management in a shell where you can see the use of dup / dup2 .. in the link below

http://www.gnu.org/software/libc/manual/html_node/Launching-Jobs.html#Launching-Jobs

+4
Jul 24 '12 at 19:01
source share

dup is used to redirect output from a process.

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

+3
Jul 24 2018-12-12T00:
source share



All Articles