Relations and differences relationship with the difference (rpm) Linux library functions and system calls Linux system calls and library functions

Relations with the difference between Linux library functions and system calls

Last week, she summed up the "basic IO C standard library", in fact, can achieve the corresponding functions of these functions, "calling the system" by. This article is not intended to detail usage of each system call interface, but to in-depth understanding of the relationship and differences between the "library functions" and "system" calls.

First, the system calls

System calls, we can understand that the operating system provides users with a series of interface operations (API), which provides an operating system interface to the hardware device capabilities. That might be more abstract, for example, we are most familiar with the  hello world program will print out the information on the screen. When calling the  printf() function, and the function library  printf is essentially a call to the system call  write() function to achieve a printing function terminal information.

Second, the library functions

Library functions can be understood as a layer system call package. The system call interface as provided by the kernel to the user program, its efficiency is more efficient and streamlined, but sometimes we need access to information is more complex processing, or more humane needs, we put these processes packaged into a programmer and supplied to the function, the program easier ape coding.

Library functions are likely to contain a system call, it is possible to have several system calls, of course, there may be no system calls, for example, some operations do not involve core functions. You can refer to the following figure to understand the relationship between library functions and system calls.
Library functions and system calls

Third, the system calls the meaning

  • To avoid the user to program directly on the underlying hardware. For example, the simplest hello worldprocedure is to print the information to the terminal, the terminal is the system hardware resources, if not a system call, the user program requires drivers to write their own terminal equipment, as well as control how the code displayed on the terminal.
  • Hide behind the technical details. Such as read and write files, if you use a system call, the user program without concern for data in which track and sector of the disk, and the data to be loaded into memory what position.
  • Ensure the security and stability of the system. To know the user program can not directly manipulate kernel address space, such as a fledgling program ape, let him direct access to the underlying data kernel, then the kernel security system can not be guaranteed. The system calls function is implemented by the kernel, users need only call interface, without having to care about the details, but also to avoid the security risks the system.
  • Facilitate the portability of the program. If the function of the operating system for a resource such as write (), we all follow their own ideas to achieve this, then we write out the transplant procedure will be very poor.

All in all, we just need as a system call interface, this interface can achieve our function, convenient and secure.

Fourth, the library functions vs system calls

Appendix reference books in the "C Programming experts" A.4, to answer questions about the book is such a difference between the two, the library calls are part of the language or application, and system calls are part of the operating system.

  • All C libraries are the same, and the system calls each operating system is different.
  • Library call is a program that calls the function library, and kernel system call is the call of service.
  • Library calls are linked to the user program, and the system call is the operating system of a point of entry
  • Library calls are executed in the user address space, and system calls are executed in the kernel address space
  • Run-time library calls belonging to "User" time and run-time system calls belong to the "System" time
  • Library calls belonging procedure call overhead is small, and the need to switch to a system call kernel context and then switch back, large overhead
  • In the C library libc about 300 programs in UNIX there are about 90 system calls
  • Typical C library functions: system, fprintf, malloc, and typical system calls: chdir, fork, write, brk

According to the book records, library calls takes around half subtle time, the system call and the time required is approximately 70 times (35 sec) library function calls, because the overhead of context switching system kernel will call the. From purely on performance, you should reduce the number of system calls as much as possible, but you must remember that many C function library program to implement functions through system calls.

Fifth, the correct understanding of the system call library functions efficiently

First explain, library functions described above performance is much higher than the premise of the system call that kind of library functions without the use of system calls. Again explain some library functions include system calls, but it does have higher performance system call. For example, the last article on the file IO functions fread, fwrite, fputc, fgetc, etc., under normal circumstances the performance of these functions is indeed higher than the system call, because these library functions use a buffer to reduce the number of system calls, which seems Performance relatively high.

Sixth, how to run the system call is

Above the basic concept of clear library functions and system calls, and the relationships between them, let's understand the system call in the end is how it works.

When a process is running, met to read and write file operations, an interrupt occurs, the system will register some of the current user process information stored in the kernel stack after interrupt, the interrupt service routine and then to deal with here is going to execute the system call , Linux by performing  int $0x80 to execute the interrupt system calls, but the kernel implements a number of system calls, then you need to pass "system calls" need to indicate which system call.

In order to more clearly illustrate the process of system calls, we here refer to the online piece of code to implement a 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

 

Firstly  mov $0xd %%eax to call into the system  %eax register, time () system call number is 13, then perform  int $0x80 the system will perform to time () system call. In fact, part of the code is compiled to realize time () system call functionality, assembly code do not understand it does not matter (I do not understand), here primarily to make it clear that the whole process of system calls.

