c - 打开/读/写是否缓冲?

标签 c linux

我的程序中只使用了打开/读/写函数,但 oprofile 向我显示:

20537     2.9883  tyn_indexer              tyn_indexer              nodes_term32_flush
11966     1.7411  vmlinux                  vmlinux                  jbd2_journal_commit_transaction
11733     1.7072  vmlinux                  vmlinux                  __strnlen_user
10741     1.5629  vmlinux                  vmlinux                  nobh_truncate_page
9728      1.4155  vmlinux                  vmlinux                  generic_file_buffered_write
9443      1.3740  vmlinux                  vmlinux                  mpage_da_map_and_submit
9023      1.3129  vmlinux                  vmlinux                  do_get_write_access
7283      1.0597  vmlinux                  vmlinux                  invalidate_interrupt31
5894      0.8576  vmlinux                  vmlinux                  write_cache_pages_da
5332      0.7758  vmlinux                  vmlinux                  journal_submit_commit_record
5316      0.7735  vmlinux                  vmlinux                  hugetlbfs_symlink

generic_file_buffered_write 是怎么出来的,open/read/write 函数是缓冲的吗?

最佳答案

这取决于你所说的缓冲是什么意思。当 C 程序员说这些函数是无缓冲的时,这意味着没有应用程序进程的本地缓冲区 可以阻止其他进程以一致的方式查看数据。这些功能的实现(在内核中)可以自由地做尽可能多或尽可能少的缓冲,只要它以对应用程序进程透明的方式完成(即不干扰他们是否或何时看到数据在文件)。

另一方面,stdio(通常)是缓冲的,这意味着如果您使用 stdio(stdio.h 中的 FILE * 函数)访问文件,数据可能会从底层文件描述符中读取并缓冲在您进程的内存空间中,而其他进程在您实际打算读取和使用它之前看不到它,或者您写入的数据可能会保留在您进程的内存空间中其他进程在您编写后很久就看不到它的内存空间(除非您调用 fflush)。

关于c - 打开/读/写是否缓冲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9759019/

相关文章:

c++ - gcc 链接的默认库?

c - 二维数组自动增长

c - C信号死锁条件

在C中调用一个宏,结果是什么?

c 程序计算系统内存使用量?

ruby - 用于从 shell 管理 Virtualbox 的 Vagrant Ruby 代码

linux - shell脚本: when to test on the last command?

c - 来自 files.c 的 "mind map"生成器

c - 中止陷阱 : 6 (Calculating a long number factorial)

linux - KDE 登录屏幕上预填的用户名存储在哪里