System calls occur in kernel space, so if you use system calls for file operations in a generic application in user space, there is a cost of user-space-to-kernel-space switching. In fact, even if the library function is used in user space to manipulate the file, because the file always exists on the storage medium, both the read and write operations, which are the operations of the hardware ( memory ) , will inevitably cause system calls. In other words, the operation of a library function on a file is actually implemented through a system call. For example , the C library function fwrite () is implemented through the write () system call.
In this case, using library functions also has the overhead of system calls, so why not use system calls directly? This is because the use of library functions can greatly reduce the number of system calls, since the read-write file is usually a large amount of data ( which is a large number of data manipulation units implemented relative to the underlying driver's system call ). This result is also due to the buffer technology. In both user and kernel space, buffers are used for file operations, such as writing files with fwrite , which writes the contents to the user space buffer, and writes the contents of the user buffer to the kernel buffer when the user space buffer is full or the write operation ends.
Differences between Linux system calls and library function calls