Image Understanding dup and dup2 Functions

Source: Internet
Author: User
Image Understanding dup and dup2 functions-general Linux technology-Linux programming and kernel information. The following is a detailed description. I believe that most programmers programming in Unix/Linux have the "APUE in Unix environment" Super classic masterpiece. In this book, the author explains dup/dup2 previously mentioned "file sharing", which is helpful for understanding dup/dup2. Here is a simple excerpt for later analysis:
Steven s said:
(1) Each process has a record item in the Process Table. Each record item contains an open file descriptor table, which can be considered as a vector, and each descriptor occupies one item. Associated with each file descriptor is:
(A) file descriptor flag.
(B) pointer to a file table item.
(2) The kernel maintains a file table for all open files. Each file table item includes:
(A) File status signs (read, write, add write, synchronization, non-blocking, etc ).
(B) Current file displacement.
(C) pointer to the table entry of the file v node.
Figure:
File descriptor table
------------
Fd0 0 | p0 -------------> File Table 0 ---------> vnode0
------------
Fd1 1 | p1 -------------> File Table 1 ---------> vnode1
------------
Fd2 2 2 | p2
------------
Fd3 3 3 | p3
------------
......
......
------------

1. dup and dup2 in a single process
Assume that process A has an opened file descriptor fd3, and its status is as follows:
Process A's file descriptor table (before dup2)
------------
Fd0 0 | p0
------------
Fd1 1 | p1 -------------> File Table 1 ---------> vnode1
------------
Fd2 2 2 | p2
------------
Fd3 3 3 | p3 -------------> File Table 2 ---------> vnode2
------------
......
......
------------

It is called as follows:
N_fd = dup2 (fd3, STDOUT_FILENO); The process status is as follows:

Process A's file descriptor table (after dup2)
------------
Fd0 0 | p0
------------
N_fd 1 | p1 ------------
------------\
Fd2 2 2 | p2 \
------------ _ \ |
Fd3 3 3 | p3 -------------> File Table 2 ---------> vnode2
------------
......
......
------------
Explanation:
N_fd = dup2 (fd3, STDOUT_FILENO) indicates that n_fd and fd3 share a file table item (their file table Pointer Points to the same file table item ), the n_fd position in the file descriptor table is STDOUT_FILENO, and the file table item pointed to by the original STDOUT_FILENO is disabled. I think this should be clearly reflected. According to the above explanation, we can explain some of the problems raised in CU:
(1) "Must the first parameter of dup2 be a valid enabled filedes? "-- Answer: required.
(2) "Can the second parameter of dup2 be a filedes value in any valid range? "-- Answer: Yes. In Unix, the value range is [0,255].

In addition, I feel that a good way to understand dup2 is to regard fd as a struct type, as shown in the figure above. We may wish to define it:
Struct fd_t {
Int index;
Filelistitem * ptr;
};
Then, dup2 matches the index and modifies the ptr to complete the dup2 operation.

When studying dup2, you always encounter the word "redirection", which completes a "redirection from standard output to file ", after dup2, any I/O operations such as printf with the target STDOUT_FILENO of process A will flow into the file corresponding to fd3. The following is an example program:
# Define TESTSTR "Hello dup2 \ n"
Int main (){
Int fd3;

Fd3 = open ("testdup2.dat", 0666 );
If (fd <0 ){
Printf ("open error \ n ");
Exit (-1 );
}

If (dup2 (fd3, STDOUT_FILENO) <0 ){
Printf ("err in dup2 \ n ");
}
Printf (TESTSTR );
Return 0;
}
The result is that you can see "Hello dup2" in testdup2.dat ".

2. Restore after redirection
There is such a post on CU, that is, how to restore the original state after redirection? First, you can think of saving the file descriptor before redirection. So how to save it, like below?
Int s_fd = STDOUT_FILENO;
Int n_fd = dup2 (fd3, STDOUT_FILENO );
Or can this happen?
Int s_fd = dup (STDOUT_FILENO );
Int n_fd = dup2 (fd3, STDOUT_FILENO );
What is the difference between the two methods? The answer is that the second solution is correct. The analysis is as follows: according to the first method, we only saved the index equivalent to fd_t (according to the method I mentioned earlier) on the surface, after calling dup2, the file table items pointed to by ptr are closed because the Count value is zero. If we call dup2 (s_fd, fd3) Again) an error occurs (the cause of the error is described above ). In the second method, we first perform a copy. The copied status is shown in:
Process A's file descriptor table (after dup)
------------
Fd0 0 | p0
------------
Fd1 1 | p1 -------------> File Table 1 ---------> vnode1
------------/|
Fd2 2 2 | p2/
------------/
Fd3 3 3 | p3 -------------> File Table 2 ---------> vnode2
------------/
S_fd 4 | p4 ------/
------------
......
......
------------

After calling dup2, the status is:
Process A's file descriptor table (after dup2)
------------
Fd0 0 | p0
------------
N_fd 1 | p1 ------------
------------\
Fd2 2 2 | p2 \
------------ _ \ |
Fd3 3 3 | p3 -------------> File Table 2 ---------> vnode2
------------
S_fd 4 | p4 -------------> File Table 1 ---------> vnode1
------------
......
......
------------
The meaning of dup (fd) is that the new file descriptor returned shares a file table item with fd. Just like s_fd and fd1 shared file table 1 in the after dup diagram.

After determining the second scheme, it is easy to restore the redirection. You only need to call dup2 (s_fd, n_fd. The following is a complete example program:
# Define TESTSTR "Hello dup2 \ n"
# Define SIZEOFTESTSTR 11

Int main (){
Int fd3;
Int s_fd;
Int n_fd;

Fd3 = open ("testdup2.dat", 0666 );
If (fd3 <0 ){
Printf ("open error \ n ");
Exit (-1 );
}

/* Copy the standard output descriptor */
S_fd = dup (STDOUT_FILENO );
If (s_fd <0 ){
Printf ("err in dup \ n ");
}

/* Redirect standard output to file */
N_fd = dup2 (fd3, STDOUT_FILENO );
If (n_fd <0 ){
Printf ("err in dup2 \ n ");
}
Write (STDOUT_FILENO, TESTSTR, SIZEOFTESTSTR);/* write to testdup2.dat */

/* Redirect recovery standard output */
If (dup2 (s_fd, n_fd) <0 ){
Printf ("err in dup2 \ n ");
}
Write (STDOUT_FILENO, TESTSTR, SIZEOFTESTSTR);/* output to the screen */
Return 0;
}
Note that when I output data, I use a write library function without buffering. If I use a printf with a buffer, the final result is two rows of "Hello dup2" output on the screen, and the file testdup2.dat is empty because the buffer is faulty because the final target is the screen, so the program finally outputs the buffer content to the screen.


3. dup/dup2 between parent and child Processes
The same file descriptor of the child process and the parent process called by fork shares the same file table item, as shown in:
File descriptor table of parent process
------------
Fd0 0 | p0
------------
Fd1 1 | p1 -------------> File Table 1 ---------> vnode1
------------/| \
Fd2 2 2 | p2 |
------------ |
|
File descriptor table of sub-process B |
------------ |
Fd0 0 | p0 |
------------ |
Fd1 1 | p1 ------------------- |
------------
Fd2 2 2 | p2
------------
Therefore, the proper use of dup2 and dup can establish a "communication bridge" between the parent and child processes ". This is not detailed here.

Iv. Summary
The flexible use of dup/dup2 can bring you a lot of powerful functions, it took some time to summarize the above so much, do not know whether you understand thoroughly, it can only be explored in future practices.

References:
1. Advanced Programming in Unix environment
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.