目录
linux 中man 2和man 3的区别
文件内容介绍
C语言文件接口
示例:
输出信息到显示器,你有哪些方法
总结:
系统文件I/O
文件类的系统调用接口介绍
示例
open 函数具体使用哪个,和具体应用场景相关,
write read close lseek ,类比C文件相关接口
open函数返回值
在认识返回值之前,先来认识一下两个概念: 系统调用 和 库函数
文件描述符fd
为什么打开文件myfile.txt的描述符是3不是0?
linux 中man 2和man 3的区别
文件内容介绍
文件 = 内容+属性.
文件系统
- 被打开的文件 -------内存中
- 没有被打开的文件-------磁盘中
没打开文件之前,文件存储在磁盘, 进程想要访问文件之前 ,要打开文件(也就是将文件加载到内存中)
文件加载到内存中要被OS管理(先描述,再组织)
也就产生了文件的内核数据结构+文件的内容.(类似进程)
C语言文件接口
示例:
1 #include<stdio.h>2 #include<string.h>3 #include<errno.h>4 5 int main()6 {7 FILE* fp =fopen("myfile.txt","w+");8 if(!fp) perror("fopen");9 10 int count =6;11 const char* ch ="hello world\n";12 while(count--)13 {14 fwrite(ch ,strlen(ch),1,fp);15 }16 fclose(fp);17 return 0;18 }
进程创建好了,默认先打开了三个输入输出流
- stdin 标准输入流 键盘
- stdout 标准输出流 显示器
- stderr 标准错误流 显示器
仔细观察发现,这三个流的类型都是FILE*, fopen返回值类型,文件指针
输出信息到显示器,你有哪些方法
printf("hello linux\n");const char* ch ="hello linux\n";
fwrite(ch ,strlen(ch), 1,stdout);
fpritf(stdout, "hello linux\n");
fputs("aaaa\n" ,stdout);
总结:
打开文件的方式
r Open text file for reading. The stream is positioned at the beginning of the file.r+ Open for reading and writing.The stream is positioned at the beginning of the file.w Truncate(缩短) file to zero length or create text file for writing.The stream is positioned at the beginning of the file.w+ Open for reading and writing.The file is created if it does not exist, otherwise it is truncated.The stream is positioned at the beginning of the file.a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.a+ Open for reading and appending (writing at end of file).The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
系统文件I/O
前面在讲计算机冯诺依曼体系结构时讲过
- OS要确保底层的安全稳定,对于上层开发提供了一些系统调用(system call)接口,用于对硬件的访问(I/O)
- 而上面C语言的库函数fopen可以打开文件, fwrite可以向文件中写入, 文件在磁盘上,可以推断出,这些C语言中这些文件I/O一定封装了系统调用接口.
文件类的系统调用接口介绍
open接口可以在linux中输入查询
man 2 open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);pathname: 要打开或创建的目标文件
flags: 打开文件时,可以传入多个参数选项,用下面的一个或者多个常量进行“或”运算,构成flags。flags的参数:O_RDONLY: 只读打开O_WRONLY: 只写打开O_RDWR : 读,写打开这三个常量,必须指定一个且只能指定一个O_CREAT : 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限O_APPEND: 追加写返回值:成功:新打开的文件描述符失败:-1
flags是位图, 参数: 比如O_RDONLY是32位中只有一个为1的int.
因此,参数之间要用 | 或运算.
mode_t理解:第三个参数 mode用于指定创建文件时的权限(文件模式)。它仅在创建新文件时生效.
示例
创建一个新文件,权限为
0644
(用户可读写,组和其他用户只读):int fd = open("example.txt", O_CREAT | O_WRONLY, 0644);
open 函数具体使用哪个,和具体应用场景相关,
- 如目标文件不存在,需要三参数的open创建,则第三个参数表示创建文件 的默认权限.
- 目标文件存在,使用两个参数的open。
write read close lseek ,类比C文件相关接口
open函数返回值
在认识返回值之前,先来认识一下两个概念: 系统调用 和 库函数
- 上面的 fopen fclose fread fwrite 都是C标准库当中的函数,我们称之为库函数(libc)
- 而, open close read write lseek 都属于系统提供的接口,称之为系统调用接口 回忆一下讲操作系统概念时,画的一张图
- 也就是C语言的文件接口(fopen fclose fread fwrite ),封装了文件类的系统调用接口(open read write close),方便了二次开发.
文件描述符fd
通过对open函数的学习,我们知道了文件描述符就是一个小整数
在上面open示例代码中发现打出来的myfile.txt文件描述符fd=3
为什么打开文件myfile.txt的描述符是3不是0?
进程启动 ,默认打开三个标准的输入输出流
因为在打开文件myfile.txt前,就打开了三个文件(stdin stdout stderr),他们的文件描述符分别是0 ,1 ,2
验证:
(先描述,再组织) 当我们打开文件时,操作系统在内存中要创建相应的数据结构来 描述目标文件.
struct file就是文件的属性 ,类似进程的task_struct
OS想要管理进程创建了进程管理列表,文件同理,创建了文件管理列表(也是双链表)
正如图所示:左面是进程管理,右侧是文件管理
一个进程通常打开多个文件,如何让进程与文件关联?
- 每个进程都有一个指针*files, 指向一张表files_struct,该表最重要的部分就是包涵一个指针数组(文件描述符表),每个元素都是一个指向打开文件的指针!
所以,本质上,文件描述符就是该数组的下标。所以,只要拿着文件描述符,就可以找到对应的文件