CSAPP:第十章 系统级I/O
10.1 unix I/O10.2 文件10.3 读取文件元数据10.4 读取目录内容10.5 共享文件10.6 我们该使用哪些I/O函数?
10.1 unix I/O
所有的I/O设备(例如网络、磁盘和终端)都被模块化为文件,而所有的输入和输出都被当作对相应文件的读和写来执行。这种允许Linux内核引出一个简单、低级的应用接口,称为Unix I/O,这使得所有的输入和输出都能以一种统一且一致的方式来执行:
- 打开文件。
- 改变当前的文件位置,
- 读写文件。
- 关闭文件。
10.2 文件
每个Linux文件都有一个类型来表明它在系统中的角色:
- 普通文件。包含任意数据。
- 目录文件。包含一组链接的文件。
- 套接字。用来与另一个进程进行跨网络通信的文件。
- 命名管道。
- 字符文件。
- 块设备。
Linux内核将所有文件组织成一个目录层次结构。
打开和关闭文件
进程通过调用open函数来打开一个已经存在的文件或者创建一个新的文件:
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <fcntl.h>
4/*
5 * filename : 文件名
6 * flags :
7 * O_RDONLY:只读
8 * O_WRONLY:只写
9 * O_RDWR:可读可写
10 * O_CREAT:如果文件不存在,创建新文件
11 * O_TRUNC:如果文件存在,截断它
12 * O_APPEND:在每次写操作前,设置文件位置到文件结尾处
13 * mode :指定了新文件权限
14 * 返回值:返回文件描述符
15 */
16int open(char *filename,int flags,mode_t mode);
进程通过调用close来关闭一个已经打开的文件:
1#include <unistd.h>
2int close(int fd);
读写文件
应用程序分别调用read和write函数来执行输入和输出的。
1#include <unistd.h>
2/* 返回:若成功则为读的字节数,若为EOF则为0,若出错为-1*/
3ssize_t read(int fd,void *buf,size_t n);
4/* 返回:若成功则为写的字节数,若出错为-1*/
5ssize_t write(int fd,const void *buf,size_t n);
10.3 读取文件元数据
应用程序能够通过stat和fstat函数,检索到关于文件的信息(有时也称为文件的元数据)
1#include <unistd.h>
2#include <sys/stat.h>
3
4int stat(const char *filename,struct stat *buf);//以文件名作为输入
5int fstat(int fd,struct stat *buf); //以文件描述符作为输入
6
7struct stat {
8 dev_t st_dev; /* ID of device containing file */
9 ino_t st_ino; /* inode number */
10 mode_t st_mode; /* protection */
11 nlink_t st_nlink; /* number of hard links */
12 uid_t st_uid; /* user ID of owner */
13 gid_t st_gid; /* group ID of owner */
14 dev_t st_rdev; /* device ID (if special file) */
15 off_t st_size; /* total size, in bytes */
16 blksize_t st_blksize; /* blocksize for filesystem I/O */
17 blkcnt_t st_blocks; /* number of 512B blocks allocated */
18
19 /* Since Linux 2.6, the kernel supports nanosecond
20 precision for the following timestamp fields.
21 For the details before Linux 2.6, see NOTES. */
22
23 struct timespec st_atim; /* time of last access */
24 struct timespec st_mtim; /* time of last modification */
25 struct timespec st_ctim; /* time of last status change */
26
27#define st_atime st_atim.tv_sec /* Backward compatibility */
28#define st_mtime st_mtim.tv_sec
29#define st_ctime st_ctim.tv_sec
30};
10.4 读取目录内容
应用可以用readdir系列函数来读取目录的内容。
1#include <sys/types.h>
2#include <dirent.h>
3
4DIR *opendir(const char *name);
5
6struct dirent *readdir(DIR *dirp); //若成功,返回指向下一个目录的指针
7
8struct dirent{
9 ino_t ; /*inode number*/
10 char d_name[256]; /*Filename*/
11};
12
13int closedir(DIR *dirp);//关闭目录
10.5 共享文件
- 描述符表。每个进程都有它独立的描述符表,它的表项是由进程打开的文件描述符来索引的。每个打开的描述符表项,指向文件表中的一个表项。
- 文件表。打开文件的集合是由一张文件表来表示的,所有进程共享这张表。
- v-node表。同文件表一样,所有进程共享这张v-node表。每个表项包含stat结构中的大多数信息,包括st_mode和st_size成员。