The relationship and difference between Linux library functions and system calls

Last week, I summarized the "Basic IO of the C Standard Library". In fact, these functional functions can also achieve corresponding functions through "system calls". This article is not to introduce the usage of each system call interface in detail, but to deeply understand the relationship and difference between "library function" and "system" call.

1. System call

System calls can be understood as the interfaces (APIs) of a series of operations provided by the operating system to users, and these interfaces provide operations on the functions of the system hardware devices. It may be abstract to say that, for example, the hello worldprograms print information on the screen. The program calls the printf()function , and the library function printfessentially calls the system call write()function to realize the printing function of terminal information.

Second, the library function

Library functions can be understood as a layer of encapsulation of system calls. As the interface provided by the kernel to the user program, the system call is more efficient and streamlined, but sometimes we need to perform more complex processing on the acquired information, or for more user-friendly needs, we encapsulate these processing processes as A function is then provided to the programmer, which is more convenient for programmers to code.

The library function may contain a system call, there may be several system calls, and of course there may be no system calls. For example, some operations do not need to involve the functions of the kernel. You can refer to the following figure to understand the relationship between library functions and system calls.
Library functions and system calls

3. The meaning of system call

  • The user is prevented from programming the underlying hardware directly. For example, the simplest hello worldprogram is to print information to the terminal. The terminal is a hardware resource to the system. If there is no system call, the user program needs to write the driver of the terminal device and the code to control how the terminal displays.
  • Hide the technical details behind it. For example, to read and write files, if a system call is used, the user program does not need to care which track and sector of the disk the data is in, and where the data is to be loaded into the memory.
  • Ensure the security and stability of the system. You must know that user programs cannot directly manipulate the kernel address space. For example, if a programmer who has just debuted directly accesses the underlying data of the kernel, the security of the kernel system cannot be guaranteed. The function of the system call is implemented by the kernel, and the user only needs to call the interface without paying attention to the details, which also avoids the security risks of the system.
  • To facilitate program portability. If we implement a function operation on a system resource, such as write(), according to our own ideas, the portability of the program we write will be very poor.

All in all, we only need to treat the system call as an interface, and this interface can implement one of our functions, which is convenient and safe.

Fourth, library function vs system call

Referring to the appendix A.4 in the "C Expert Programming" book, the answer to the difference between the two is this, library calls are part of the language or application, and system calls are part of the operating system.

  • All C function libraries are the same, and the system calls of each operating system are different.
  • The function library call is to call a program in the function library, and the system call is to call the service of the system kernel.
  • The function library call is associated with the user program, and the system call is an entry point of the operating system
  • Function library calls are executed in user address space, while system calls are executed in kernel address space
  • The runtime of library calls belongs to "user" time, while the runtime of system calls belongs to "system" time
  • The function library call belongs to the procedure call, the overhead is small, and the system call needs to switch to the kernel context and then switch back, the overhead is large
  • There are about 300 programs in the C library libc and about 90 system calls in UNIX
  • Function library typical C functions: system, fprintf, malloc, and typical system calls: chdir, fork, write, brk

According to the book, a library function call takes about half a microsecond, and a system call takes about 70 times as long (35 microseconds) as a library function call, because the system call has the overhead of a kernel context switch. Purely for performance reasons, you should try to reduce the number of system calls as much as possible, however, you must remember that many programs in the C library use system calls to implement their functionality.

5. Correctly understand that library functions are efficient in system calls

First of all, the premise that the performance of the library functions described above is much higher than the system calls is that the library functions do not use system calls. Let's explain some library functions that contain system calls, but their performance is indeed higher than system calls. For example, the file IO functions fread, fwrite, fputc, fgetc, etc. in the previous article, these functions are usually higher in performance than system calls. The reason is that these library functions use buffers, reducing the number of system calls. relatively high.

6. How system calls work

The above content basically explains the concept of library functions and system calls and the relationship between them. Let's understand how system calls work.

When a process is running and encounters a read and write file operation, an interrupt will occur. After the interrupt, the system will save some register information of the current user process in the kernel stack, and then process the interrupt service routine. Here is to execute the system call , In Linux, the interrupt of the system call is executed int $0x80by , but the kernel implements many system calls. At this time, the "system call number" needs to be passed to indicate which system call is required.

In order to explain the process of the system call more clearly, we refer to a piece of code on the Internet to implement the system call:

int main()
{
    time_t tt; 
    struct tm *t; 
    asm volatile (
        "mov $0,%%ebx\n\t"
        "mov $0xd,%%eax\n\t"
        "int $0x80\n\t"
        "mov %%eax,%0\n\t"
        : "=m" (tt)
    );  
    t = localtime(&tt);
    printf("Time: %d-%02d-%02d %02d:%02d:%02d\n",
           t->tm_year + 1900,
           t->tm_mon + 1, t->tm_mday,
           t->tm_hour, t->tm_min, t->tm_sec);
}

[linuxblogs@host ~]$ gcc a.c -oa && ./a
Time: 2018-05-06 03:23:46

mov $0xd %%eaxFirst put the system call into %eaxthe register through , the system call number of time() is 13, and then the execution int $0x80system will execute the time() system call. In fact, the assembly part of the code is to realize the function of the time() system call. It doesn't matter if you don't understand the assembly code (I don't understand it very well), the main purpose here is to clarify the whole process of the system call.

Welcome to the public account: "linuxblogs"

Wechat QR code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325378800&siteId=291194637
Recommended