在linux操作系统中,文件操作通常涉及系统调用,特别是open和close。这些系统调用通过内核的虚拟文件系统(vfs)和具体的文件系统(如ext4)来执行。让我们详细探讨这两个系统调用的过程和实现。

Posix之open
open系统调用允许用户态进程通过文件路径名称打开文件,并创建一个struct file结构体。这个结构体与进程的文件描述符(fd)关联,并返回给用户态进程以便后续操作。open系统调用的流程如下:

// 进入系统调用do_sys_open // 本进程内部申请一个空闲的fd get_unused_fd_flags // 根据open参数遍历并且通过inode初始化struct file do_filp_open // 根据路径名称定为文件dentry和目标文件的inode,返回struct file path_openat // 申请struct file alloc_empty_file // 准备查找路径起点 path_init // 逐级路径解析,并且查找 link_path_walk // 本层文件路径查找 walk_component // 在dcache中查找 lookup_fast // 读取具体的磁盘进行查找 lookup_slow // ext4查找目标文件的inode ext4_lookup ext4_find_entry // 目标文件inode创建 ext4_create __ext4_new_inode // 把fd打开的struct file关联起来 fd_install
Posix之close
close系统调用则涉及到解除进程内部的fd与struct file的关联,并释放相关资源,同时需要将struct file对应的inode数据刷新到磁盘。以下是close系统调用的实现过程:
// close系统调用 ksys_close // 释放fd,解绑struct file,同时flush inode __close_fd // 获取fd对应的struct file,解绑fd和struct file关系,调用方法释放资源(fd) files_fdtable // 调用struct file->f_op的flush方法,更新inode数据,最后释放struct file filp_close
具体的close系统调用代码如下:
static inline int ksys_close(unsigned int fd) {
return __close_fd(current->files, fd);
}
int __close_fd(struct files_struct *files, unsigned fd) {
struct file *file;
struct fdtable *fdt;
spin_lock(&files->file_lock);
// 获取文件描述符表
fdt = files_fdtable(files);
if (fd >= fdt->max_fds)
goto out_unlock;
// 获取fd对应的fd
file = fdt->fd[fd];
if (!file)
goto out_unlock;
rcu_assign_pointer(fdt->fd[fd], NULL);
// 释放fd资源,设置为可用
__put_unused_fd(files, fd);
spin_unlock(&files->file_lock);
// 调用struct file->f_op的flush方法,更新对应的inode
return filp_close(file, files);
out_unlock:
spin_unlock(&files->file_lock);
return -EBADF;
}整个open和close操作都是通过system call->vfs->ext4这样的路径来执行的,确保了文件操作的正确性和效率。
以上就是聊聊Posix语义之open和close系统调用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号