Unbuffered low-level IO and Buffered high-level IO

本文探讨了Unix系统中的两种I/O操作:非缓冲I/O(如read、write)和缓冲I/O(如fread、fwrite)。通过具体示例说明了这两种I/O方式的区别及其在进程间通信中的应用。
 
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 ;
    // writeunbuffered
    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 bufferedPrintf执行后,并没有输出到stdout中。后面执行了fork,输出缓冲区也被拷贝到了child中。在parentchild中,再执行printf时,实际是向输出缓冲区追加内容。当parentchild结束时,输出缓冲区的内容被flush
 
由于这些问题很subtle,所以我们要特别注意这方面的问题。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值