DUP and dup2 calls

Source: Internet
Author: User

DUP and dup2 are two very useful calls. They are used to copy the descriptor of a file. They are often used to redirect stdin, stdout, and stderr processes. The two functions are prototype as follows:
# Include
Int DUP (INT oldfd );
Int dup2 (INT oldfd, int targetfd)
Using the DUP function, we can copy a descriptor. If a new descriptor is returned to the function, the new descriptor is a copy of the descriptor sent to the function. This means that the two descriptors share the same
Data Structure
. For example, if we perform the lseek operation on a file descriptor, the location of the first file is the same as that of the Second file. The following code snippet describes how to use the DUP function:
Int fd1, fd2;
...
Fd2 = DUP (fd1 );
Note that we can create a descriptor before calling fork, which is the same as calling DUP to create a descriptor. The sub-process will also receive a copied descriptor.

The dup2 function is similar to the DUP function, but the dup2 function allows the caller to specify the ID of a valid Descriptor and a target descriptor. When the dup2 function is returned successfully
Two Parameters) will be changed to a replica of the source Descriptor (the first parameter of the dup2 function). In other words, both file descriptors currently point to the same file, and is the file pointed to by the first parameter of the function. Below
We use a piece of code to describe:
Int oldfd;
Oldfd = open ("app_log", (o_rdwr | o_create), 0644 );
Dup2 (oldfd, 1 );
Close (oldfd );

In this example, we open a new file called "app_log" and receive a file descriptor named fd1. We call the dup2 function. The parameters are oldfd and 1. This will
This causes us to replace the file descriptor represented by 1 with the newly opened file descriptor (that is, stdout, because the ID of the standard output file is 1 ). All things written to stdout will be changed
Enter the file named "app_log. It should be noted that after the dup2 function copies the oldfd, it will immediately turn it off, but will not turn off the newly opened file descriptor, because the file description
Character 1 also points to him.
Next we will introduce a more in-depth sample code. Recall the command line pipeline mentioned earlier in this article, where we will? 1 is the standard output of the command connected to WC as the standard input? L command. Next, we will use a C program to illustrate the implementation of this process. Sample Code 3 is shown in the following code.

In sample code 3, first create an MPS queue in the Code in line 9th, and then divide the application into two processes: one sub-process (13th? Line 16) and a parent process (20th? 23 rows ). Next,
In the sub-process, first disable the stdout Descriptor (row 13th), and then provide the LS
? 1 command function, but it is not written to stdout (13th rows), but to the input end of the pipeline we created, which is redirected through the DUP function. In row 3, dup2 is used.
The function redirects stdout to the pipeline (PFDS [1]). Then, immediately turn off the input of the pipeline. Then, use the execlp function to replace the sub-process image with the command ls
? 1. Once the command is executed, all the output will be sent to the input end of the pipeline.

At present, we will study the receiver of the pipeline. From the code, we can see that the receiver of the pipeline is undertaken by the parent process. First, disable the stdin Descriptor (20th rows), because we will not use the keyboard level of the machine.
The quasi-Device File receives data input, but receives data from the output of other programs. Then, the dup2 function (line 1) is used again to change stdin to the output end of the pipeline.
The block descriptor 0 (that is, the conventional stdin) is equal to PFDS [0. Close the stdout end of the MPs Queue (PFDS [1]) because it is not used here. Finally, use
The execlp function replaces the parent process image with the WC-1 process image. The WC-1 command uses the content of the pipeline as its input (line 1 ).
Example code 3: Use C to implement code for command line operations
1: # include
2: # include
3: # include
4:
5: int main ()
6 :...{
7: int PFDS [2];
8:
9: If (pipe (PFDS) = 0 )...{
10:
11: If (Fork () = 0 )...{
12:
13: Close (1 );
14: dup2 (PFDS [1], 1 );
15: Close (PFDS [0]);
16: execlp ("ls", "ls", "-1", null );
17:
18:} else ...{
19:
20: Close (0 );
21: dup2 (PFDS [0], 0 );
22: Close (PFDS [1]);
23: execlp ("WC", "WC", "-l", null );
24:
25 :}
26:
27 :}
28:
29: Return 0;
30 :}
In this program, we need to pay special attention to the fact that our sub-process redirects the output of the pipeline input, and then the parent process redirects its input to the output of the pipeline.

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.