Linux dup、dup2函数

1、dup函数

作用:复制一个新的文件描述符

man 2 dup

在这里插入图片描述
DESCRIPTION
The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused file descriptor for the new descriptor.
就是会用空闲的最小的文件描述符去创建oldfd的拷贝。
这跟 “每次新打开文件都会占用一个文件描述符,而且是最小的” 是一致的。

RETURN VALUE
On success, these system calls return the new file descriptor. On error, -1 is returned, and errno is set appropriately.

#include <unistd.h>
int dup(int oldfd);
    作用:复制一个新的文件描述符
    fd=3, int fd1 = dup(fd),
    fd指向的是a.txt, fd1也是指向a.txt
    从空闲的文件描述符表中找一个最小的,作为新的拷贝的文件描述符

测试:

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int main() {
   
   

    int fd = open
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值