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 & ...
随机推荐
- go 数组 切片 字典 结构体
数组 ##数组的定义与赋值: 1. var num [3]int num = [3]int{1,2,3} 2. var num [3]int = [3]int {1,2,3} 3. num := [3 ...
- kubernetes云平台管理实战:如何创建deployment更好(九)
一.文件创建带--record 1.文件 [root@k8s-master ~]# cat nginx_deploy.yml apiVersion: extensions/v1beta1 kind: ...
- Docker:私有仓库registry [十一]
一.运行docker私有仓库 安装registry docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregi ...
- Linux记录-open-falcon开源监控系统部署
参考https://book.open-falcon.org/zh_0_2/quick_install/prepare.html一.安装后端1.环境准备yum -y install redisyum ...
- Centos7 安装 scrapy
Centos7 安装 scrapy ( *:此python版本为 2.7 ) 1.先安装 python (2.7) 在安装 scrapy 要先安装 python 和 pip, 链接:https:// ...
- Choreographer解析
Choreographer_舞蹈编导 为什么叫舞蹈编导,因为舞蹈是由节奏的,节奏是每个点位动作的快慢控制,跳舞时节奏很重要,编舞者控制节奏.视图刷新也是如此,不是说你想刷就能刷,一切要按照底层信号要求 ...
- Lambda表达式-聚合操作
文章参考自博客:https://www.cnblogs.com/franson-2016/p/5593080.html 以及学习网站:how2java.cn 1.传统方式和聚合操作遍历数据的不同 传统 ...
- 一个基于Net 的CMS -- Umbraco CMS
一个基于Net 的CMS -- Umbraco CMS: https://umbraco.com/ Sitecore 和 EPiServer http://www.bubuko.com/i ...
- ssh-copy-id 拷贝用户秘钥
生成秘钥 ssh-keygen -t [rsa|dsa] 将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub 将 .pub 文件复制到B机器的 .ssh ...
- SQLAlchemy 使用(一)创建单一model
前言 最近项目等待前端接接口,比较空闲.就想学习一些新东西.学啥呢?考虑到ORM的易用性,还是学习一下ORM.那么与Flask搭配的ORM有 flask-sqlalchemy 但是该组件专为Flask ...