YuFeng Shen
YuFeng Shen

Reputation: 1565

what's the difference between fwrite(), write() , pwrite(),fread(), read() ,pread(), fsync() in Linux?

I suppose the fwrite() is pass the data from the user application to the buffer in the user mode, however write() is passing the data from the buffer in the user mode to the buffer in the kernel mode, and fsync() is passing the data from the buffer in the kernel mode to the disk .Right? and read() is passing data from buffer in kernel mode to buffer in user mode, and fread() is passing data from buffer in user mode to user Application , right? For pwrite() , besides lseek, it also call the fsync()?

Upvotes: 0

Views: 2200

Answers (1)

Armali
Armali

Reputation: 19375

For pwrite() , besides lseek, it also call the fsync()?

No, pwrite() does not call fsync(). See pwrite(3): on file - Linux man page:

The pwrite() function shall be equivalent to write(), except that it writes into a given position without changing the file pointer.

Also fsync() write data from kernel buffers to disk, so which system call read data from disk to kernel buffers ?

To write data from kernel buffers to disk, fsync() can be called, but it doesn't have to, if it suffices that the buffers are flushed eventually, which will happen anyway sooner or later, unless the system crashes or is reset.
To read data from disk to kernel buffers, having a dedicated system call is not needed1. The system knows from the read() call which data are to be read, and data must be read before the call returns (unless they are already buffered).
1What comes closest to such a system call is probably (as already mentioned by Tsyvarev) fadvise(2): Give advice about file access - Linux man page:

Allows an application to to tell the kernel how it expects to use a file handle, so that the kernel can choose appropriate read-ahead and caching techniques for access to the corresponding file.

Upvotes: 2

Related Questions