Let's take a look at how C-standard I/O library functions are implemented using system calls.
Fopen (3)
Call open (2) to open the specified file, return a file descriptor (an int type number), and assign
File struct, including the file descriptor, I/O buffer, and the current read/write location.
The address of the file struct.
Fgetc (3)
You can use the input file * parameter to find the file descriptor, I/O buffer, and current read/write location, and determine whether the next character can be read from the I/O buffer area, if this character can be read, it is returned directly. Otherwise, read (2) is called to pass the file descriptor to the kernel to read the data of the file to the I/O buffer, and then the next character is returned. NOTE: For the C standard I/O Library, the opened file is identified by the file * pointer, and for the kernel, The opened file is identified by the file descriptor, the file descriptor is obtained from the open system call. When using the read, write, and close systems call, you must upload the file descriptor.
Fputc (3)
Determine whether the I/O buffer of the file has space to store another character. If there is space, it is directly stored in the I/O buffer and returned, if the I/O buffer is full, write (2) is called and the kernel writes the content of the I/O buffer back to the file.
Fclose (3)
If there is still data in the I/O buffer that is not written back to the file, call write (2) to write back the file, call close (2) to close the file, and release the file structure and I/O buffer.
Take writing files as an example. C standard I/O library functions (printf (3), putchar (3), fputs (3) and system calls write (2) as shown in.
Hierarchical relationship between library functions and system calls
System functions such as open, read, write, and close are called unbuffered I/O functions because they are located at the underlying layer of the I/O buffer of the C standard quasi-library. When a user program is reading and writing files, it can call both the C standard I/O library function and the underlying unbuffered I/O function. Which function group is better?
The unbuffered I/O function is used in the kernel for each read/write operation. Calling a system call is much slower than calling a user space function, therefore, it is necessary to open up the I/O buffer in the user space. It is easier to use the C standard I/O library function, saving the trouble of managing the I/O buffer by yourself.
When using C standard I/O library functions, you must always note that the I/O buffer may be inconsistent with the actual file, and you must call fflush (3) when necessary ).
We know that the tradition of UNIX is everything is a file. I/O functions are not only used to read and write conventional files, but also for read and write setup and backup, such as terminals or network devices. When reading and writing a device, you usually do not want to buffer data. For example, writing data to a file representing a network device means you want the data to be sent out through a network device, instead of writing data to the buffer zone, the application will be notified immediately when the network device receives the data, therefore, the unbuffered I/O function is usually directly called for network programming.
C Standard library functions are part of the C standard, while unbuffered I/O functions are part of the UNIX standard, c Standard library functions should be available on all platforms that support C language (except for C compilers on some platforms that do not fully comply with C standards ), the unbuffered I/O function can be used only on UNIX platforms. Therefore, the C standard I/O library function is in the header file stdio. h declaration, and read, write and other functions in the header file unistd. h. In a non-UNIX operating system that supports C language, the underlying level of the standard I/O library may be supported by another set of system functions. For example, the underlying level of a Windows system is Win32.
Api. The system functions used to read and write files are readfile and writefile.
Reference: <Linux C Programming one-stop learning> (open source books)