"Linux kernel design and implementation" Fourth week reading notes--fifth Chapter 5.1 communication with the kernel 57
The system call adds a middle layer between the user space process and the hardware device, which has a primary role of three:
- First, it provides a hardware abstraction interface for user space, for example, when a file needs to be read and written, the application can do away with the type of disk and media, or even the file system in which the file resides.
- Second, system calls guarantee the stability and security of the system, and as a middleman between hardware devices and applications, the kernel can adjudicate the required access based on permissions, user types, and other rules, for example, to avoid improper use of hardware devices by applications, and to steal resources from other processes. Or to make other things that harm the system.
- Thirdly, as mentioned in the 3rd chapter, each process is run in a virtual system, and in the user space and the rest of the system to provide such a layer of public interface, but also for this reason, if the application can freely access the hardware and the kernel does not know about it, it is almost impossible to achieve multi-tasking and virtual memory, Of course, it is not possible to achieve good stability and security.
In Linux, system calls are user-space access. The only means of the kernel is that they are the only legitimate entry for the kernel except for exceptions and falls.
This chapter focuses on the rules and implementation methods of Linux system invocation.
5.2 API, POSIX, and C library 57
- In general, applications are programmed through application programming interfaces implemented in user space rather than directly through system tuning.
- The programming interface used by the application does not actually need to correspond to the system calls provided by the kernel. An API defines the programming interfaces used by a set of applications, which can be implemented as a system call, or by invoking multiple system calls, without any system calls being used at all.
- In fact, APIs can be implemented in a variety of operating systems, providing the exact same interface to applications, which can be implemented on these systems in different ways.
- The relationships between APIs, POSIX and C libraries, and system calls such as
Figure A
- In the Unix world, the most popular application programming interface is based on the POSIX standard.
- The interface design for UNIX has a motto: to provide a mechanism rather than a strategy.
5.3 System Call 58
- To access system calls, it is usually done through function calls defined in the C library.
- The system call eventually has a definite operation.
- How to define a system call
First, note the Asmlinkage qualifier in the function declaration, which is a compilation instruction that notifies the compiler to extract only the parameters of the function from the stack. This qualifier is required for all system calls.
The next function returns a long. To ensure compatibility between 32-bit and 64-bit systems, system calls have different return value types in user space and kernel space, where the user space is int in kernel space is long.
Finally, note that the system call in Get_pid () is defined in the kernel as Sys_getpid (). This is the naming convention that all system calls in Linux should follow, and the system call Bar () is also implemented as the Sys_bar () function in the kernel.
Experimental process:
1. Library function API uses system call
2. Embed assembly code in C code using system call
3. Experimental results
Fourth week of Linux learning