Linux下的文件访问方法

发布于 2024-12-08 15:27:25 字数 209 浏览 4 评论 0原文

在课本上看到文件访问方式主要有两种;顺序和直接。我们在 Linux 中使用的是哪一种?

在读取命令中,我们给出要读取的字节数以及读取到哪个缓冲区。那么我们在 Linux 中可以进行顺序访问吗?

但是物理上我们存储的文件是块吗?我无法理解它。

Linux下能否直接访问?

我在 Galvin 的《操作系统概念》中读到了有关这些访问模型的内容

Read in text books that there are mainly two file access methods; sequential and direct. Which one we are using in Linux?

In read command we are giving the how much bytes to read and to which buffer. So we are having sequential access in Linux?

But physically we have files stored is blocks? I couldnt relate to it.

Whether direct access possible in Linux?

I read about these access models in Operating System Concepts by Galvin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能否归途做我良人 2024-12-15 15:27:25

两者皆有可能。

当您对普通文件执行读取时,它会按顺序读取文件,每次都会将文件指针前进适当的量。

但您也可以使用 seek 移动到文件中的任意点。

并非所有文件都支持随机/直接访问。例如,管道通常只能顺序访问(不能倒带或快进)。

所以几乎一切皆有可能,但某些文件类型有限制。

(使用直接 I/O(O_DIRECT 标志)进行文件访问是完全不同的概念。)

Both are possible.

When you do a read on an ordinary file, it does read the file sequentially, advancing the file pointer each time by the right amount.

But you can also use seek to move to an arbitrary point in the file.

Not all files support random/direct access. Pipes for instance are typically only sequential access (you can't rewind or skip forward).

So pretty much everything is possible, but some file types have restrictions.

(File access with direct I/O (O_DIRECT flag) is a different concept altogether.)

筱果果 2024-12-15 15:27:25

您当然可以从打开的(光盘)文件中的任意位置读取/写入。

有多种执行随机 IO 的方法,这些方法针对不同类型的用途进行了优化。

  • 最简单的方法是seek(),然后是read() 或write()。文件指针按照读/写的字节数移动,并且它可以允许随机跳转后的顺序 IO。将seek() 视为逻辑上旋转旧的“卷到卷”磁带驱动器(即使我们不再拥有这些)。
  • pread 和 pwrite 系统调用结合了eek() 和read/write(),专门用于多线程程序(其中两个系统调用将导致竞争条件)。它们不会更改文件指针,因此您可以从逻辑上认为它只是获取或放置随机数据位。
  • mmap() 将文件映射到内存中 - 然后您可以使用传统的指针/内存操作(例如 memset、memcpy 等)对它进行操作。

You can certainly read/write from an arbitrary position in an open (disc) file.

There are a number of methods of doing random IO, which are optimised for different kinds of usage.

  • The simplest method is seek() followed by read() or write(). The file pointer moves on by the amount of bytes read/written, and it can allow sequential IO following a random jump. Consider seek() as logically spinning the an old "reel-to-reel" tape drive (even though we don't have these any more).
  • The pread and pwrite system calls combine seek() and read/write(), specifically for use in multithreaded programs (where two syscalls would result in a race condition). They don't change the file pointer, so you can think of it logically just taking or putting a random bit of data.
  • mmap() maps a file into memory - where you can then do with it, what you will, using conventional pointer/ memory manipulation (for example, memset, memcpy, etc).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文