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

最低0.47元/天 解锁文章
1669

被折叠的 条评论
为什么被折叠?



