The relationship and difference between Linux library function and system call

Source: Internet
Author: User

Last week summed up the "C standard library base IO", in fact, these function functions through "system call" can also achieve the corresponding function. This article is not to detail the use of each system call interface, but rather to understand the relationship between "library function" and "system" call and the difference.

First, the system calls

System invocation, we can understand that the operating system provides users with a series of operational interfaces (APIs) that provide operations on the functionality of the system's hardware devices. This may be more abstract, for example, the program we are most familiar with hello world will print information on the screen. The function is called in the program printf() , and the library function printf essentially calls the system call write() function, which realizes the printing function of the terminal information.

Second, the library function

Library functions can be understood as a layer of encapsulation of system calls. System call as the kernel to the user program interface, its execution efficiency is relatively efficient and streamlined, but sometimes we need to obtain more complex processing of information, or more human needs, we put these processing process into a function to provide to the programmer, more convenient for the program ape Code.

Library functions may contain a system call, there may be several system calls, and of course there may be no system calls, such as some operations that do not require kernel-related functionality. You can refer to the relationship between library functions and system calls.

Third, system call meaning
    • The user is prevented from programming the underlying hardware directly. For example, the simplest hello world program is to print information to the terminal, the terminal to the system is the hardware resources, if there is no system call, the user program needs to write their own terminal device driver, as well as control how the terminal display code.
    • Hide the technical details behind. For example, if a system call is used, the user program does not need to care about which track and sector of the disk the data is in, and where the data is to be loaded into memory.
    • Ensure the security and stability of the system. To know that the user program can not directly manipulate the kernel address space, such as a new program ape, let him directly to access the kernel of the underlying data, then the security of the kernel system can not be guaranteed. And the function of the system call is implemented by the kernel, the user only needs to call the interface, need not care about the detail, also avoids the system security hidden trouble.
    • Facilitate the portability of the program. If the function of a system resource such as write (), people follow their own ideas to achieve this function, then we write the program will be very poor transplant.

All in all, we just need to think of the system call as an interface, and this interface can implement one of our functions, which is both convenient and secure.

Iv. Library functions vs system calls

Referring to appendix A.4 in the book "C Expert Programming", the answer to the difference is that the library call is part of the language or application, and the system call is part of the operating system.

    • All C function libraries are the same, and system calls are different for each operating system.
    • A function library call is a program that calls a function library, and a system call is a service that invokes the system kernel.
    • The function library call is associated with the user program, and the system call is an entry point for the operating system
    • The function library call is executed in the user address space, while the system call is performed in the kernel address space
    • The run time of the library call is "user" time, while the run time of the system call belongs to "system" time
    • The function library call is a procedure call, and the overhead is small, and the system call needs to switch to the kernel context and then switch back to a higher overhead
    • About 300 programs in the C library libc, about 90 system calls in UNIX
    • function library Typical C functions: System, fprintf, malloc, and typical systems call: chdir, fork, write, BRK

According to the book, the library function call takes time to be half-subtle, and the system call takes approximately 70 times times (35 microseconds) of the library function call, because the system call has the overhead of kernel context switching. Purely in terms of performance, you should minimize the number of system calls, but you must remember that many of the programs in the C function library implement functionality through system calls.

V. Correct understanding of library functions efficient for system invocation

First of all, the above description of the library function performance is much higher than the premise of the system call, the library function type does not use system calls. Again, there are some library functions that contain system calls, but the performance is actually higher than the system call. For example, in the previous article about file IO functions fread, fwrite, FPUTC, fgetc, and so on, these functions are usually higher performance than system calls, because these library functions use buffers, reduce the number of system calls, and thus appear to be relatively high performance.

Vi. how the system calls are run

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

When a process is running, encountered a read-write file operation, an interrupt will occur, the system will be the current user process some of the register information stored in the kernel stack, and then to deal with the interrupt service program, here is to execute system calls, Linux execution int $0x80 to perform the system call interrupt, But the kernel implements a lot of system calls, and then it needs to pass "system call number" to indicate which system call is required.

To better illustrate the process of system invocation, here we refer to a section of code on the Web to implement system calls:

intMain () {time_t TT;structTM *t;ASM volatile("mov $,%%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 &&./atime:2018-05-06 Geneva: at: $

First, the system call mov $0xd %%eax is put into the %eax Register, time () system call number is 13, then the execution int $0x80 system will go to execute the system call. In fact, the assembly part of the code is to implement the function of the time () system call, the assembly code does not know it is ok (I do not understand), here is mainly to clarify the system call the entire process.

Welcome to follow the public number: "linuxblogs"

The relationship and difference between Linux library function and system call

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.