Library functions and system calls, library function system calls
System calls can be interpreted as some interfaces provided by the operating system for users. These interfaces provide operations on the hardware functions of the system. In this case, there may be some abstraction. I will give another more specific example: for example, if I want to write a program, the function of this program is to display a string "hello, world!" on the screen! ". The system calls the write () function to display a string on the screen.
So what is the significance of system calling? You can think about it. You still need to print the string code on the screen by yourself when writing a program. This is too tiring ,, therefore, system calling frees us from the underlying hardware programming. Furthermore, the system calls the kernel code, which can access all the address spaces on the system, and the code we run is the user space code, user space code is restricted when operating the system. (as a rookie programmer, if the system does not restrict the code you write ,, in case the system gets stuck ..). Therefore, another function called by the system is to maintain the security of the system. You can call this interface directly if you want to use it. You do not need to write it yourself. Another function of system calling is to facilitate program portability .. In short, you can call the system as an interface. When you need to use it, you can call it easily and securely .. You may be wondering, isn't the printf () function used to print a string when we write C language? What is the difference between this printf () and the system calling write () mentioned above? Yes. In fact, you can use library functions as another encapsulation of system calls .. What does it mean? System calling is the interface provided to us by the kernel, and its execution efficiency is more efficient and streamlined. But sometimes we need to perform more complex processing on the obtained information. In this case, if we wrap these processing processes into a function and then provide it to programmers, isn't it more convenient to program? Therefore, a library function may contain one system call, and there may be several system calls. Of course, there may be no system calls. For example, some operations do not need to involve kernel functions. In short, library functions are application programming interfaces for programmers. Looking at the figure below, we may have a better understanding of the relationship between them: After talking about the relationship between library functions and system calls, let's take a look at how the system calls actually run. When a process is running and a read/write file or something occurs, an interruption occurs, the system will save some register information of the current user process in the kernel stack (for future recovery), and then execute the interrupt service program. Here we will execute the system call, in Linux, the system call is interrupted by executing int $0x80, but the kernel implements many system calls. Therefore, the process must specify which system call is required. At this time, a system call number must be passed. This system call number is stored in the % eax register. Here is an example to illustrate: we use this program to display the current time. First, we implement it through the library function: The result is as follows: the current time is obtained successfully. Next, we embed the assembly language to implement system calling: No? First, the system call number is put into the % eax register through mov $ 0xd % eax. After checking, we find that the system call number of time () is 13. At this time, the system calls time () by executing int $0x80. View results: You can still get the system time! Many of the knowledge involved here may not be well understood. It doesn't matter. We will introduce it in detail later! The purpose of this blog is to let everyone understand the differences between library functions and system calls, as well as the general execution methods of system calls.