1. partial block operations are inefficient.

The operating system has to “fix up” your I/O by ensuring that everything occurs on block-aligned boundaries and rounding up to the next largest block

 
用户级程序可能在某一时刻仅仅读写一个字节,这是极大的浪费。Each of those one-byte writes is actually writing a whole block
user-buffered I/O:a way for applications to read and write data in whatever amounts feel natural but have the actual I/O occur in units of the filesystem
block size

2. User-buffered I/O

上如表明,只要将执行I/O操作的请求数设置为物理I/O块大小的整数倍 就可以获得很大的性能提升。Larger multiples will simply result in fewer system calls
使用stat系统调用可以获知文件I/O块大小
 
 #include <stdio.h>

 int main(int argc, char* argv[])
{
struct private {
char name[]; /* real name */
unsigned long booty; /* in pounds sterling */
unsigned int beard_len; /* in inches */
};
struct private p;
struct private blackbeard = {"Edward Teach", , }; FILE* out = fopen("data", "r");
if (out == NULL) {
fpiintf(stderr, "fopen error\n");
return ;
} if (fwrite(&blackbeard, sizeof(struct private), , out) == ) {
fprintf(stderr, "fwrite error\n");
return ;
} if (fclose(out)) {
fprintf(stderr, "fclose error\n");
return ;
} FILE* in = fopen("data", "r");
if (in == NULL) {
fprintf(stderr, "fopen error\n");
return ;
}
if (fread(&p, sizeof(struct private), , in) == ) {
fprintf(stderr, "fread error\n");
return ;
} if (fclose(in)) {
fprintf(stderr, "fclose error\n");
return ;
} fprintf(stdout, "name = \"%s\" booty = %lu beard_len = %u\n", p.name, p.booty, p.beard_len);
return ;
}
it's important to bear in mind that because of differences in variable sizes, alignment, and so on, binary data written with one application may not be readable by other applications. These things are guaranteed to remain constant only on a particular machine type with a particular ABI
 
fflush() merely writes the user-buffered data out to the kernel buffer. Calling fflush(), followed immediately by fsync(): that is, first ensure that
the  user buffer is written out to the kernel and then ensure that the kernel's buffer is written  out to disk.
int fileno (FILE *stream);   //返回文件流(C标准I/O库)对应的文件描述符(Unix系统调用) 
绝不能混用Unix系统调用I/O和C语言标准I/O
You should almost never  intermix file descriptor and stream-based I/O operations
 

3. 控制缓冲

标准I/O提供三种类型缓冲:
(1) 无缓冲:Data is submitted directly to the kernel. 无性能优势,基本不用。标准错误默认是无缓冲
(2) 行缓冲: With each newline character, the buffer is submitted to the kernel.  终端文件(标准输入输出)默认是行缓冲
(3) 块缓冲:Buffering is performed on a per-block basis. By default, all streams associated with files are block-buffered
 

4. 线程安全

标准I/O函数本身是线程安全的。标准I/O函数使用锁机制来确保进程内的多个线程可以并发执行标准I/O操作。(注意:确保线程安全的原子区域仅限于单一函数,多个I/O函数之间并不保证)
Any given thread must acquire the lock and become the owning thread before issuing any I/O requests,within the context of single function calls,
standard I/O operations are atomic
 
void flockfile (FILE *stream);
void funlockfile (FILE *stream);
 

5.标准I/O的缺陷

The biggest complaint with standard I/O is the performance impact from the double copy
reading data: kernel ==> standard I/O buffer ==> application buffer
writing data: application data ==> standard I/O buffer ==> kernel

Linux System Programming 学习笔记(三) 标准缓冲I/O的更多相关文章

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

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

  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 学习笔记(二) 文件I/O

    1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...

  4. Linux System Programming 学习笔记(十) 信号

    1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0)   内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...

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

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

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

随机推荐

  1. Nginx: ubuntu系统上如何判断是否安装了Nginx?

    问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...

  2. pandas学习series和dataframe基础

    PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...

  3. 201621123080 《Java程序设计》第2周学习总结

    Week02-Java基本语法与类库 1. 本周学习总结 本周主要学习了java的数据类型.运算符,String类,java的简单输入输出与流程控制. 在做题上对String和数组的理解与区分仍不够深 ...

  4. Flask-基本原理与核心知识

    虚拟环境 使用pipenv创建一个虚拟环境和项目绑定,安装:E:\py\qiyue\flask>python3 -m pip install pipenv 和项目绑定:到项目的目录中pipenv ...

  5. 01、Linux介绍

    一. Linux介绍 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和 ...

  6. EditPlus 比较完整的快捷键记录

    FileFtpUpload Ctrl+Shift+S 上传文件到FTP 服务器 FileNew Ctrl+N 新建普通的文本文档 FileNewHtml Ctrl+Shift+N 创建一个空白的 HT ...

  7. debian安装之后使用android手机上网

    安装debian的过程中,没有连接网线.因为路由器在客厅,电脑在卧室,拖条长长的线很不方便. 断网安装完成之后,通过usb连上i9250. 在i9250上,执行以下操作: “设置”--->“更多 ...

  8. gpg: signing failed: secret key not available

    1 使用png签名tag时报错“ jb@39:~/11$ git tag -s gpg -m "gpg"gpg: directory `/home/jb/.gnupg' creat ...

  9. VR开发的烦恼——范围限制

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52230865 作者:car ...

  10. PHP如何利用sleep实现 输出->等待->输出

    sleep()函数一般用在暂停上,但是一个PHP一旦有了sleep,其他的输出(print,echo)就都要等待sleep()函数的完成,这是因为缓冲区,这里有详细解释 在这里不赘述,而如果要实现先输 ...