1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024

文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file)
默认情况下,子进程会获得其父进程文件表的完整拷贝
 

2.打开文件

open系统调用必须包含 O_RDONLY,O_WRONLY,O_RDWR 三种存取模式之一
注意 O_NONBLOCK模式
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, )
int fd = creat(filename, )

3.读文件

read系统调用会有以下结果:
(1)返回值与请求数len相同,所有len字节都存储在buf内
 
(2)返回值小于请求数len,但是大于0。发生此种情况有很多原因:
a.read系统调用中途被信号中断
b.read系统调用中途发生错误
c.可读字节数大于0,但是小于len
d.在读完len字节之前遇到EOF
 
(3)返回0,表示EOF
(4)调用被阻塞,因为当前没有可读,这种情况不会发生在非阻塞模式
(5)返回-1,errno设置为EINTR,表示在读任一字节之前就接收到信号
(6)返回-1,errno设置为EAGAIN,表示读操作被阻塞,因为当前并没有可读字节,这只发生在 非阻塞模式
(7)返回-1,errno设置为 EINTR,EAGAIN之外的值,表示发生其他更严重的错误
 
读完所有字节:
size_t readn(int fd, void* buf, size_t len)
{
size_t tmp = len;
ssize_t ret = ;
while (len != && (ret = read(fd, buf, len)) != ) {
if (ret == -) {
if (errno == EINTR) {
continue;
}
fprintf(stderr, "read error\n");
break;
}
len -= ret;
buf += ret;
}
return tmp - len;
}
非阻塞读:
有时我们并不希望当没有可读数据时read系统调用被阻塞,而是希望调用可以立即返回,表明没有数据可读,这就是非阻塞I/O
 

4.写文件

write系统调用没有EOF,对于普通文件,write默认操作是全部写,除非是发生错误返回-1
对于其他文件就有可能发生部分写,最典型的是网络编程中socket读写时,应该如下写:
size_t writen(int fd, void* buf, size_t len)
{
ssize_t ret = ;
size_t tmp = len;
while (len != && (ret = write(fd, buf, len)) != ) {
if (ret == -) {
if (errno == EINTR) {
continue;
}
fprintf(stderr, "write error\n");
break;
}
len -= ret;
buf += ret;
}
return tmp - len;
}
追加模式可以确保文件的当前位置总是位于文件末尾,并且可以把文件偏移更新操作看成原子操作,所以该模式对于多任务追加写非常有用
 

5.文件同步

当调用write时,内核从用户buffer拷贝数据到内核buffer,但并不一定是立即写到目的地,内核通常是执行一些检查,将数据从用户buffer拷贝到一个dirty buffer,后而内核收集所有这些dirty buffer(contain data newer than what is on disk),最后才写回磁盘。
这种延迟写并没有改变POSIX语义,反而可以提升读写性能
if a read is issued for just-written data that lives in a dirty buffer and is not yet on disk, the request will be satisfied from the buffer and not cause a read from the "stale" data on disk. so the read is satisfied from an in-memory cache without having to go to disk.
 
延迟写可以大幅提升性能,但是有时候需要控制写回磁盘的文件,这是需要确保文件同步
fsync系统调用确保fd关联的文件数据已经写回到磁盘
int ret = fsync(fd);

open调用时 O_SYNC标志表示 文件必须同步

int fd = open(file, O_WRONLY | O_SYNC);
O_SYNC导致I/O等待时间消耗巨大,一般地,需要确保文件写回到磁盘时我们使用 fsync函数

6.文件定位

显式的文件定位函数:
a. 将文件偏移定位到1825
off_t ret = lseek(fd, (off_t)1825, SEEK_SET);
b. 将文件便宜定位到文件末尾处
off_t ret = lseek(fd, 0, SEEK_END);
c. 将文件偏移定位到文件开始处
off_t ret = lseek(fd, 0, SEEK_CUR)
文件定位是可以超出文件末尾的,此时对该文件写操作会填补0,形成空洞,空洞是不占有物理磁盘空间的。
This implies that the total size of all files on a filesystem can add up to more than the physical size of the disk
 

