1
Introduction to Linux Library Functions
Linux provides a rich set of library functions that cover various domains, from file operations to network programming, graphical interfaces, mathematical operations, and more. Most of these library functions are standard C library functions, along with some that are specific to the Linux system.
Linux library functions are typically provided in the form of dynamic library files (.so) and are stored in the /lib directory of the root filesystem.
Linux library functions are built on top of system calls, although some library functions do not invoke system calls. For example, some string handling functions (strlen(), strcat(), memcpy(), memset(), strchr(), etc.) do not make system calls.
However, other library functions may use system calls to perform actual operations. For example, fopen internally calls the system call open() to open a file, fread() utilizes the system call read() to read a file, and fwrite() uses the system call write() to write a file.
The Linux kernel provides a series of system calls for application layer use. Directly using system calls is one method. However, some system calls are not convenient to use, which is why the C language library was introduced. These library functions are designed to provide a more convenient, user-friendly, and portable interface compared to the low-level system calls. The differences between them are as follows:
-
Library functions belong to the application layer, while system calls are programming interfaces provided by the kernel to the application layer, which are part of the system kernel.
-
Library functions run in user space, while calling a system call causes a transition from user space (user mode) to kernel space (kernel mode).
-
Library functions usually have caching, while system calls are uncached; therefore, in terms of performance and efficiency, library functions typically outperform system calls.
-
In terms of portability, library functions have better portability compared to system calls. Since the definitions, functionalities, parameter lists, and return values of system calls often differ across different operating systems, the interface definitions of C language library functions are almost the same across different operating systems, providing better portability.
Although from the implementer’s perspective, there are fundamental differences between system calls and library functions, these differences are not significant from the user’s perspective. For application programmers, they are both C language functions. In actual application programming, both library functions and system calls will be used. Therefore, from the user’s perspective, they can be viewed as C functions without overly distinguishing the differences between them.
2
Standard C Library Functions
In the Linux system, the C language library primarily used is the GNU C Library, also known as glibc. It is the standard C library under Linux, providing developers with a rich set of functions covering input/output, string processing, mathematical operations, file operations, and more.
Website: https://sourceware.org/glibc/
Obtaining the glibc source code is very simple; it can be downloaded directly from the git repository or via FTP:
