DUP () system call

Source: Internet
Author: User
Today, see DUP and dup2 system calls, is not too understanding, first write a little simple application examples.

DUP and dup2 are used to copy file descriptors.

Function Prototypes:
#include
int dup (int oldfd);
int dup2 (int oldfd,int newfd);


DUP is used to copy the file descriptor that the OLDFD refers to. However, when the copy succeeds, it returns the smallest file descriptor that has not yet been used. If there is an error return-1, the error code is stored in errno. The new file descriptor and parameters returned OLDFD point to the same file, sharing all locks, reading and writing pointers, and various permissions or flags.
Dup2 can specify the numeric value of the new file descriptor with the parameter NEWFD. If the NEWFD is already in use by the program, the system closes it to release the file descriptor, and if NEWFD is equal to OLDFD, DUP2 returns NEWFD without closing him. The DUP2 call successfully returns a new file descriptor, and an error returns-1.

Standard input (stdin), standard output (STDOUT), standard error message (stderr) file number is 0,1,2

A simple example: first in the current directory exists a file Mytest2, the contents of the file is Hhhhhhhhhhhh

#include
#include
#include
#include
#include
#include

int main ()
{
int OLDFD;

OLDFD = open ("Mytest2", o_rdwr| o_creat,0644);
Dup2 (oldfd,1); Copy OLDFD to file descriptor 1 (stdout standard output)
Close (OLDFD); Close File Descriptor OLDFD
printf ("ddd"); The DDD is printed on standard output when the standard output has been replaced by the OLDFD file descriptor
return 0; The content printed to the standard output is printed to the file mytest2.
}

The result of program execution changes to the contents of the file Mytest2: dddhhhhhhhhh

Program instance: File name is file5.c

#include
#include
#include
#include
#include
#include

int main ()
{
int FD;
int i;
if (Fd=open ("Mytest3", o_creat| o_rdwr,0644) ==-1) {
printf ("Open File error!");
Exit (1);
}

Close (1); Turn off standard output
DUP (FD); Copy file descriptor FD to 1
Close (FD);

printf ("Writ to file\n");
return 0;
}

Program Runtime Results:
moalong@xiyoulinux-desktop:~/along/code/c/part5$ make File5
CC File5.c-o File5
moalong@xiyoulinux-desktop:~/along/code/c/part5$./file5
moalong@xiyoulinux-desktop:~/along/code/c/part5$ Cat Mytest3
Writ to File

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.