dup(dup2/dup3)
readme man~
NAME
dup, dup2, dup3 - duplicate a file descriptor SYNOPSIS
#include <unistd.h> int dup(int oldfd);
int dup2(int oldfd, int newfd); #define _GNU_SOURCE
#include <unistd.h> int dup3(int oldfd, int newfd, int flags); DESCRIPTION
These system calls create a copy of the file descriptor oldfd. dup() uses the lowest-numbered unused descriptor for the new descriptor. dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following: * If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed. * If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd. After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably. They refer to the same
open file description (see open()) and thus share file offset and file status flags; for example, if the file offset is modified by using
lseek() on one of the descriptors, the offset is also changed for the other. The two descriptors do not share file descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl()) for the
duplicate descriptor is off. dup3() is the same as dup2(), except that: * The caller can force the close-on-exec flag to be set for the new file descriptor by specifying O_CLOEXEC in flags. See the description of
the same flag in open() for reasons why this may be useful. * If oldfd equals newfd, then dup3() fails with the error EINVAL. RETURN VALUE
On success, these system calls return the new descriptor. On error, - is returned, and errno is set appropriately. ERRORS
EBADF oldfd isnan open file descriptor, or newfd is out of the allowed range for file descriptors. EBUSY (Linux only) This may be returned by dup2() or dup3() during a race condition with open() and dup(). EINTR The dup2() or dup3() call was interrupted by a signal; see signal(). EINVAL (dup3()) flags contain an invalid value. Or, oldfd was equal to newfd. EMFILE The process already has the maximum number of file descriptors open and tried to open a new one.
1、dup用于复制文件描述符,返回新的描述符;dup2区别是可指定目标描述符,通常用于重定向或新打开文件;dup3能够设置close-on-exec 标志位的开启和关闭,它通过在flag参数位设置 O_CLOEXEC 来强行设置 close-on-exec 标志位的开启,使进程就无法够使用该文件描述符。
2、dup的返回值newfd和原oldfd指向的是同一文件,共享锁、读写指针(偏移量)、权限,不共享close-on-exec标志。
dup(dup2/dup3)的更多相关文章
- Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...
- 文件I/O(不带缓冲)之dup和dup2函数
下面两个函数都可用来复制一个现有的文件描述符: #include <unistd.h> int dup( int filedes ); int dup2( int filedes, int ...
- dup和dup2应用实例(dup跟APUE有出入,close+dup=dup2?)
dup/dup2函数 有时我们希望把标准输入重定向到一个文件,或者把标准输出重定向到一个网络连接. dup()与dup2()能对输入文件描述符进行重定向. 函数原型如下: dup函数创建一个新的文件描 ...
- Linux内核分析:dup、dup2的实现
一.首先需要看一下这两个函数的作用: #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); 根据m ...
- dup和dup2函数
下面两个函数都可用来复制一个现存的文件描述符: #include<unistd.h> int dup(int filedes); int dup2(int filedes,int file ...
- dup和dup2函数以及管道的实现
疑问:管道应该不是这样实现的,因为这要求修改程序的代码 dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符.它们经常用来重定向进程的stdin.stdout和stderr. ...
- (转载)可重入函数(reentrant function)
(转载)http://blog.163.com/xu_jin_rong/blog/static/1491966220086775017178 由于cublog系统的缘故,将前段时间写的一篇blog文章 ...
- Linux系统编程--文件描述符的复制dup()和dup2()【转】
本文转载自:http://blog.csdn.net/tennysonsky/article/details/45870459 dup() 和 dup2() 是两个非常有用的系统调用,都是用来复制一个 ...
- Unix 网络编程 dup和dup2函数
dup和dup2也是两个很实用的调用,它们的作用都是用来复制一个文件的描写叙述符. 它们经经常使用来重定向进程的stdin.stdout和stderr.这两个函数的原形例如以下: #include & ...
随机推荐
- 2018-2019-2 《Java程序设计》第1周学习总结
# 20175319 2018-2019-2 <Java程序设计>第1周学习总结 ## 教材学习内容总结 第一周我根据老师提供的博客,下载和设置了各种需要的软件,并对这些软件进行初步的了解 ...
- 第一节: Timer的定时任务的复习、Quartz.Net的入门使用、Aop思想的体现
一. 前奏-Timer类实现定时任务 在没有引入第三方开源的定时调度框架之前,我们处理一些简单的定时任务同时都是使用Timer类, DotNet中的Timer类有三个,分别位于不同的命名空间下,分别是 ...
- SQL修改日期类型字段为字符串类型
select * from test1 --添加行 ) --将转换格式后的数据放到列中 ) --删除老的字段 alter table test1 drop column startdate --修改字 ...
- 使用OpenPop.dll开发读取POP3邮件程序
1.设置邮件的pop3 以163网易邮箱为例: https://jingyan.baidu.com/article/adc81513804be9f722bf7350.html 2.下载OpenPop. ...
- [转]C语言的int最值问题,以及原码反码及补码
以2字节为例来说: 对于无符号的数值(原码反码及补码都一样),最大值为1111 1111 1111 1111=65535 最小值为0000 0000 0000 0000=0 对于有符号的来 ...
- Ubuntu 终端关机和重启命令
原文地址:https://blog.csdn.net/zzc15806/article/details/80907779 (diss一下原文地址的作者,你也是转载的为何不添加原文链接?) 重启命令:1 ...
- jarvisoj level5爬坑
本着纸上得来终觉浅,绝知此事要躬行的原则,把一个简单的ROP做了一下.漏洞很明显,libc有给出:唯一的限制就是不允许调用system或execve,而是用mprotect或者mmap 脚本调了半天, ...
- 2017-2018-2 165X 『Java程序设计』课程 结对编程练习_四则运算
2017-2018-2 165X 『Java程序设计』课程 结对编程练习_四则运算 经过第一阶段的学习,同学们已经熟悉了这门语言基本的用法.在一次又一次对着电脑编写并提交代码,进行练习的时候,有没有觉 ...
- 资本寒冬下的android面经
在2018年10月初,公司倒闭,无奈走上找工作的道路,不想自己平时图安逸,不思进取,再次找工作才发现,android行业也不是站在风口上,猪也能吹上天的世道了.作为技术小菜的我,再找工作那几个月真是战 ...
- Java_变量类型
目录 主要是为了复习java相关知识,本文主要内容来自于 http://www.runoob.com/java 一.局部变量 局部变量声明在方法.构造方法或语句块中 在方法.构造方法.语句块被执行的时 ...