gpt4 book ai didi

multithreading - linux如何管理线程栈

转载 作者:行者123 更新时间:2023-12-02 04:40:23 26 4
gpt4 key购买 nike

在英特尔 x86 32/64 位架构上,每个线程都拥有自己的堆栈。在同一进程的线程之间进行内核切换的地方,当前线程的堆栈和寄存器在某处备份。我想它在堆栈中?

这是否意味着如果我的进程中有 N 个线程,进程内存中将有 N+1 个堆栈?还是由内核管理?如果知道另一个线程的 esp/bsp,我可以线程读取另一个线程的堆栈吗?(我说的是同一进程的线程)

最佳答案

Where the kernel switch between threads of the same process, stack and registers of current threads are backuped somewhere. I suppose it is in a stack ?

是的,没错。通常,重新安排是由 timer interrupt 触发的。与任何其他硬件中断[1] 一样,计时器中断 请求 CPU 停止处理当前正在执行的操作并执行一些特殊代码,中断服务例程。内核可以自己设置这个,所以它只是内核提供的一个函数。通常,内核在中断发生时做的第一件事是将所有寄存器压入堆栈。内核唯一需要保存的是栈顶,esp。当内核现在切换到另一个线程时,它所要做的就是加载新线程 esp 并弹出所有寄存器。

Does that mean if i have N thread on my process, i will have N+1 stacks in the process memory ?

您很可能在内存中有 N 个堆栈,在内核内存中有 N 个堆栈,作为中断,调用内核代码,加载另一个堆栈。通常,每个线程都有自己的内核堆栈,但不要引用我的话。

Can i thread read the stack of another thread if it knows esp/bsp of that thread ? (i am talking about threads of the same process)

是的,你可以。同一进程中的线程共享相同的虚拟地址空间,因此如果您知道线程堆栈的确切位置,您就可以访问该内存。


[1]:Programmable Interrupt Controller 触发硬件中断当发生某些事情时从 CPU 的“外部”,例如键盘上的按键。还有软件中断,由 cpu/软件本身触发,有意或用于处理错误(被零除)。软件中断是实现系统调用的一种方式,因为你不能从 ring-3 代码调用 ring-0 代码。然而,如果 TSS 中断可以“向上”跳转到 ring-0设置为这样做。

关于multithreading - linux如何管理线程栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com