- 2
- 0
- 约 15页
- 2017-10-05 发布于河南
- 举报
linux 下dup和dup2(Linux under DUP and dup2)
linux 下dup和dup2(Linux under DUP and dup2)
DUP and dup2 are also two very useful calls that are used to copy descriptors of a file.
They are often used to redirect processes stdin, stdout, and stderr.
The prototype of these two functions is as follows:
#include unistd.h
Int dup (int oldfd);
Int dup2 (int, oldfd, int, targetfd)
Using the function DUP, we can copy a descriptor. A given descriptor is passed to the function, and it returns a new descriptor,
This new descriptor is a copy of the descriptor passed to it. This means that the two descriptors share the same data structure. Such as,
If we perform lseek operations on a file descriptor, the location of the first file we get is the same as the second.
Here is a snippet of code to illustrate how the DUP function uses methods:
Int, FD1, fd2;
...
Fd2 = dup (FD1);
It is important to note that we can create a descriptor before calling fork, which is the same as calling the DUP to build the descriptor,
The child process also receives a copy of the 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 target descriptor. When the dup2 function returns successfully,
The target descriptor (the second parameter of the dup2 function) will become a copy of the source descriptor (the first parameter of the dup2 function), in other words,
The two file descriptors now point to the same file, and are the files pointed to by the first argument of the function. Lets illustrate with a piece of code:
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 called fd1. We call the dup2 function,
The arguments are oldfd and 1, which results in the replacement of the file descriptor represented by 1 (that is, stdout, because the standard output file has a ID of 1) instead of our newly opened file descriptor.
Anything written to std
您可能关注的文档
- (转)什么是西南大旱的最坏准备_月是故乡明_新浪博客((trans) what's the worst preparation for a drought in Southwest China _ is home next month _ Sina blog).doc
- (人教版)八年级下册语文课后生字词拼音及解释(完整版)((PEP) grade eight Chinese Pinyin words epigenetic and interpretation (full version)).doc
- 0-90度正弦值(0-90 degree sine).doc
- - 上海lng加气站 上海cng加气站博客上海lng加气站 上海cng加气站博客(- Shanghai LNG filling station, Shanghai CNG filling station blog, Shanghai LNG filling station, Shanghai CNG filling station blog).doc
- 010年顺义区中考二模数学试题(010 years of Shunyi District senior high school entrance examination mathematics examination the second mock exam).doc
- (设计)工作细则 - 上海工程技术大学 继续教育学院((Design) working rules - Continuing Education College of Shanghai University of Engineering Science).doc
- 02-03第一学期初三语文试卷52898(02-03 Chinese exam paper for the first semester 52898).doc
- 020我国基层群众自治制度地位的重大提升(020, the important promotion of our country's grass-roots mass autonomy system).doc
- 05-2-1.汉王降价,能降出个怎样的未来(05-2-1. Hanvon prices can drop out of what the future).doc
- 05.无形资产(5 intangible assets).doc
最近下载
- 2025年河北省中考数学真题.docx VIP
- 6.3.2 世界上最大的黄土堆积区—黄土高原(第2课时2023-2024学年八年级下册地理精品教学课件(人教版)+.pptx VIP
- 安源光伏特玻璃厂带式输送机投标书.doc VIP
- 压缩热再生吸附式干燥机.ppt VIP
- 中医执业医师中医基础试题(一)(中医执业医师中医基础考试卷).doc VIP
- 中医执业医师考试中医基础模拟试题(2)(中医执业医师中医基础考试卷).doc VIP
- 吊耳计算对照表.pdf VIP
- 香港中医执业医师考试试题题库.pdf
- 高中语文选择性必修上课本原文、翻译.pdf VIP
- SY_T6592-2016固井质量评价方法.docx VIP
原创力文档

文档评论(0)