Unbuffered low-level IO and Buffered high-level IO
来源:互联网 发布:电影源码是什么 编辑:程序博客网 时间:2024/05/19 20:19
Unbuffered low-level IO and Buffered high-level IO
1,概述
Unix提供的函数如read,write等都属于Unbuffered IO函数。
Standard IO提供的函数如fread,fwrite都属于Buffered IO函数。Standard IO的底层都是调用read,write来实现的。
2,Unbuffered
Unbuffered意味着每次调用read,write都引起内核的一个系统调用,将对文件进行实际的读写。
3,Buffered
Buffer为了最少次数的调用read,write函数,从而提升性能。
Stream
Buffering type
Stderr
Always unbufferer
Streams referring to a
Terminal (interactive) device
Line Bufferer
All other streams
Full Buffer
4,例子
[1] 父进程发送消息给子进程,子进程读取消息后返回,父进程等待子进程结束
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
int main() {
pid_t pid ;
int fd[2] ;
int rtn ;
pipe( fd ) ;
pid = fork() ;
if ( pid == 0 ) { // child
close ( fd[1] ) ;
char info[56]= { 0 } ;
dup2( fd[0] , STDIN_FILENO ) ;
//read( fd[0] , info , 56 ) ;
read( STDIN_FILENO , info , 56 ) ; ß读取父进程发送的信息
printf("child from parent: %s/n" , info);
close ( fd[0] ) ;
return 0 ;
} else { // parent
char * pstr = "parent send to child : " ;
close( fd[0] ) ;
dup2( fd[1] , STDOUT_FILENO ) ;
printf("%s" ,pstr ); ß如果没有fflush调用的话,child将在read处阻塞
fflush( stdout ) ; ß因为是fully buffered
/* 管道中一个进程的输出,作为下一个进程的输入,大概就是使用这种方式 */
//write( fd[1] , pstr , strlen("parent: send to child/n") ) ;
//write( STDOUT_FILENO , pstr , strlen("parent: send to child/n") ) ;
// 如果使用以上任意一种的话,将没有阻塞现象,因为是unbuffered的
waitpid( pid , &rtn , 0) ; ß等待child结束
close ( fd[1] ) ;
}
return 0 ;
}
[2]
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
int glob = 6 ;
char buf[] = "a write to stdout/n";
int main(void) {
pid_t pid;
int var = 88 ;
// write是unbuffered
write(STDOUT_FILENO, buf, sizeof(buf)-1) ;
printf("before fork/n"); ß line buffered,如果和interactive terminal连接的话
ß否则就是fully buffered
if ((pid = fork()) < 0) {
printf("fork error");
return (-1) ;
} else if (pid == 0) { // child
glob++;
var++;
} else { // parent
sleep(2);
}
printf("pid = %d, glob = %d, var = %d/n", getpid(), glob, var);
return (0);
}
程序的运行结果为:
$ ./a.out
a write to stdout
before fork
pid = 430, glob = 7, var = 89
pid = 429, glob = 6, var = 88
$ ./a.out > temp.out
$ cat temp.out
a write to stdout
before fork
pid = 432, glob = 7, var = 89
before fork
pid = 431, glob = 6, var = 88
解释一下:
[1] write是unbuffered,所以在执行fork时,已经输出到stdout中了。
[2] 直接执行时,stdout和interactive terminal相连接,所以是line buffered。Printf执行后,就被输出到stdout中了。因为字符串以换行符结束。
[3] 输出重定向执行时,stdout被重定向为文件temp.out。所以是fully buffered。Printf执行后,并没有输出到stdout中。后面执行了fork,输出缓冲区也被拷贝到了child中。在parent,child中,再执行printf时,实际是向输出缓冲区追加内容。当parent或child结束时,输出缓冲区的内容被flush。
由于这些问题很subtle,所以我们要特别注意这方面的问题。
- Unbuffered low-level IO and Buffered high-level IO
- low level descriptors and high level descriptors
- buffered, unbuffered, direct IO的区别
- zabbix应用之Low-level discovery监控磁盘IO
- zabbix监控磁盘IO low-level-discory方式-iostat说明
- zabbix应用之Low-level discovery监控磁盘IO
- Top-level const and low-level const
- Write Great Code: Thinking Low-level, Writing High-level
- Kafka:High level consumer vs. Low level consumer
- Kafka High Level API vs. Low Level API
- Low Level Operators and Bit Fields
- Low Level Operators and Bit Fields
- Wargames:io.smashthestack Level 1
- Wargames:io.smashthestack Level 2
- Low Level MIDI API
- Low-level text rendering
- RPi Low-level peripherals
- python low level thread
- javascript的test方法的“陷阱”
- Access和sql server的语法区别 (包括db2 oracle)
- SQL中以count及sum为条件的查询---转(http://kiddymeet.51cto.com/blog/20194/19376)
- 范型算法与模板参数推演
- 管道和xargs命令
- Unbuffered low-level IO and Buffered high-level IO
- Dialog & Windows 使用技巧
- 跳过验证,安装IE7.0
- JAVA生成压缩文件
- ContextMenuStrip 中添加子菜单
- 世界500强企业优秀员工的十二条核心标准
- 什么是TNotifyEvent
- 使用SVN时遇到的变态的sc命令
- p-unit 教程3 -- 执行参数化测试案例