Linux下的文件描述符
在 Linux 的世界里,一切设备皆文件。对文件的操作都是通过文件描述符(fd)来进行的。Linux 中有7中文件类型:我们可以通过 Il 命令来查看文件类型:**文件描述符相当于非负的索引。**我们可以通过文件描述符操作相应的文件。Linux进程会默认打开三个文件描述符:文件描述符的分配原则:分配出未被占用的最小文件描述符。一般来说,我们打开文件后占用一个文件描述符3(0、1、2已被标准文件描述
引文
在 Linux 的世界里,一切设备皆文件。对文件的操作都是通过文件描述符(fd)来进行的。
Linux 中有7种文件类型:
文件类型 | 文件类型描述 | 符号 |
---|---|---|
普通文件 | 最常使用的一类文件,其特点是不包含有文件系统信息的结构信息。这种类型的文件是按照其内部结构又可分为纯文本文件(ASCII)、二进制文件(binary)、数据格式的文件(data)、各种压缩文件。 | REG (-) |
目录文件 | 用于存放文件名以及其相关信息的文件,是内核组织文件系统的基本节点。 | DIR (d) |
块设备 | 块设备文件 : 就是存储数据以供系统存取的接口设备,简单而言就是硬盘。 | BLK (b) |
字符设备 | 字符设备文件:即串行端口的接口设备,例如键盘、鼠标等等。 | CHR (c) |
套接字 | 这类文件通常用在网络数据连接。可以启动一个程序来监听客户端的要求,客户端就可以通过套接字来进行数据通信。 | SOCK(s) |
管道 | 一种很特殊的文件,主要用于不同进程的信息传递。 | FIFO (p) |
链接 | 一种特殊文件,指向一个真实存在的文件链接,类似于Windows下的快捷方式,链接文件的不同,又可分为硬链接和软链接。 | LNK (l) |
未知 | … | … |
我们可以通过 Il 命令来查看文件类型:
1. 文件描述符
**文件描述符相当于非负的索引。**我们可以通过文件描述符操作相应的文件。
Linux进程会默认打开三个文件描述符:
- 0 :标准输入 stdin
- 1 :标准输出 stdout
- 2 :标准错误 stderror
文件描述符的分配原则:分配出未被占用的最小文件描述符。
一般来说,我们打开文件后占用一个文件描述符3(0、1、2已被标准文件描述符占用),下一个则是4(如果它没被占用的话)…
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main() {
// int open(const char *pathname, int flags);
// 参数:
// - pathname:要打开的文件路径
// - flags:对文件的操作权限设置还有其他的设置
// O_RDONLY, O_WRONLY, O_RDWR 这三个设置是互斥的
// 返回值:返回一个新的文件描述符,如果调用失败,返回-1
// 打开或创建一个文件
int fd1 = open("temp.txt", O_RDONLY | O_CREAT);
if(fd1 == -1) {
perror("open");
return -1;
}
int fd2 = open("temp.txt", O_RDONLY );
if(fd2 == -1) {
perror("open");
return -1;
}
printf("fd1: %d\n", fd1);
printf("fd2: %d\n", fd2);
// 关闭文件描述符
close(fd1);
close(fd2);
return 0;
}
运行结果:
我们观察上述代码可以发现:同一个进程的不同文件描述符可以指向同一个文件。
同时,当我们复制该会话(相当于打开了一个新进程),再运行该程序:
发现fd1 = 3 和 fd2 = 4,这表明:不同进程可以拥有相同的文件描述符。
进而,不同进程的不同文件描述符也可以指向同一个文件。
2. dup 和 dup2
2.1 dup函数
Linux下可通过 man 命令查看它们的具体使用。
ydh@ubuntu:~/linux/test_dup$ man 2 dup
ydh@ubuntu:~/linux/test_dup$ man 2 dup
头文件:#include <unistd.h>
函数原型:int dup(int oldfd);
作用:复制一个新的文件描述符,从空闲的文件描述符表中找一个最小的,作为新的拷贝的文件描述符。
#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("a.txt", O_RDWR | O_CREAT, 0664);
int fd1 = dup(fd);
if(fd1 == -1) {
perror("dup");
return -1;
}
printf("fd : %d , fd1 : %d\n", fd, fd1);
close(fd);
char * str = "hello,world\n";
int ret = write(fd1, str, strlen(str));
if(ret == -1) {
perror("write");
return -1;
}
close(fd1);
return 0;
}
运行结果:
观察运行结果可以发现,fd1被分配到了一个最小的空闲文件描述符;fd1 和 fd 指向同一个文件,当我们通过操作fd1进行写数据时,fd指向的文件发生了修改。
2.2 dup2函数
#include <unistd.h>
int dup2(int oldfd, int newfd);
作用:重定向文件描述符
假如 oldfd 指向 a.txt, 而 newfd 指向 b.txt,调用函数成功后:newfd 和 b.txt 做close, newfd 指向了 a.txt
注:oldfd 必须是一个有效的文件描述符,oldfd和newfd值相同,相当于什么都没有做
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main() {
int fd = open("1.txt", O_RDWR | O_CREAT, 0664);
if(fd == -1) {
perror("open");
return -1;
}
int fd1 = open("2.txt", O_RDWR | O_CREAT, 0664);
if(fd1 == -1) {
perror("open");
return -1;
}
printf("fd : %d, fd1 : %d\n", fd, fd1);
int fd2 = dup2(fd, fd1);
if(fd2 == -1) {
perror("dup2");
return -1;
}
// 通过fd1去写数据,实际操作的是1.txt,而不是2.txt
char * str = "hello, dup2";
int len = write(fd1, str, strlen(str));
if(len == -1) {
perror("write");
return -1;
}
printf("fd : %d, fd1 : %d, fd2 : %d\n", fd, fd1, fd2);
close(fd);
close(fd1);
return 0;
}
运行结果:
上述代码运行成功后,fd2从原来的2.txt,指向了1.txt。
3.fcntl函数
fcntl 函数所需头文件:
#include <unistd.h>
#include <fcntl.h>
函数原型:
int fcntl(int fd, int cmd, ... /* arg */ );
参数含义:
fd : 表示需要操作的文件描述符
cmd: 表示对文件描述符进行如何操作
… /* arg */:为可选参数
fcntl的两个常用功能:
- 复制文件描述符
int fd = open("1.txt", O_RDONLY);
// F_DUPFD : 复制文件描述符,复制的是第一个参数fd,得到一个新的文件描述符(返回值)
int ret = fcntl(fd, F_DUPFD);
- 修改或者获取文件描述符状态flag
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main() {
// 2.修改或者获取文件描述符状态flag
int fd = open("1.txt", O_RDWR);
if(fd == -1) {
perror("open");
return -1;
}
// 获取文件描述符状态flag
int flag = fcntl(fd, F_GETFL);
if(flag == -1) {
perror("fcntl");
return -1;
}
flag |= O_APPEND; // flag = flag | O_APPEND
// 修改文件描述符状态的flag,给flag加入O_APPEND这个标记
int ret = fcntl(fd, F_SETFL, flag);
if(ret == -1) {
perror("fcntl");
return -1;
}
char * str = "world";
write(fd, str, strlen(str));
close(fd);
return 0;
}
上述代码为文件描述符增加了O_APPEND(追加)选项,向1.txt中追加了"world"。
其他更多选项可通过man指向进行查询。
ydh@ubuntu:~/linux/test_fcntl$ man 2 fcntl
更多推荐
所有评论(0)