C language--the difference between system function and library function

Source: Internet
Author: User
Tags garbage collection local time time and date
system call:is a set of interfaces that the operating system interacts with for user-state processes and hardware devices (such as CPUs, disks, printers, and so on), that is, an interface layer that is set between applications and hardware devices. It can be said that the operating system to leave the User a program interface. Again, the Linux kernel is a single core, compact structure, fast execution, each module is a direct call between the relationship. Look at the entire Linux system, from top to bottom in turn is the user process->linux kernel-> hardware. Where the system call interface is located in the Linux kernel, if a little subdivision, the entire Linux system from top to bottom can be: User process-> system call Interface->linux kernel subsystem-> hardware, That is, the Linux kernel includes both the system call interface and the kernel subsystem, or it can be: physical hardware->os kernel->os service-> application, in which the operating system plays a key role in "Connecting Link", Managing the physical hardware down, Provides an interface to the operating system services and applications, where the interface is the system call.
Generally, the operating system, in order to consider the difficulty of implementation and management convenience, it provides only a small number of system calls, these system calls are generally composed of C and compilation of mixed implementation of the interface with C to define, and the specific implementation of the assembly, the advantage is that the implementation of high efficiency, and greatly facilitate the upper call.

Library functions:As the name implies, the function is placed in the library. is to put some commonly used functions into a file for others to use. When others use the file name it is in #include<> added to the inside on it. Usually put in Lib file. Typically refers to a function that the compiler provides that can be invoked in a C source program. Can be divided into two categories, a class is the C language standard library functions, a class of compiler-specific library functions. (due to copyright reasons, the library function source code is generally not visible, but in the header file you can see its external interface)
LIBC is a C standard library that contains basic functions, which are all standardized, and these functions are usually implemented directly with the assembler.
Library functions can generally be summed up in two categories, one is provided by the operating system, and the other is provided by third parties. As the system provides these library functions to encapsulate or combine system calls, more functions can be implemented, such library functions can achieve some of the kernel complex operations. For example, the read () function can read the file directly according to the parameters, but hidden behind such as the file on the hard disk of which track, which sector, where the load into memory and so on these operations, programmers are not concerned about, these operations naturally also contains the system call. And for a third party library, it is the same as the system library, but it is directly using the system calls less likely, but use the system provided API interface to implement the function (API interface is open). The implementation of functions in some libc libraries is based on system calls, such as the implementation of printf, which ultimately invokes system calls such as write, while others do not use system calls, such as strlen, Strcat, memcpy, and so on.

In real time, the system calls to the user is direct and pure advanced services, if you want to be more humane, with a more specific function of the situation, then we have to define the user, so we have derived a library function, it will be part of the system call packaging, on the one hand, the system call abstraction, on the one hand, convenient user-level calls. system calls and library functions are very similar in effect (of course the library functions are more in demand), but the system calls run in the kernel state, and the library functions are invoked by the user and run in User state.

System calls are designed to facilitate the use of the operating system's interfaces, while library functions are designed to facilitate people's programming.

From the perspective of the performer, there is a significant difference between the system call and the library function, but the difference from the user's point of view is not very important. Both serve the application, but we should understand that if we want to, we can take the library function, but usually we can't replace the system service.

Take the memory allocation function malloc as an example. There are many ways to do memory allocation and its associated garbage collection operations (best fit, first adaptation, etc.), and there is no one technology that is best for all programs. The processing of memory allocations in UNIX system calls is SBRK (2), which is not a general-purpose memory manager. It increases or decreases the process address space for the specified number of bytes. How the address space is managed depends on the process. The memory allocation function malloc (3) implements a specific type of assignment. If we do not like the way it operates, we can define our own malloc function, and it is extremely likely that it will still invoke the SBRK system call. In fact, there are many packages that implement their own memory allocation algorithms, but still use SBRK system calls. From this, the two responsibilities are different, separate, the system calls in the kernel allocate another space to the process, and the library function malloc manage this space.

Another example of the difference between system calls and library functions is that UNIX provides an interface that determines the current time and date. Some operating systems provide one system call to return the time, while the other returns the date. Any special treatment, such as the conversion between normal time and daylight saving time, is determined by the system nuclear process or by requiring a person to do so. UNIX is different, it provides only one system call, the system call returns the international standard 197  year of January 1, so the number of seconds elapsed. Any interpretation of the value, such as converting it to people-readable, using the time and date of the local time zone, is left to the user process. In the standard C library, a number of routines are provided to handle most situations. These library functions deal with a variety of details, such as various daylight saving time algorithms.

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.