linux - 为什么系统调用比普通的函数调用更耗时?用户态和内核态切换的代价在哪?
巴扎黑
巴扎黑 2017-04-17 11:43:55
0
2
845

为什么系统调用比普通函数调用耗时?如果是因为需要用户态和内核态的切换,那么这个切换具体做了什么,哪些步骤代价大呢?

巴扎黑
巴扎黑

reply all(2)
伊谢尔伦

When there is a system call statement in the program and the program executes the system call, first use a soft interrupt instruction similar to int 80H to save the scene and the system call number, execute it in the kernel mode, and then restore the scene, each process There will always be two stacks, a kernel-mode stack and a user-mode stack. When an int interrupt is executed, the stack will be transferred from the user state to the kernel stack. Stack switching is required during system calls. Moreover, the kernel code does not trust users and requires additional checks. The return process of the system call has a lot of extra work, such as checking whether scheduling is needed, etc.

阿神

System calls generally need to save the context of the user program. When entering the kernel, the user mode registers need to be saved. The contents of these registers will be restored when the kernel mode returns to the user mode. This is a place of overhead. If you need to switch between different user programs, you will also need to update the cr3 register, which will change the address of each program's virtual memory to the physical memory mapping table, which is also a relatively high-burden operation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!