7.截断文件

int ftruncate(int fd, off_t len);  

将给定文件截断为给定长度,这里的给定长度是可以小于文件大小,也可以大于文件大小(会造成空洞)

8.多路I/O

阻塞I/O:如果read系统调用时,文件(例如管道输入)没有可读数据,这时进程会一直阻塞等待,直到有可读数据。效率低下,不能同时进行多个文件读写操作
多路I/O可以允许程序并发地阻塞在多个文件上,并且当任一文件变为可读或可写的时候会立马接收到通知
Multiplexed I/O becomes the pivot point for the application,designed similarly to the following activity:
a. Multiplexed I/O : Tell me when any of these file descriptors becomes ready for I/O
b. Nothing ready? Sleep until one or more file descriptors are ready.
c. Woken up ! What is ready?
d. Handle all file descriptors ready for I/O, without bolocking
e. Go back to step a

9.select

int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);
FD_CLR(int fd, fd_set* set); // removes a fd from a given set
FD_ISSET(int fd, fd_set* set); // test whether a fd is part of a given set
FD_SET(int fd, fd_set* set); // adds a fd to a given set
FD_ZERO(int fd, fd_set* set); // removes all fds from specified set. shoule be called before every invocation of select()

因为fd_set是静态分配的,系统有一个文件描述符的最大打开数 FD_SETSIZE,在Linux中,该值为 1024

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h> #define TIMEOUT 5 /* select timeout in seconds */
#define BUFLEN 1024 /* read buffer in bytes */ int main(int argc, char* argv[])
{
struct timeval tv;
tv.tv_sec = TIMEOUT;
tv.tv_usec = ; /* wait on stdin for input */
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds); int ret = select(STDIN_FILENO + , &readfds, NULL, NULL, &tv);
if (ret == -) {
fprintf(stderr, "select error\n");
return ;
} else if (!ret) {
fprintf(stderr, "%d seconds elapsed.\n", TIMEOUT);
return ;
}
if (FD_ISSET(STDIN_FILENO, &readfds)) {
char buf[BUFLEN + ];
int len = read(STDIN_FILENO, buf, BUFLEN);
if (len == -) {
fprintf(stderr, "read error\n");
return ;
}
if (len != ) {
buf[BUFLEN] = '\0';
fprintf(stdout, "read:%s\n", buf);
}
return ;
} else {
fprintf(stderr, "This should not happen\n");
return ;
} }

10. poll

int poll(struct pollfd* fds, nfds_t  nfds, int timeout);

This is a program that uses poll() to check whether a read from stdin and a write to stdout will block

#include <unistd.h>
#include <poll.h> #define TIMEOUT 5 int main(int argc, char* argv[])
{
struct pollfd fds[]; /* watch stdin for input */
fds[].fd = STDIN_FILENO;
fds[].events = POLLIN; /* watch stdout for alibity to write */
fds[].fd = STDOUT_FILENO;
fds[].events = POLLOUT; int ret = poll(fds, , TIMEOUT * );
if (ret == -) {
fprintf(stderr, "poll error\n");
return ;
} if (!ret) {
fprintf(stdout, "%d seconds elapsed.\n", TIMEOUT);
return ;
} if (fds[].revents & POLLIN) {
fprintf(stdout, "stdin is readable\n");
}
if (fds[].revents & POLLOUT) {
fprintf(stdout, "stdout is writable\n");
}
return ;
}
poll vs select 
a. poll不需要用户计算并传递文件描述符参数(select中必须将该值设为最大描述符数加1)
b. select的fd_set是静态分配的,有一个最大文件数限制FD_SETSIZE,poll就没有这个限制,只需要创建一个合适大小的结构体数组
c. select移植性更好,支持select的unix更多
d. select支持更精细的timeout,poll只支持毫秒

11.内核实现

