There are two ways to manipulate files under Linux: System call and library function call (library functions). The system call actually refers to the bottom-level invocation, which is the meaning of the underlying call in the Linux programming. The hardware is oriented. The library function call is for the application development, the equivalent of the application API, in such a way there are many reasons, first: the implementation of double buffering technology. The second is portability. Thirdly, some performance aspects of the underlying invocation itself are flawed. IV: Let the API also have a level and dedicated work-oriented.
1, system call
The system call provides functions such as open, close, read, write, IOCTL, etc., which need to include the header file Unistd.h. Write as an example: its function prototype is size_t write (int fd, const void *BUF, size_t Nbytes), whose action object is a file descriptor or file handle FD (filename descriptor), to write a file, you must first open a file with writable permission, to obtain the FD of the open file, such as Fd=open (\ "/dev/video\" , O_RDWR). FD is an integer value, with each new file opened, the FD obtained is the current maximum FD plus 1. The Linux system is assigned a default of 3 file descriptor values: 0-standard input,1-standard output,2-standard error.
System calls are typically used for the underlying file access (low-level files access), such as direct access to the device files in the driver.
System calls are operating system-related, so there is generally no portability across operating systems.
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 the user space and kernel space, the use of the file operation of the buffer, for example, write a file with fwrite, the content is written to the user space buffer, when the user space buffer full or the end of the write operation, the contents of the user buffer is written to the kernel buffer, the same reason, The kernel buffer content is written to the hardware medium that corresponds to the file when the kernel buffer is full or the write is complete.
2. Library function call
Library function calls are typically used for access to generic files in an application.
Library function calls are system-independent, so portability is good.
※ function library Call VS system call
Differences between Linux system calls and library function calls