• 文件IO


    标准 C 库 IO 和 Linux 系统 IO 的关系

    在这里插入图片描述
    使用fopen打开hello.txt文件,返回FILE *fp文件指针,指向一个结构体,文件描述符(文件句柄)(整数值)索引到你打开的文件,文件读写指针位置是用来读写数据,IO缓冲区(内存地址)找到对应的内存块,再将数据从内存刷新到磁盘。
    缓冲区提高效率
    在这里插入图片描述
    在这里插入图片描述

    虚拟地址空间

    在这里插入图片描述
    解决程序加载问题和调用栈堆这些

    文件描述符

    在这里插入图片描述由PCB管理文件描述符,其中标准输入、输出、错误默认打开

    Linux 系统IO 函数

    int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);
    
    int close(int fd);
    
    ssize_t read(int fd, void *buf, size_t count);
    ssize_t write(int fd, const void *buf, size_t count);
    
    off_t lseek(int fd, off_t offset, int whence);
    
    int stat(const char *pathname, struct stat *statbuf);
    int lstat(const char *pathname, struct stat *statbuf);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    chmod命令使用数字修改文件权限
    Linux 系统中,文件的基本权限由 9 个字符组成,以 rwxrw-r-x 为例,我们可以使用数字来代表各个权限,各个权限与数字的对应关系如下:

    r --> 4
    w --> 2
    x --> 1
    
    • 1
    • 2
    • 3

    由于这 9 个字符分属 3 类用户,因此每种用户身份包含 3 个权限(r、w、x),通过将 3 个权限对应的数字累加,最终得到的值即可作为每种用户所具有的权限。

    拿 rwxrw-r-x 来说,所有者、所属组和其他人分别对应的权限值为:

    所有者 = rwx = 4+2+1 = 7
    所属组 = rw- = 4+2 = 6
    其他人 = r-x = 4+1 = 5
    
    • 1
    • 2
    • 3
    struct stat {
    dev_t st_dev; // 文件的设备编号
    ino_t st_ino; // 节点
    mode_t st_mode; // 文件的类型和存取的权限
    nlink_t st_nlink; // 连到该文件的硬连接数目
    uid_t st_uid; // 用户ID
    gid_t st_gid; // 组ID
    dev_t st_rdev; // 设备文件的设备编号
    off_t st_size; // 文件字节数(文件大小)
    blksize_t st_blksize; // 块大小
    blkcnt_t st_blocks; // 块数
    time_t st_atime; // 最后一次访问时间
    time_t st_mtime; // 最后一次修改时间
    time_t st_ctime; // 最后一次改变时间(指属性)
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    文件属性操作函数

    int access(const char *pathname, int mode);
    int chmod(const char *filename, int mode);
    int chown(const char *path, uid_t owner, gid_t group);
    int truncate(const char *path, off_t length);
    
    • 1
    • 2
    • 3
    • 4

    目录操作函数

    int rename(const char *oldpath, const char *newpath);
    int chdir(const char *path);
    char *getcwd(char *buf, size_t size);
    int mkdir(const char *pathname, mode_t mode);
    int rmdir(const char *pathname);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    目录遍历函数

    DIR *opendir(const char *name);
    struct dirent *readdir(DIR *dirp);
    int closedir(DIR *dirp);
    
    • 1
    • 2
    • 3

    dup、dup2 函数

    int dup(int oldfd);
    复制文件描述符
    int dup2(int oldfd, int newfd);
    重定向文件描述符
    
    • 1
    • 2
    • 3
    • 4

    fcntl 函数

    int fcntl(int fd, int cmd, ... /* arg */ );
    复制文件描述符
    设置/获取文件的状态标志
    
    • 1
    • 2
    • 3
  • 相关阅读:
    双坐标轴柱状图
    9、Spring之推断构造方法源码解析
    arcgis如何给没有连通的路打交点
    Android学习笔记 36. 网络加载框架OkHttp的同步与异步请求
    PG索引失败排查记录
    每个表空间数据文件数的限制
    强化学习基础
    Tomcat的Engine容器
    霸道!阿里最新版Spring Cloud Alibaba项目文档,竟将重要组件弃用
    Python爬虫-使用代理伪装IP
  • 原文地址:https://blog.csdn.net/qq_42413161/article/details/126685231