fseek() 在文件系统中是如何实现的? [英] How is fseek() implemented in the filesystem?

查看:30
本文介绍了fseek() 在文件系统中是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个纯粹的编程问题,但是它会影响使用 fseek() 的程序的性能,因此了解它是如何工作的很重要.一点免责声明,以免被关闭.

This is not a pure programming question, however it impacts the performance of programs using fseek(), hence it is important to know how it works. A little disclaimer so that it doesn't get closed.

我想知道在文件中间插入数据的效率如何.假设我有一个包含 1MB 数据的文件,然后我在 512KB 偏移量处插入了一些内容.与在文件末尾附加我的数据相比,它的效率如何?为了使示例完整,假设我想插入 16KB 的数据.

I am wondering how efficient it is to insert data in the middle of the file. Supposing I have a file with 1MB data and then I insert something at the 512KB offset. How efficient would that be compared to appending my data at the end of the file? Just to make the example complete lets say I want to insert 16KB of data.

我知道答案因文件系统而异,但是我认为常见文件系统中使用的技术非常相似,我只是想了解它的正确概念.

I understand the answer varies depending on the filesystem, however I assume that the techniques used in common filesystems are quite similar and I just want to get the right notion of it.

推荐答案

(免责声明:我只是想为这个有趣的讨论添加一些提示)恕我直言,有一些事情需要考虑:

(disclaimer: I want just to add some hints to this interesting discussion) IMHO there are some things to take into account:

1) fseek 不是一个主要的系统服务,而是一个库函数.为了评估其性能,我们必须考虑文件流库是如何实现的.一般来说,文件I/O库在用户空间增加了一层缓冲,所以如果目标位置在当前缓冲区内部或外部,fseek的性能可能会有很大的不同.此外,I/O 库使用的系统服务可能会有很大差异.IE.在某些系统上,如果可能,库会广泛使用文件内存映射.

1) fseek is not a primary system service, but a library function. To evaluate its performance we must consider how the file stream library is implemented. In general, the file I/O library adds a layer of buffering in user space, so the performance of fseek may be quite different if the target position is inside or outside the current buffer. Also, the system services that the I/O libary uses may vary a lot. I.e. on some systems the library uses extensively the file memory mapping if possible.

2) 正如您所说,不同的文件系统可能以非常不同的方式运行.特别是,我希望事务性文件系统必须做一些非常聪明而且可能很昂贵的事情,以准备好在文件中间进行中止的写入操作的可能回滚.

2) As you said, different filesystems may behave in a very different way. In particular, I would expect that a transactional filesystem must do something very smart and perhaps expensive to be prepared to a possible rollback of an aborted write operation in the middle of a file.

3) 现代操作系统具有非常激进的缓存算法.fseeked"文件可能已经存在于缓存中,因此操作变得更快.但是,如果其他进程产生的整体文件系统活动变得重要,它们可能会降级很多.

3) Modern OS'es have very aggressive caching algorithms. An "fseeked" file is likely to be already present in cache, so operations become much faster. But they may degrade a lot if the overall filesystem activity produced by other processes become important.

有什么意见吗?

这篇关于fseek() 在文件系统中是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