内核堆栈和用户堆栈有什么区别? [英] What is the difference between kernel stack and user stack?

查看:58
本文介绍了内核堆栈和用户堆栈有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同一程序中使用两个不同的堆栈有什么需要? trap如何将当前程序堆栈从用户堆栈更改为内核堆栈?完成系统调用后如何返回到用户堆栈?

What is the need of using two different stacks in same program? How does trap change the current stack of program from user stack to kernel stack? How does it come back to user stack after completing system call?

每个进程都有内核和用户堆栈吗?

Does every process have a kernel and user stack?

推荐答案

每个CPU有一个内核堆栈".每个进程都有一个用户堆栈",尽管每个线程都有自己的堆栈,包括用户线程和内核线程.

There is one "kernel stack" per CPU. There is one "user stack" for each process, though each thread has its own stack, including both user and kernel threads.

陷阱如何改变堆栈"实际上很简单.

How "trapping changes the stack" is actually fairly simple.

由于中断,CPU更改了进程或模式".发生中断的原因有很多-发生错误(例如错误或页面错误),或者发生物理硬件中断(例如来自设备)-或计时器中断(例如,在使用进程时发生)所有这些都分配了CPU时间".

The CPU changes processes or "modes", as a result of an interrupt. The interrupt can occur for many different reasons - a fault occurs, (like an error, or page-fault), or a physical hardware interrupt (like from a device) - or a timer interrupt (which occurs for example when a process has used all of it's allotted CPU time").

无论哪种方式-调用此中断时,CPU寄存器-所有寄存器-包括堆栈指针本身,都保存在堆栈中.

Either way - when this interrupt is called, the CPU registers are saved on the stack - all the registers - including the stack pointer itself.

通常情况下,将调用调度程序".然后,调度程序选择另一个要运行的进程-恢复所有保存的寄存器,包括堆栈指针,并从中断处(存储在返回地址指针中)继续执行.

Typically then a "scheduler" would be called. The scheduler then chooses another process to be run - restoring all of its saved registers including the stack pointer, and continues execution from where it left off (stored in the return-address pointer).

这称为上下文切换".

我正在简化一些事情-例如如何保存和恢复内存管理上下文,但这就是想法.只是为了响应中断而保存和恢复寄存器-包括堆栈指针"寄存器.

I'm simplifying a few things - like how memory management context are saved and restored, but that's the idea. It's just saving and restoring registers in response to an interrupt - including the "stack pointer" register.

这篇关于内核堆栈和用户堆栈有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