To interact with processes running on the user space, the kernel provides a set of interfaces. Through this interface, applications can access hardware devices and other operating system resources. This set of interfaces acts as a messenger between the application and the kernel, and the application sends various requests, while the kernel is responsible for satisfying those requests. The system call adds a middle tier between the user space and the hardware device. The main role of this layer is three:
- System call provides a kind of hardware abstraction interface for user space;
- System calls to ensure the stability and security of the system;
- System calls are the only means of user space access to the kernel.
API, POSIX, C library
The application is programmed through the API rather than directly through the system call, because 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. They can be implemented as a system call, or through multiple system calls, without any system calls or problems at all. In fact, APIs can be implemented on a variety of operating systems, providing the exact same interface to the application, and they may have different implementations on those systems. The most popular API in Linux is based on the POSIX standard.
Linux system calls, like most Unix systems, are provided as part of the C library. The C library implements the main API for UNIX systems, including standard C library functions and system calls.
System calls
The system call is usually called through a function, the function returns 0, the execution succeeds, if the call error occurs, the error code will be written to the errno global variable, by calling the Perror () library function, you can translate the variable into the user can understand the error string.
In Linux, each system call is given a system call number. When a user-space process executes a system call, the system call number is used to indicate which system call is being executed. The system call number is critical, and once the assignment can no longer be changed, the compiled application crashes. In addition, if a system call is deleted, the system call number it occupies is not allowed to be recycled, otherwise the previously compiled code calls the system call, but in fact it calls another system call.
Linux system calls are performed faster than many other operating systems. Linux's incredible down-and-down file switching time is an important reason, and access to the kernel is optimized for simplicity and efficiency. Another reason is that the system call handlers and each system call itself are also very concise.
System Call handlers
User-space programs cannot directly execute kernel code, they cannot directly invoke functions in kernel space because the kernel resides on a protected address space. If a process can read and write directly to the kernel's address space, system security will be out of control.
Therefore, the application should notify the system in some way, telling the kernel that it needs to perform a system call, and that it wants the system to switch to the kernel state so that the kernel can execute the system call on behalf of the application.
The mechanism for notifying the kernel is implemented by soft interrupts: by throwing an exception that causes the system to switch to the kernel state to execute the exception handler. The exception handling at this point is actually the system call handler. Most system calls require some external parameter input in addition to the system call number. Therefore, when an exception occurs, these parameters should be passed from user space to the kernel.
Implementation of system calls
The first step in implementing a system call is to determine its purpose. Each system call should have a clear purpose. Multi-purpose system calls are not advocated in Linux. Then, consider the parameters of the new system call, the return value, and what the error code is. The new system call must check that all of their parameters are valid.
Linux kernel--system call