Linux内核主要由 virtual filesystem, page cache, page write-back 来支持有效且强大的I/O机制
(1) virtual filesystem
The virtual filesystem (also called a virtual file switch) is a mechanism of abstraction that allows the Linux kernel to call filesystem functions and manipulate filesystem data without knowing the specific type of filesystem being used.
So, a single system call can read any filesystem on any medium, All filesystems support the same concepts, the same interfaces, and the same calls
 
(2) page cache
The page cache is an in-memory store of recently accessed data from an on-disk filesystem.
Storing requested data in memory allows the kernel to fulfill subsequent requests for the same data  from memory, avoiding repeated disk access
The page cache exploits the concept of temporal locality, which says that a resource accessed at one point has a high probability of being accessed again in the near future
 
时间局部性:
The page cache is the first place that kernel looks for filesystem data. The first time any item of sata is read, it is transferred from the disk into the page cache, and is returned to the application from the cache. 
 
空间局部性:
The data is often referenced sequentially. The kernel implements page cache  readahead(预读). Readahead is the act of reading extra data off the disk and into the page cache following each read request. In effect, reading a little bit ahead. 
 
(3) page write-back
When a process issues a write request, the data is copied into a buffer, and the buffer is marked dirty, denoting that the in-memory copy is newer than the on-disk copy.
Eventually, the dirty buffers need to be committed to disk, sync the on-disk files with the data in memory. 

Linux System Programming 学习笔记(二) 文件I/O的更多相关文章

  1. Linux System Programming 学习笔记(八) 文件和目录管理

    1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...

  2. Linux System Programming 学习笔记(四) 高级I/O

    1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and mu ...

  3. Linux System Programming 学习笔记(一) 介绍

    1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...

  4. Linux System Programming 学习笔记(十一) 时间

    1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...

  5. Linux System Programming 学习笔记(九) 内存管理

    1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...

  6. Linux System Programming 学习笔记(七) 线程

    1. Threading is the creation and management of multiple units of execution within a single process 二 ...

  7. Linux System Programming 学习笔记(六) 进程调度

    1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...

  8. Linux System Programming 学习笔记(五) 进程管理

    1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...

  9. Linux System Programming 学习笔记(三) 标准缓冲I/O

    1. partial block operations are inefficient. The operating system has to “fix up” your I/O by ensuri ...

随机推荐

  1. 用户输入和while循环

    函数input()的工作原理 message=input('Tell me something,and I will repeat it back to you:') print(message) 编 ...

  2. python @staticmethod和@classmethod

    Python其实有3个方法,即 静态方法 (staticmethod), 类方法 (classmethod)和 实例方法. 如下: def foo(x): print "executing ...

  3. js函数式编程(二)-柯里化

    这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...

  4. nginx下配置laravel+rewrite重写

    server { listen ; server_name ha.d51v.cn; #access_log /data/wwwlogs/access_nginx.log combined; root ...

  5. Node项目实战-静态资源服务器

    打开github,在github上创建新项目: Repository name: anydoor Descripotion: Tiny NodeJS Static Web server 选择:publ ...

  6. 二叉排序树:POJ2418-Hardwood Species(外加字符串处理)

    Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Description Hardwoods are the botanical gr ...

  7. P1627 中位数

    P1627 中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式: 第一行为两个正整 ...

  8. 全网最详细python中socket套接字send与sendall的区别

    将数据发送到套接字. 套接字必须连接到远程套接字.  返回发送的字节数. 应用程序负责检查是否已发送所有数据; 如果仅传输了一些数据, 则应用程序需要尝试传递剩余数据.(需要用户自己完成) 将数据发送 ...

  9. JSP 页面 jstl 时间戳 long型转时间

    转载http://www.cnblogs.com/gmq-sh/p/5528989.html

  10. 00018_流程控制语句switch

    1.选择结构switch switch 条件语句也是一种很常用的选择语句,它和if条件语句不同,它只能针对某个表达式的值作出判断,从而决定程序执行哪一段代码. 2.switch语句的语法格式 swit ...