缓冲区

  1. 缓冲区分为3种
    全缓冲区、行缓冲区、无缓冲区

    1.  全缓冲区是对磁盘文件的,不存在\n刷新缓冲区的问题        对I/O方面flush,意味将缓冲区的内容写到磁盘上,对终端/驱动,flush会直接丢弃缓冲区的内容
    2.    行缓冲区  针对I/O库方面  ,遇到 \n  刷新缓冲区   
    3.   无缓冲区就是没有缓冲区,如使用write
        1 #include <stdio.h>
        2 
        3 int main()
        4 {
        5     printf("hello");
        6     fork();
        7 }
      

       ./a.out 输出2次 hello   fork 后子进程父进程复制了文件缓冲区 ,进程结束的时候会再次刷新缓冲区 所以会输出2此
        

        1 #include <stdio.h>
        2 #include <unistd.h>
        3 int main()
        4 {
        5     printf("hello");
        6     printf("nihao\n");
        7     pid_t pid =fork();
        8     if(pid == 0){
        9         printf("验证printf什么时候打印");
       10     }
       11 
       12 }
      ~                     
      

        

      hello nihao 验证~~~    直到第二个printf才才flush缓冲区  所以 fork不到父进程缓冲区内容

        1 #include <stdio.h>
        2 #include <unistd.h>
        3 int main()
        4 {
        5     printf("hello");
        6     printf("nihao\n");
        7     pid_t pid =fork();
        8 
      

        ./a.out > b.txt   输出2次  从定向到文件使用全缓冲  ,

        1 #include <stdio.h>
        2 #include <unistd.h>
        3 int main()
        4 {
        5     printf("hello\n");
        6     write(1,"killme",7);
        7     printf("nihao\n");
        8     pid_t pid =fork();
        9 
       10 }

      1 killme^@hellonihao
      2 hellonihao
      ~write无缓冲  

posted @ 2017-07-11 11:00  0x小小x0  阅读(118)  评论(0)    收藏  举报