lseek() 的复杂度是 O(1) 吗?

2024-04-24

我知道我的问题在这里有答案:QFile 寻道性能 https://stackoverflow.com/questions/6171403/qfile-seek-performance。但我对这个答案并不完全满意。即使在查看了以下实现之后generic_file_llseek()对于ext4,我似乎无法理解如何衡量复杂性。

/**
 * generic_file_llseek - generic llseek implementation for regular files
 * @file:       file structure to seek on
 * @offset:     file offset to seek to
 * @origin:     type of seek
 *
 * This is a generic implemenation of ->llseek useable for all normal local
 * filesystems.  It just updates the file offset to the value specified by
 * @offset and @origin under i_mutex.
 */
loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
{
        loff_t rval;

        mutex_lock(&file->f_dentry->d_inode->i_mutex);
        rval = generic_file_llseek_unlocked(file, offset, origin);
        mutex_unlock(&file->f_dentry->d_inode->i_mutex);

        return rval;
}

/**
 * generic_file_llseek_unlocked - lockless generic llseek implementation
 * @file:       file structure to seek on
 * @offset:     file offset to seek to
 * @origin:     type of seek
 *
 * Updates the file offset to the value specified by @offset and @origin.
 * Locking must be provided by the caller.
 */
loff_t
generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
{
        struct inode *inode = file->f_mapping->host;

        switch (origin) {
        case SEEK_END:
                offset += inode->i_size;
                break;
        case SEEK_CUR:
                /*
                 * Here we special-case the lseek(fd, 0, SEEK_CUR)
                 * position-querying operation.  Avoid rewriting the "same"
                 * f_pos value back to the file because a concurrent read(),
                 * write() or lseek() might have altered it
                 */
                if (offset == 0)
                        return file->f_pos;
               break;
        }

        if (offset < 0 || offset > inode->i_sb->s_maxbytes)
                return -EINVAL;

        /* Special lock needed here? */
        if (offset != file->f_pos) {
                file->f_pos = offset;

                file->f_version = 0;
        }

        return offset;
}

举例来说,我有一个 4GB 的文件,并且我知道文件中间部分的偏移量,那么具体如何执行呢?lseek()让我在不遍历整个文件的情况下到达那里?操作系统是否已经知道文件的每个字节所在的位置?


lseek()如实施于ext4只会增加文件指针并进行一些验证检查。它不依赖于文件大小,这意味着它是O(1).

您也可以在代码中看到这一点,其中没有任何循环或可疑的函数调用。

然而,虽然这是事实ext4,对于其他文件系统可能不是这样,因为 POSIX 不保证这种行为。但这是可能的,除非文件系统有非常特殊的用途。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

lseek() 的复杂度是 O(1) 吗? 的相关文章

  • 如何使用 ASP.Net Core Identity 从登录用户检索 Google 个人资料图片?

    好的 我目前正在使用 ASP NET Core 1 1 2 和 ASP NET Core Identity 1 1 2 其中重要的部分是启动 cs看起来像这样 public void Configure IApplicationBuilde
  • 类似于 Active Directory 中的搜索

    我正在使用 C 中的以下代码搜索 LDAP 以轮询用户的活动目录 DirectoryEntry entry new DirectoryEntry ldapPath userName password DirectorySearcher Se
  • 如何利用磁盘 IO 队列

    我需要从 3 7 GB 文件中读取小数据序列 我需要阅读的职位是不相邻 但我可以命令 IO 以便从头到尾读取文件 该文件存储在 iSCSI SAN 上 该 SAN 应该能够处理 优化排队 IO 问题是 如何一次性请求我需要的所有数据 位置
  • 删除 QComboBox“下拉”动画

    我正在使用 Qt 4 8 并且想在单击 QComboBox 时摆脱 下拉 动画 我也想稍微移动一下 到目前为止 我一直在考虑重新实现 showPopup 和 hidePopup 但不知道如何使其工作 此外 每次我尝试使用 CSS 进行移动或
  • 时间:2019-03-17 标签:c++rapidjson返回值

    我在我的项目中使用rapidjson 我有一个方法可以解析 json 并返回其中的一部分 static rapidjson Document getStructureInfo std string structureType rapidjs
  • 从命名管道读取

    我必须实现一个 打印服务器 我有 1 个客户端文件和 1 个服务器文件 include
  • Windows 10 ScrollIntoView() 不会滚动到列表视图中间的项目

    我有一个包含 20 个项目的列表视图 我想以编程方式滚动列表视图 ListView ScrollIntoView ListView Items 0 将滚动列表视图到第一项 ListView ScrollIntoView ListView I
  • winapi 函数的函数指针 (stdcall/cdecl)

    请有人给我一些为 MS winapi 函数创建函数指针的提示吗 我试图为 DefWindowProc DefWindowProcA DefWindowProcW 创建一个指针 但出现此错误 LRESULT dwp HWND UINT WPA
  • 为什么即将推出的 Ranges 库不支持范围内的容器初始化?

    介绍 随着即将推出的 Ranges 库 用两个迭代器表示范围的需要几乎消失了 例如 代替 if std equal begin foo end foo begin bar end bar we have if std ranges equa
  • 将数据表传递给存储过程

    我有一个用 C 创建的数据表 using DataTable dt new DataTable dt Columns Add MetricId typeof int dt Columns Add Descr typeof string dt
  • Fluent NHibernate 一对一映射

    我很难利用 Fluent NHibernate 的 HasOne 映射 基本上 A 类在 B 类中可以有匹配的 只有一条或没有 记录 请帮助定义关系的 AMap 和 BMap 类 谢谢 public class A public virtu
  • C++ 按值而不是按引用将数组发送到函数

    我的 C 有问题 我有一个对数组进行排序的函数 但我不想处理原始数组 我想通过值而不是通过引用将数组发送到函数 请帮我 int bogoSort int tab int n int iloscOperacjiDominujacych 0 c
  • 私有静态方法有必要吗?

    原理工程师 https stackoverflow com users 201787 metal在我上一家公司有一条规则private static方法应该作为实现文件中的函数实现 而不是作为类方法 我不记得他的规则是否有任何例外 我在目前
  • Eclipse 调试模式下的 GDB 找不到 stdlib/rand.c

    我试图让 gdb 在 ubuntu 上与 eclipse cdt 一起运行 以开始调试一些简单的程序 所以我做了我认为必要的步骤来让它运行 1 创建可执行项目 2 Compile 3 Run 4 创建文件 gdbinit 并将其放在主项目文
  • 如何使用 html 敏捷包获取自定义标签?

    需要创建摘要 索引 为此我有标签
  • 升压参数库

    最近我发现参数 http www boost org doc libs 1 50 0 libs parameter doc html index htmlBoost 中的库 老实说 我不明白为什么这是 Boost 的一部分 当需要向函数传递
  • 虚拟继承 - 跳过构造函数

    我有以下课程 class Socket Socket Socket SOCKET s class Connection public virtual Socket Connection IP ip 这两个类包含一些纯虚函数和一些非虚函数以及
  • K&R 之后用什么书来学习纯 C 编程? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • for循环内递归函数的时间复杂度

    如果我们有一个函数 int x 0 int fun int n if n 0 return 1 for int i 0 i
  • 如何将谓词作为参数传递#

    如何将谓词传递到方法中 但在没有传递谓词的情况下仍使其工作 我想也许是这样的 但似乎并不正确 private bool NoFilter return true private List

随机推荐