• [Linux]----文件操作(重定向+缓冲区)



    前言

    本节继续基于上节文件描述符继续往下拓展,讲一讲关于文件操作的重定向和缓冲区。


    正文开始!

    首先来基于上节课的问题

    #include
    #include
    #include
    #include
    #include
    #include
    int main()
    {
        close(1);
        int fd=open("log.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
        if(fd<0)
        {
            perror("open");
            return 1;
        }
        fprintf(stdout,"打开文件成功,fd=%d",fd);
        //fflush(stdout);
        close(fd);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述
    我们发现既没有往显示器打印,也没有往文件里面打印!

    然后我们将fflush()这一行代码取消注释后

    在这里插入图片描述
    我们可以发现打印内容到了文件中了。至于为什么要用fflush()函数,需要了解到缓冲区的内容,接下来带大家理解!

    那我有一个问题了?为什么不往显示器去打印,而是打印在了文件中呢???

    一、重定向

    在这里插入图片描述
    如果我们要进行重定向,上层只认0,1,2,3这样的fd,我们可以在OS内部,通过一定的方式调整数组的特定下标内容够(指向),我们就可以完成重定向操作!

    对于上图,我们进行重定向后,fprintf并不知道1号文件描述符指向了"log.txt"文件,而继续向1号文件描述符打印东西。

    具体操作

    上面的一堆的数据,都是内核数据结构。只有谁有权限呢???

    必定是操作系统(OS)---->必定提供系统结构!

    dup2

    在这里插入图片描述

    相比于dup,dup2更复杂一些,我们今天主要使用多duo2进行重定向操作!

    在这里插入图片描述
    在这里我们首先进行输出重定向

    stdout–>1 log.txt–>fd

    1. 那么对于dup2()接口,谁是谁的一份拷贝呢?

      对于上面框起来的内容翻译就是newfd是oldfd的一份拷贝,就是把oldfd的内容放置newfd里面。最后只剩oldfd了!!!

    2. 参数怎么传呢??
      我们要输出重定向到文件中,即就是stdout的输出到文件中,即就是1号文件描述符的内容要指向新创建文件的描述符。

    dup2(fd,1);

    int main()
    {
    
        int fd=open("log.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
        if(fd<0)
        {
            perror("open");
            return 1;
        }
        dup2(fd,1);
        fprintf(stdout,"打开文件成功,fd=%d",fd);
    
        fflush(stdout);
        close(fd);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    输入重定向

    int main()
    {
    
        int fd=open("log.txt",O_RDONLY);
        if(fd<0)
        {
            perror("open");
            return 1;
        }
        dup2(fd,0);
        char line[64];
        while(fgets(line,sizeof(line),stdin)!=NULL)
        {
            printf(line);
        }
    
        fflush(stdout);
        close(fd);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    二、关于缓冲区的理解

    1. 什么是缓冲区

    • 缓冲区的本质:就是一段内存。

    2. 为什么要有缓冲区

    • 解放使用缓冲区的进程时间
    • 缓冲区的存在可以集中处理数据刷新,减少IO的次数,从而达到提高整机的效率的目的!

    3. 缓冲区在哪里

    我来写一份代码带大家验证一下!

    int main()
    {
        printf("hello printf\n");   //stdout-->1
        const char* msg="hello write\n";
        write(1,msg,strlen(msg));
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    去掉’'以后

    int main()
    {
        printf("hello printf");   //stdout-->1
        const char* msg="hello write";
        write(1,msg,strlen(msg));
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    printf没有立即刷新的原因,是因为有缓冲区的存在

    write可是立即刷新的!

    所以我们根据以上的实验现象我们可以发现stdout必定封装了write!

    那么这个缓冲区不在哪里?? ---->一定不在wirte内部!

    所以我们曾经讨论的缓冲区,不是内核级别的!

    所以这个缓冲区在哪里???—>只能是C语言提供的!!!(语言级别的缓冲区)

    因为printf是往stdout中打印,stdout—>FILE—>struct—>封装很多的属性---->fd—>该FILE对于的语言级别的缓冲区!

    在这里插入图片描述

    刷新策略机制

    int main()
    {
        printf("hello printf");//stdout->1->封装了write
        fprintf(stdout,"hello fprintf");
        fputs("hello fputs",stdout);
    
        const char* msg="hello write";
        write(1,msg,strlen(msg));
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    什么时候刷新?

    • 无缓冲(立即刷新)
    • 行缓冲(逐行刷新)—>显示器文件
    • 全缓冲(缓冲区满,刷新)—>块设备对应的文件(磁盘文件)

    无缓冲的特殊情况
    a.进程退出
    b.用户强制刷新

    如果在刷新之前,关闭了fd会有什么问题?

    int main()
    {
        printf("hello printf");//stdout->1->封装了write
        fprintf(stdout,"hello fprintf");
        fputs("hello fputs",stdout);
    
        const char* msg="hello write";
        write(1,msg,strlen(msg));
        close(1);
        //close(stdout->_fileno);//和上面close(1)效果相同
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述
    一开始我们只是把数据写入到FILE结构体的缓冲区,然后你把文件描述符给关了,当然就写不进文件中了!!!

    在这里插入图片描述
    现在我们就能来理解一开始我们抛出的问题了!!!

    既然缓冲区在FILE内部,在C语言中,而我们每一次打开一个文件,都要有一个FILE*会返回!!
    那就意味着,每一个文件都有一个fd和属于他自己的语言级别的缓冲区!!!

    FLIE内部的封装

    /usr/include/libio.h
    struct _IO_FILE {
     int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
    #define _IO_file_flags _flags
     //缓冲区相关
     /* The following pointers correspond to the C++ streambuf protocol. */
     /* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
     char* _IO_read_ptr; /* Current read pointer */
     char* _IO_read_end; /* End of get area. */
     char* _IO_read_base; /* Start of putback+get area. */
     char* _IO_write_base; /* Start of put area. */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    特殊情况

    int main()
    {
        const char* str1="hello printf\n";
        const char* str2="hello fprintf\n";
        const char* str3="hello fputs\n";
        const char* str4="hello write\n";
        
        //C库函数
        printf(str1);
        fprintf(stdout,str2);
        fputs(str3,stdout);
        
        //系统接口
        write(1,str4,strlen(str4));
        
        //是调用完了上面的代码,才执行的fork
        fork();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    对代码去掉’\n’

    int main()
    {
        const char* str1="hello printf";
        const char* str2="hello fprintf";
        const char* str3="hello fputs";
        const char* str4="hello write";
        
        //C库函数
        printf(str1);
        fprintf(stdout,str2);
        fputs(str3,stdout);
        
        //系统接口
        write(1,str4,strlen(str4));
        
        //是调用完了上面的代码,才执行的fork
        fork();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    1. 刷新的本质,就是把缓冲区的数据写到OS内部,清空缓冲区!
    2. 缓冲区是自己的FILE内部维护的,属于父进程
    3. 子进程也继承了父进程的缓冲区,也就打印了父进程缓冲区的内容!

    模拟实现封装C标准库

    只封装了C语言库的一部分!

    #include
    #include
    #include
    #include 
    #include
    #include
    #include
    #include
    
    #define NUM 1024
    
    #define NONE_FLUSH 0x0
    #define LINE_FLUSH 0x1
    #define FULL_FLUSH 0x2
    
    typedef struct _MyFILE
    {
        int _fileno;
        char buffer[NUM];
        int _end;
        int _flags;//fflush method
    }MyFILE;
    
    MyFILE* my_fopen(const char* filename,const char* method)
    {
        assert(filename&&method);
        int flags=O_RDONLY;
    
        if(strcmp(method,"r")==0)
        {}
        else if(strcmp(method,"r+")==0)
        {} 
        else if(strcmp(method,"w")==0)
        {
            flags=O_WRONLY|O_CREAT|O_TRUNC;
    
        }
        else if(strcmp(method,"w+")==0)
        {}
        else if(strcmp(method,"a")==0)
        {
            flags=O_WRONLY|O_CREAT|O_APPEND;
        }
        else if(strcmp(method,"a+")==0)
        {}
        
        int fileno=open(filename,flags,0666);
        if(fileno<0)  return NULL;
        MyFILE* fp=(MyFILE*)malloc(sizeof(MyFILE));
        if(fp==NULL)
        {
            return NULL;
        }
        memset(fp,0,sizeof(MyFILE));
        fp->_fileno=fileno;
        fp->_flags|=LINE_FLUSH;
        fp->_end=0;
        return fp;
    }
    
    void my_fflush(MyFILE* fp)
    {
        assert(fp);
        if(fp->_end>0)
        {
            write(fp->_fileno,fp->buffer,fp->_end);
            fp->_end=0;
            syncfs(fp->_fileno);
        }
        
    }
    
    
    void my_fwrite(MyFILE* fp,const char* start,int len)
    {
        assert(fp&&start&&len>0);
    
        strncpy(fp->buffer+fp->_end,start,len);//将数据写入到缓冲区了
        fp->_end+=len;
    
        if(fp->_flags&NONE_FLUSH)
        {
    
        }
        else if(fp->_flags&LINE_FLUSH)
        {
            if(fp->_end>0&&fp->buffer[fp->_end-1]=='\n')
            {
                //仅仅是写入到内核中
                write(fp->_fileno,fp->buffer,fp->_end);
                fp->_end=0;
            }
        }
        else if(fp->_flags&FULL_FLUSH)
        {
    
        }
    }
    
    void my_close(MyFILE* fp)
    {
        my_fflush(fp);
        close(fp->_fileno);
        free(fp);
    }
    
    int main()
    {
        MyFILE* fp=my_fopen("log.txt","w");
        if(fp==NULL)
        {
            printf("my_fopen fail\n");
            return 1;
        }
        const char* s="hello my 111\n";
        my_fwrite(fp,s,strlen(s));
        
        printf("消息立即刷新");
        sleep(3);
    
    
        const char* ss="hello my 222";
        my_fwrite(fp,ss,strlen(ss));
        sleep(3);
        printf("写入了一个不满足条件的字符串\n");
    
    
        const char* sss="hello my 333";
        my_fwrite(fp,sss,strlen(sss));
        sleep(3);
        printf("写入了一个不满足条件的字符串\n");
    
    
        const char* ssss="end\n";
        my_fwrite(fp,ssss,strlen(ssss));
        sleep(3);
        printf("写入了一个满足条件的字符串\n");
    
        const char* sssss="aaaaaaaaaa";
        my_fwrite(fp,sssss,strlen(sssss));
        printf("写入了一个不满足条件的字符串\n");
        
        sleep(1);
        my_fflush(fp);
        sleep(3);
    
        my_close(fp);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149

    标准输出和标准错误

    #include
    #include
    
    int main()
    {
        //stdout
        printf("hello printf 1\n");
        fprintf(stdout,"hello fprintf 1\n");
        fputs("hello puts 1\n",stdout);
    
        //stderr
        fprintf(stderr,"hello fprintf 2\n");
        fputs("hello puts 2\n",stderr);
        perror("hello perror 2");
    
        //cout
        std::cout<<"hello cout 1"<<std::endl;
    
        //cerr
        std::cerr<<"hello cerr 2"<<std::endl;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    我们发现打1的都不见了

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    向stdout里面打的都重定向到log.txt文件中,stderr继续打印到显示器。

    在这里插入图片描述

    ./a.out >stdout.txt 2>stderr.txt

    一条语句进行两次重定向

    那么意义在哪里呢?

    可以区分那些是程序日常输出,那些是错误!

    我们也可以将上面的两个文件打印在一个文件

    ./a.out >all.txt 2>&1

    在这里插入图片描述


    总结

    (本章完!)
    下节课我们基于重定义的学习来完善我们之前写的myshell!!!

  • 相关阅读:
    防火墙NAT实验(接上一个用认证实验)
    oracle删除重复的数据
    qDebug() 显示行号
    (附源码)计算机毕业设计JavaJava毕设项目美容院管理系统
    Python爬虫实战,requests+parsel模块,爬取二手房房源信息数据
    黑马JVM总结(三十二)
    【算法入门与九月打卡】—— 第五天
    【pytorch】torchvion.transforms.RandomResizedCrop
    数字时代,商业智能BI的落地意味着什么
    手机和WINDOWS电脑蓝牙连接后怎样放歌,无法选择媒体音频 蓝牙媒体音频勾选不上
  • 原文地址:https://blog.csdn.net/m0_61560468/article/details/127671789