简体   繁体   English

读写C系统调用是否使用缓冲区?

[英]Do read and write C system calls use buffers?

I was talking with a teacher and he told me that read and write system calls was using buffers, because there is a variable in your system spec that controls how many times you can have access to the device you want to read/write on, and the system uses buffer to stock data while he is waiting for writing on the device. 我正在和老师谈话,他告诉我读写系统调用是使用缓冲区,因为系统规范中有一个变量控制着你可以访问你想要读/写的设备的次数,以及系统在等待写入设备时使用缓冲区来存储数据。

I saw on an other Stack Overflow post ( C fopen vs open ) that one of the advantages of fopen and fwrite functions was that those functions were using buffers (which is supposed to be way faster). 我在另一个Stack Overflow帖子( C fopen vs open )上看到fopenfwrite函数的一个优点是那些函数正在使用缓冲区(假设速度更快)。 I have read the man page of read and write sys calls, and the man pages do not talk about any buffers. 我已经阅读了readwrite sys调用的手册页,并且手册页没有讨论任何缓冲区。

Did I misunderstood something ? 我误解了什么吗? How do read / write C syscall buffers work? read / write C系统调用缓冲区如何工作?

The functions you mention, read and write are system calls, therefore their behavior is platform dependent. 您提及, readwrite的函数是系统调用,因此它们的行为取决于平台。

As you know, fread and fwrite are C standard library functions. 如您所知, freadfwrite是C标准库函数。 They do buffering in the user space and in this way optimize the performance for typical application. 它们在用户空间中进行缓冲,并以此方式优化典型应用程序的性能。 read and write are different. readwrite是不同的。 There is some stub code in userspace C libraries (such as GNU libc) for these functions, but the main function of that code is just to provide a convenient wrapper for invoking the right kernel functionality (but it's also possible to invoke that functionality with syscall() directly!) 这些函数在用户空间C库(例如GNU libc)中有一些存根代码,但该代码的主要功能只是为调用正确的内核功能提供方便的包装(但也可以使用syscall()来调用该功能) syscall()直接!)

If you're interested in the details, here is an example: the wrapper for write system call in the uclibc library. 如果你感兴趣的细节, 这里有一个例子:用于包装write在uClibc的图书馆系统调用。

So the typical implementations of read and write do not do buffering in user space. 因此,典型的实现readwrite没有在用户空间做缓冲。 They may still do buffering in the kernel space , though. 但是,它们仍然可以在内核空间中进行缓冲。 Read about the O_DIRECT flag for more details: How are the O_SYNC and O_DIRECT flags in open(2) different/alike? 阅读有关O_DIRECT标志的更多详细信息: 打开(2)中的O_SYNC和O_DIRECT标志如何不同/相似?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM