Linux has two ways of file operations: system call and library functions. You can refer to "Linux Programming" (English original "Beginning Linux Programming" by Neil Matthew and Richard Stones) Chapter 3: Working with files. The system call actually refers to the lowest level of a call, in the linux programming is the meaning of the underlying call. It is geared towards hardware. The library function call is oriented to application development, which is equivalent to the application api. There are many reasons for this. First, the implementation of double buffering technology. Second, portability. Third, some of the performance flaws of the underlying call itself. Fourth: Let api also have level and specialized work oriented.
1,
system call system call functions provided as open, close, read, write, ioctl, etc., need to include the header file unistd.h. Take write as an example: its function prototype is size_t write (int fd, const void *buf, size_t nbytes), its operation object is file descriptor or file handle fd (file descriptor), in order to write a file, you must first Write permission Opens a file with the open system call and gets the fd of the open file, for example fd=open(\\"/dev/video\\", O_RDWR). Fd is an integer value. For each new file opened, the obtained fd is the current maximum fd plus one. The Linux system allocates three file descriptor values by default: 0-standard input, 1-standard output, 2-standard error.
System calls are typically used for low-level file access, such as direct access to 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 general application in user space, there is overhead for user space to kernel space switching. In fact, even if you use a library function to manipulate a file in user space, because the file always exists on the storage medium, the operation of the hardware (memory), whether it is a read or write operation, will inevitably cause a system call. In other words, the library function's operation on the file is actually implemented through a system call. For example, the C library function fwrite() is implemented by the write() system call.
In this case, the use of library functions also has the overhead of system calls, why not use system calls directly? This is because read and write files are usually large amounts of data (this is a large amount of data operations relative to the underlying driver's system calls), in which case the number of system calls can be greatly reduced by using library functions. This result is due to the buffer technology. In user space and kernel space, buffers are used for file operations. For example, writing files with fwrite first writes the contents to the user space buffer. When the user space buffer is full or the write operation ends, the user buffers the user. The contents of the area are written to the kernel buffer. For the same reason, the contents of the kernel buffer are written to the hardware medium corresponding to the file when the kernel buffer is full or the end of the write.
2,
standard C library function calls the library function such as file manipulation functions fopen, fread, fwrite, fclose, fflush, fseek etc., must contain the header file stdio.h. Take fwrite as an example. The function prototype is size_t fwrite(const void *buffer, size_t size, size_t item_num, FILE *pf). The operation object is the file pointer FILE *pf. To write a file, you must first have write permission. Open a file with the fopen function and get the FILE structure pointer pf of the open file, for example, pf=fopen(\\"~/proj/filename\\", \\"w\\"). In fact, since the operation of the library function on the file is finally realized by the system call, the FILE structure pointer obtained by opening each file has a kernel space file descriptor fd corresponding thereto. There are also corresponding predefined FILE pointers: stdin-standard input, stdout-standard output, stderr-standard error.
Library function calls are typically used for access to general files in an application.
Library function calls are system-independent and therefore portable.
Since the library function calls are based on the C library, it is not possible to operate on the device in the kernel space driver.
※ library calls the VS system call library system call in all ANSI C compiler version, C library functions is the same system calls each operating system was a different library it calls the Program (or function) The service that calls the system kernel is associated with the user program. An entry point of the operating system is executed in the user address space. The running time of executing it in the kernel address space belongs to "user time"; its running time belongs to &ldquo "System" time belongs to the procedure call, the call overhead is small, need to switch between user space and kernel context, the overhead is large. There are about 300 functions in the C function library libc in UNIX. A typical system call 90 C library calls: system fprintf malloc typical system call: chdir fork write brk;