Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User space buffer and Kernel buffer

Tags:

c

linux

buffer

I am recently studying file system in linux. I learned that when we call fopen(), the library call will call malloc() to allocate space for the FILE structure, and inside this FILE structure there will be a buffer for I/0. But later on I found that the write system call actually writes the data to the kernel buffer, So what's the difference between these two buffers?

like image 472
Louis Kuang Avatar asked Nov 10 '15 12:11

Louis Kuang


2 Answers

You have to understand two things: fwrite() is a standard library's routine operating on FILE structure, but write() is a system call. I bet fwrite() uses write() internally. Nothing keeps fwrite() from providing user space IO-buffering until it is ready to pass your data on to the write() syscall.

write() syscall in it's turn goes straight to the kernel and says: "Hey kernel, I've got this user space buffer here. Will you write this one to the storage for me?". And here it's up to the kernel what to do next: it will either go directly to storage to write the data, or, most likely, copy the data to kernel buffer, until it decides it's time to modify storage data.

Turning back to your question. Any kind of buffering is done to accumulate data in order to postpone turning to more expensive operations: standard library may consider invoking syscall on every len-byte expensive, kernel considers going to hard disk on every syscall expensive and so on.

You might want to read this to see how far buffering goes https://fgiesen.wordpress.com/2015/10/25/reading-and-writing-are-less-symmetric-than-you-probably-think/

like image 149
Igor S.K. Avatar answered Nov 10 '22 17:11

Igor S.K.


The FILE structure holds the meta data about the opened file (mode, stream position, etc). It is part of the C Standard I/O interface. The buffer allocated as part of FILE takes only a limited amount of data (e.g. when the stream is buffered). It is deallocated upon fclose(). You may even provide your own user space stdio buffer with setvbuf().

The kernel buffer receives the file contents written by write(), whenever the stream is flushed or the associated file descriptor is closed.

like image 28
Jens Avatar answered Nov 10 '22 17:11

Jens