What is system call in linux

WBOY
Release: 2022-03-21 14:15:30
Original
6017 people have browsed it

In Linux, system call refers to a set of special interfaces provided by the operating system for user programs to call. User programs can obtain services from the operating system kernel based on this set of interfaces; system calls specify the time when a user process is trapped in the kernel. The specific location, or the planned path for users to access the kernel, can only be entered from a fixed location.

What is system call in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is a system call in Linux

1. What is a system call

System call refers to the call provided by the operating system to user programs A set of special interfaces through which user programs can obtain services from the operating system kernel. It stipulates the specific location where the user process falls into the kernel, or plans the path for the user to access the kernel, and can only enter the kernel from a fixed location.

2. Linux system calls

For modern operating systems, system calls are a common means of communication between user space and the kernel, and Linux is no exception. According to functional areas, Linux system calls are roughly divided into process control, file access, system control, storage management, network management, process communication, etc. For detailed description, you can view the manpage description through the man 2 syscalls command.

System call only submits a request to the kernel through the soft interrupt mechanism and enters the corresponding service of the system call. The user programming interface provided by Linux follows the POSIX standard. In addition to defining some standard C functions, this set of standards also provides a set of encapsulation routines to encapsulate system calls for user programming. However, encapsulation is not necessary. If you are willing to call directly, the Linux kernel also provides a syscall() function to implement the call. Learn the difference between C library calls and direct calls through the following example.

/*
**  file: demo.c
**  author: eric.xu
**  date:   2016-02-25
*/
#include <syscall.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main(void)
{
    long id1, id2;
    
    /* system call no 20, __NR_getpid equal to SYS_getpid  */
    id1 = syscall(__NR_getpid);
    printf("sys_call getpid %ldn", id1);
    /* libc getpid */
    id2 = getpid();
    printf("libc getpid %ldn", id2);
    return 0;
}
Copy after login

Compile and run, you can see that the results of the two are consistent.

sys_call getpid 2899
libc getpid 2899
Copy after login

3. Linux system call implementation

When a user-mode process calls a system call, the CPU switches to kernel mode and starts executing kernel functions. Because each system call in the kernel has a unique label, user-mode calls must pass a system call number parameter to determine the specific system call function. All system call functions are integers. In the kernel, integers and 0 indicate that the system call ends successfully, and negative numbers indicate error conditions. This error value will be stored in the errno variable and returned to the application as an error code.

Linux system calls are implemented using soft interrupts. In the x86 architecture, the int $0x80 assembly instruction is called. This instruction will generate an exception with a vector of 128. In the arm architecture, the SWI instruction is used to enter the kernel space. Let's look at the format of this instruction:

SWI {cond} immed24;其中immed24表示24位立即数
Copy after login

The SWI exception interrupt handler needs to obtain the 24-bit immediate number by reading the SWI instruction that caused the software interrupt. The basic steps are to access the SPSR register to determine whether the instruction is an ARM instruction or a Thumb instruction after the SWI exception is generated, and then obtain the entire instruction address by accessing the LR register, and then obtain the instruction and obtain the lowest 24-bit immediate data.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is system call in linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template