[转]文件IO详解(二)---文件描述符(fd)和inode号的关系


13.13 Duplicating Descriptors
You can duplicate a file descriptor, or allocate another file descriptor that refers to the same open file as the original. Duplicate descriptors share one file position and one set of file status flags (see File Status Flags), but each has its own set of file descriptor flags (see Descriptor Flags).
The major use of duplicating a file descriptor is to implement redirection of input or output: that is, to change the file or pipe that a particular file descriptor corresponds to.
You can perform this operation using the fcntl function with the F_DUPFD command, but there are also convenient functions dup and dup2 for duplicating descriptors.
The fcntl function and flags are declared in fcntl.h, while prototypes for dup and dup2 are in the header file unistd.h.
- Function: int dup (int old)
-
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function copies descriptor old to the first available descriptor number (the first number not currently open). It is equivalent to
fcntl (old, F_DUPFD, 0).
- Function: int dup2 (int old, int new)
-
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function copies the descriptor old to descriptor number new.
If old is an invalid descriptor, then
dup2does nothing; it does not close new. Otherwise, the new duplicate of old replaces any previous meaning of descriptor new, as if new were closed first.If old and new are different numbers, and old is a valid descriptor number, then
dup2is equivalent to:close (new);
fcntl (old, F_DUPFD, new)However,
dup2does this atomically; there is no instant in the middle of callingdup2at which new is closed and not yet a duplicate of old.
- Macro: int F_DUPFD
-
This macro is used as the command argument to
fcntl, to copy the file descriptor given as the first argument.The form of the call in this case is:
fcntl (old, F_DUPFD, next-filedes)
The next-filedes argument is of type
intand specifies that the file descriptor returned should be the next available one greater than or equal to this value.The return value from
fcntlwith this command is normally the value of the new file descriptor. A return value of -1 indicates an error. The followingerrnoerror conditions are defined for this command:EBADF-
The old argument is invalid.
EINVAL-
The next-filedes argument is invalid.
EMFILE-
There are no more file descriptors available—your program is already using the maximum. In BSD and GNU, the maximum is controlled by a resource limit that can be changed; see Limits on Resources, for more information about the
RLIMIT_NOFILElimit.
ENFILEis not a possible error code fordup2becausedup2does not create a new opening of a file; duplicate descriptors do not count toward the limit whichENFILEindicates.EMFILEis possible because it refers to the limit on distinct descriptor numbers in use in one process.
Here is an example showing how to use dup2 to do redirection. Typically, redirection of the standard streams (like stdin) is done by a shell or shell-like program before calling one of the exec functions (see Executing a File) to execute a new program in a child process. When the new program is executed, it creates and initializes the standard streams to point to the corresponding file descriptors, before its main function is invoked.
So, to redirect standard input to a file, the shell could do something like:
pid = fork ();
if (pid == 0)
{
char *filename;
char *program;
int file;
…
file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));
dup2 (file, STDIN_FILENO);
TEMP_FAILURE_RETRY (close (file));
execv (program, NULL);
}
There is also a more detailed example showing how to implement redirection in the context of a pipeline of processes in Launching Jobs.
[转]文件IO详解(二)---文件描述符(fd)和inode号的关系的更多相关文章
- 17、文件IO详解及实例
上篇文章已经讲过了文件系统的一些基本的概念,这里首先对文件IO进行详细的学习,文件IO也称为系统调用IO,是操作系统为"用户态"运行的进程和硬件交互提供的一组接口,即操作系统内核留 ...
- 文件IO详解(四)---标准输入、标准输出和标准错误
每个进程都会默认打开3个文件描述符,即0.1.2.其中0代表标准输入流.1代表标准输出流.2代表标准错误流.通常标准输入流对应着键盘的设备文件.标准输出流和错误流对应着显示器的设备文件.在编程中通常使 ...
- 网卡驱动-BD详解(缓存描述符 Buffer Description)
DMA介绍(BD的引入) 网络设备的核心处理模块是一个被称作 DMA(Direct Memory Access)的控制器,DMA 模块能够协助处理器处理数据收发.对于数据发送来说,它能够将组织好的数据 ...
- linux系统初始化——sysinit文件写法详解
sysinit文件写法详解 sysinit文件是linux初始化文件系统时执行的第一个脚本文件.它主要做在各个运行级别中进行初始化工作,包括: 启动交换分区;检查磁盘;设置主机名;检查并挂载文件系统; ...
- 【详解】Linux的文件描述符fd与文件指针FILE*互相转换
使用系统调用的时候用文件描述符(file descriptor,简称fd)的时候比较多,但是操作比较原始.C库函数在I/O上提供了一些方便的包装(比如格式化I/O.重定向),但是对细节的控制不够. 如 ...
- Uploadify 上传文件插件详解
Uploadify 上传文件插件详解 Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例时php版本的,本文将详细介绍Uploadify在Aspnet中 ...
- Scrapy框架——介绍、安装、命令行创建,启动、项目目录结构介绍、Spiders文件夹详解(包括去重规则)、Selectors解析页面、Items、pipelines(自定义pipeline)、下载中间件(Downloader Middleware)、爬虫中间件、信号
一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...
- 史上最全的maven pom.xml文件教程详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- C++文件读写详解(ofstream,ifstream,fstream)
C++文件读写详解(ofstream,ifstream,fstream) 这里主要是讨论fstream的内容: #include <fstream> ofstream //文件写操作 内存 ...
随机推荐
- (16)zabbix history trends历史与趋势数据详解
1. 保留历史数据 我们可以通过如下方式来设置保留数据的时长:监控项(item)配置里匹配更新监控项(item)设置Housekeeper tasksHousekeeper会定期删除过期的数据.如果数 ...
- MariaDB数据库(三)
1. 基本查询 查询基本使用包括:条件.排序.聚合函数.分组和分页. 实例详解查询 1> 创建students表用作实验 MariaDB [testdb]> drop table stud ...
- JMeter安装JSON Path Extractor插件
下载地址:https://jmeter-plugins.org/wiki/PluginsManager/ 先下载jmeter-plugins-manager-1.3.jar,点击下图中的JAR fil ...
- Python第三方库之openpyxl(12)
Python第三方库之openpyxl(12) 地面天气图 在工作表上的列或行中安排的数据可以在一个表中绘制.当您想要在两组数据之间找到最佳组合时,一个表面图表是有用的.正如在地形图中一样,颜色和图案 ...
- 【LeetCode】Game of Life(生命游戏)
这道题是LeetCode里的第289道题. 题目描述: 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格 ...
- Flutter 发布APK时,release版本和debug版本的默认权限不同
Flutter 发布APK时,release版本和debug版本的默认权限不同 @author ixenos 在调试模式下,默认情况下启用服务扩展和多个权限(在flutter中) 当您处于发布模式时, ...
- oracle客户端安装与配置
在进行开发时经常需要连接Oracle数据库,一般的场景是Oracle数据库在远程服务器上,本地计算机通过plsql developer来访问. 这就要求在本地安装好plsql developer,但是 ...
- C/C++ 位操作
C/C++对位操作有如下方法: <1>位操作运算符(注意:下面几个运算符不改变原来的变量的值,只是获得运算的结果即一个新值) 按位取反:~ 位与:& 位或:| 位异或:^ 左移位运 ...
- 【Luogu】P2953牛的数字游戏(博弈论)
题目链接 自己乱搞……然后一遍AC啦! 思路从基本的必胜态和必败态开始分析.我们把减去最大数得到的数叫作Max,减去最小数得到的数叫作Min. 那么开始分析. 一.0是必败态. 这个没法解释.题目就这 ...
- 【Luogu】P1850换教室(期望DP)
题目链接 又一道面向题解编程的恶心神题.真是叫人质壁分离…… 设f[i][j][k]表示考虑了前i节课,尝试了j次,当前申请结果为k时消耗的体力值. 对于f[i][j][0]有两种情况:一是我们的主角 ...