Last week, she summed up the "basic IO C standard library", in fact, can achieve the corresponding functions of these functions, "calling the system" by. This article is not intended to detail usage of each system call interface, but to in-depth understanding of the relationship and differences between the "library functions" and "system" calls.

First, the system calls

System calls, we can understand that the operating system provides users with a series of interface operations (API), which provides an operating system interface to the hardware device capabilities. That might be more abstract, for example, we are most familiar with the  hello world program will print out the information on the screen. When calling the  printf() function, and the function library  printf is essentially a call to the system call  write() function to achieve a printing function terminal information.

Second, the library functions

Library functions can be understood as a layer system call package. The system call interface as provided by the kernel to the user program, its efficiency is more efficient and streamlined, but sometimes we need access to information is more complex processing, or more humane needs, we put these processes packaged into a programmer and supplied to the function, the program easier ape coding.

Library functions are likely to contain a system call, it is possible to have several system calls, of course, there may be no system calls, for example, some operations do not involve core functions. You can refer to the following figure to understand the relationship between library functions and system calls.
Library functions and system calls

Third, the system calls the meaning

  • To avoid the user to program directly on the underlying hardware. For example, the simplest hello worldprocedure is to print the information to the terminal, the terminal is the system hardware resources, if not a system call, the user program requires drivers to write their own terminal equipment, as well as control how the code displayed on the terminal.
  • Hide behind the technical details. Such as read and write files, if you use a system call, the user program without concern for data in which track and sector of the disk, and the data to be loaded into memory what position.
  • Ensure the security and stability of the system. To know the user program can not directly manipulate kernel address space, such as a fledgling program ape, let him direct access to the underlying data kernel, then the kernel security system can not be guaranteed. The system calls function is implemented by the kernel, users need only call interface, without having to care about the details, but also to avoid the security risks the system.
  • Facilitate the portability of the program. If the function of the operating system for a resource such as write (), we all follow their own ideas to achieve this, then we write out the transplant procedure will be very poor.

All in all, we just need as a system call interface, this interface can achieve our function, convenient and secure.

Fourth, the library functions vs system calls

Appendix reference books in the "C Programming experts" A.4, to answer questions about the book is such a difference between the two, the library calls are part of the language or application, and system calls are part of the operating system.

  • All C libraries are the same, and the system calls each operating system is different.
  • Library call is a program that calls the function library, and kernel system call is the call of service.
  • Library calls are linked to the user program, and the system call is the operating system of a point of entry
  • Library calls are executed in the user address space, and system calls are executed in the kernel address space
  • 函数库调用的运行时间属于「用户」时间,而系统调用的运行时间属于「系统」时间
  • 函数库调用属于过程调用,开销较小,而系统调用需要切换到内核上下文环境然后切换回来,开销较大
  • 在C函数库libc中大约 300 个程序,在 UNIX 中大约有 90 个系统调用
  • 函数库典型的 C 函数:system, fprintf, malloc,而典型的系统调用:chdir, fork, write, brk

据书中记载,库函数调用大概花费时间为半微妙,而系统调用所需要的时间大约是库函数调用的 70 倍(35微秒),因为系统调用会有内核上下文切换的开销。纯粹从性能上考虑,你应该尽可能地减少系统调用的数量,但是,你必须记住许多 C 函数库中的程序通过系统调用来实现功能。

五、正确理解库函数高效于系统调用

首先解释,上述说明的库函数性能远高于系统调用的前提是,库函数种没有使用系统调用。再来解释下某些包含系统调用的库函数,然而其性能确实也要高于系统调用。比如上篇文章中关于文件 IO 函数 fread、fwrite、fputc、fgetc 等,这些函数通常情况下性能确实比系统调用高,原因在于这些库函数使用了缓冲区,减少了系统调用的次数,因而显得性能比较高。

六、系统调用是如何运行的

上述内容基本说清楚了库函数与系统调用的概念以及它们之间的关系,下面我们来理解系统调用到底是如何运行的。

当一个进程正在运行,遇到读写文件操作,会发生一个中断,中断后系统会把当前用户进程的一些寄存器信息保存在内核堆栈中,接着去处理中断服务程序,这里是要去执行系统调用,Linux 中通过执行 int $0x80 来执行系统调用的中断,但内核实现了很多系统调用,这时需要传递「系统调用号」来指明需要哪个系统调用。

为了更清楚的说明系统调用的过程,我们这里参考网上的一段代码来实现系统调用:

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

 

Firstly  mov $0xd %%eax to call into the system  %eax register, time () system call number is 13, then perform  int $0x80 the system will perform to time () system call. In fact, part of the code is compiled to realize time () system call functionality, assembly code do not understand it does not matter (I do not understand), here primarily to make it clear that the whole process of system calls.

Guess you like

Origin www.cnblogs.com/ptfe/p/10969121.html