Linux system Programming--copy of File descriptor DUP () and dup2 () "Go"

Source: Internet
Author: User
Tags strlen



This article was reproduced from: http://blog.csdn.net/tennysonsky/article/details/45870459



DUP () and dup2 () are two very useful system calls that are used to copy the descriptor of a file so that the new file descriptor also identifies the file identified by the old file descriptor.






This process is similar to the real life of the key, the key equivalent to the file descriptor, the lock equivalent to a file, a key to open a lock, equivalent to a file descriptor corresponding to a file, now, we go to the key, with the old key copied a new key, so that Both the old key and the new key can open the lock. In contrast to the DUP (), dup2 (), a new file descriptor is copied from the original file descriptor, so that both the original file descriptor and the new file descriptor point to the same file, and we manipulate any one of the two file descriptors to manipulate the corresponding file.






Required header file:


#include <unistd.h>



int dup (int oldfd);



Function:


A new file descriptor is copied through OLDFD, and the new file descriptor is the smallest available file descriptor in the calling process file descriptor table, and the final OLDFD and the new file descriptor point to the same file.


Parameters:


OLDFD: The file descriptor that needs to be copied OLDFD


return value:


Success: New File descriptor

Failed:-1





The following example opens a file, copies the file descriptor, and writes the file separately by 2 descriptors:





[CPP]View PlainCopy

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main (int argc, char *argv[])
{
int fd1;
int fd2;
//Open File
FD1 = open ("1.txt", o_creat|  O_wronly, 0666);
if (Fd1 < 0) {
Perror ("open");
Exit (-1);
}
printf ("fd1 ==============%d\n", FD1);
//through FD1 copy out Fd2, final fd1, fd2 all point to 1.txt
FD2 = DUP (FD1);
printf ("Fd2 ==============%d\n", FD2);
char *buf1 = "This was a test for fd1\n";
//Operation FD1 file descriptor
Write (FD1, BUF1, strlen (BUF1));
char *buf2 = "This was a test for fd2\n";
//Operation FD2 file descriptor
Write (FD2, Buf2, strlen (BUF2));
//Close file descriptor, two off
Close (FD1);
Close (FD2);
return 0;
}








The results of the operation are as follows:









The result of the above operation shows that thenew file descriptor copied after DUP () is the smallest available file descriptor automatically assigned by the system.






Next, let's write an example of what would have been shown to the screen through printf (), not on the screen, but in a file:





[CPP]View PlainCopy

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main (int argc, char *argv[])
{
int fd1;
int fd2;
//Open File
FD1 = open ("1.txt", o_creat|  O_wronly, 0666);
if (Fd1 < 0) {
Perror ("open");
Exit (-1);
}
printf ("fd1 ==============%d\n", FD1);
//1 original pointing to standard output device (e.g., display)
//Turn off the 1 file descriptor, which means that 1 no longer points to the standard output device
//In this case, the 1 file descriptor is idle, it becomes the smallest available file descriptor
Close (1);
//through FD1 copy out Fd2, final fd1, fd2 all point to "1.txt"
//The system assigns the FD2 a minimum available file descriptor of 1, which is FD2 = 1
FD2 = DUP (FD1);
//The contents of the following sentence will not be printed on the screen, but will be written in the file "1.txt"
//printf () is a standard library function and will eventually call the system call function write ()
///equal to this, write (1,), to 1 file descriptor writing content,
//By default, 1 file descriptors point to standard output devices (e.g., display)
//So, the contents of printf () first appear on the screen
//But now 1 file descriptor points to file "1.txt"
//So, the contents of printf () are written to the file "1.txt"
printf ("Fd2 ==============%d\n", FD2);
Close (FD1);
Close (FD2);
return 0;
}









The results of the operation are as follows:









Next, we continue to learn the use of dup2 (), which is exactly the same as the DUP (), but the new file descriptor Dup2 () can specify any valid number.






int dup2 (int oldfd, int newfd);



Function:


A new file descriptor NEWFD is copied through OLDFD, and if successful, the NEWFD and function return values are the same return value, and the final OLDFD and the new file descriptor NEWFD all point to the same file.


Parameters:


OLDFD: File descriptor that needs to be copied

NEWFD: The new file descriptor, which can be used to specify a legal number (0-1023), if the specified number of children is already occupied (and a file associated with), this function will automatically close the close () disconnect the number and a file association, and then use this legal number.


return value:


Success: Return to NEWFD

Failure: Return-1





Next, we will replace the above example with dup2 () to achieve:





[CPP]View PlainCopy

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main (int argc, char *argv[])
{
int fd1;
int fd2;
//Open File
FD1 = open ("1.txt", o_creat|  O_wronly, 0666);
if (Fd1 < 0) {
Perror ("open");
Exit (-1);
}
printf ("fd1 ==============%d\n", FD1);
//through FD1 copy out Fd2, final fd1, fd2 all point to "1.txt"
//Specify the value of FD2 to 1, 1 to the standard output device, first close (), then copy
FD2 = Dup2 (fd1, 1);
//The contents of the following sentence will not be printed on the screen, but will be written in the file "1.txt"
//printf () is a standard library function and will eventually call the system call function write ()
///equal to this, write (1,), to 1 file descriptor writing content,
//By default, 1 file descriptors point to standard output devices (e.g., display)
//So, the contents of printf () first appear on the screen
//But now 1 file descriptor points to file "1.txt"
//So, the contents of printf () are written to the file "1.txt"
printf ("Fd2 ==============%d\n", FD2);
Close (FD1);
Close (FD2);
return 0;
}









The results of the operation are as follows:












Linux system Programming--copy of File descriptor DUP () and dup2 () "Go"


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.