System calls and library functions

: Under Linux, there are two ways to file operating system call (system call) and library calls (Library Functions) . In the book "APUE" the vast majority of functions are talking about a system call, rather than the library functions.

1, the system calls

1.1 concept

  • In the computer, the system call (system call), also known as system calls, which you run in user space program to the operating system kernel service requests require higher privileges to run.
  • System call provides the interface between the user program and the operating system. (Ie, the system calls the user program is the interface to interact with the kernel)
  • 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.
  • Sub-state of the operating system supervisor mode (core mode) and a mesh state (user state)
    Most systems demand interactive operation performed in kernel mode. For example, the inter-device I / O operation or process communication.
    Special instructions: one can only be special instructions not run in user mode, special instructions between different operating systems have in kernel mode differences, but some general instructions and hardware related primarily.
    User programs can only run in user mode, the system sometimes need access to core functionality, then use the system call interface calls through the system.
    Applications sometimes take some dangerous, highly privileged instruction, if these privileges safely to the user program is very dangerous. (For example, a process may modify the memory area of another process, causing it to not function properly), but it can not completely do not give these permissions. So there will be system calls, instructions are packaged into a dangerous system call, the user can not only call themselves run the risk of those instructions.
    In addition, computer hardware resources are limited, in order to better manage these resources, control of all resources by the operating system, the process can only request these resources to the operating system.
    The operating system is the only entrance to these resources, this entry is a system call.

Here Insert Picture Description

1.2 System Call Example

Can give an example, we are most familiar hello world program will print out the information on the screen, the program calls printf () function, which is a library function printf output information will be printed to the screen this hardware device. We know that, for the operation of all hardware devices need drivers, and drivers are implemented by the operating system kernel. This means that our printf () function will eventually need to call the Linux kernel correlation function to operate the device screen, which is a function of system calls. In fact, printf () library function's realization will eventually call the system call write ().

In addition, create a process fork (), vfork (),
open, read, sbrk, fork is a system call.

2, libraries

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. In the following example, the learning process, we will learn read (int fd, char * buf , int size) system call, this function is from a file (by the fd) flag, read no more than size characters section and place the data in buf to go, in this system, we can only call N bytes of data specified read, but who want to read such a demand for a line you can call a library function fgets () to achieve, which in "C Primer Plus" document / IO in mentioned, of course, all functions are library functions in "C Primer Plus" was mentioned.
For example: A typical C library printf, fopen, fread, malloc

3, the relationship between the two

Library functions are likely to contain a system call, it is possible to have several system calls, for without involving the kernel function library functions do not need to call a system call, such as strcpy (), strstr (), strlen (), etc. These functions . For a feature, sometimes we can either use the system call to achieve, you can also use the library functions to realize there is no mandatory requirement, be selected according to the actual needs of everyone. For example, for the operation of the file, we can use the open (), read (), write (), close () system calls to achieve, of course, we can also use fopen (), fread (), fwrite (), fgets (), fclose () library functions to achieve only compare to understand their respective characteristics we make a best choice for different situations.

Library calls System calls
In all ANSI C compiler, C library functions are the same System calls each operating system are different, which causes the program to non-portable
It calls the library in a program (or function) It calls the kernel services
Associated with the user program Executed in the kernel address space
Its running time are "user time" It belongs to the run-time "system" time
Belong procedure calls, call overhead is small Required between user controls and kernel context switching, larger overhead
In the C library libc Approximately 300 functions In UNIX, there are about 90 system calls
Published 17 original articles · won praise 28 · views 3846

Guess you like

Origin blog.csdn.net/weixin_46027505/article/details/104756929