Linux kernel 克隆系统调用中的子堆栈解释

Linux kernel 克隆系统调用中的子堆栈解释,linux-kernel,clone,system-calls,Linux Kernel,Clone,System Calls,在克隆(2)手册页中,对于子堆栈,它提到 Since the child and calling process may share memory, it is not possible for the child process to execute in the same stack as the calling process. 有谁能解释一下“共享内存”是如何让它变得不可能的。OTOH,一个常见的看法是线程中的函数执行序列将不同于其他线程,因此我们需要另一个堆栈 谢谢, Kapil两

在克隆(2)手册页中,对于子堆栈,它提到

Since the child and calling process may share memory, it is not possible for the child 
process to execute in the same stack as the calling process.
有谁能解释一下“共享内存”是如何让它变得不可能的。OTOH,一个常见的看法是线程中的函数执行序列将不同于其他线程,因此我们需要另一个堆栈

谢谢,
Kapil

两个线程不能使用同一堆栈。他们会把事情搞得一团糟,很快就会崩溃

使用
fork
时,没有内存共享。两个线程具有相同的堆栈指针值,但它指向物理上不同的内存页

使用
pthread\u create
时,会为新线程选择一个新堆栈指针,与父线程分开。这样它们就不会破坏彼此的堆栈


clone
是一种低级功能,介于两者之间。它保持内存共享,因此线程不能共享堆栈。但与
pthread\u create
不同,新堆栈指针由用户决定,用户可以根据自己的意愿选择。您引用的这句话警告您应该小心选择。

嗯,您的解释是正确的,但混淆的是,为什么在clone()中特别提到内存共享,而在这两种情况下(即fork和clone),每个堆栈都将在物理内存中的单独位置执行。我的意思是,堆栈永远不会共享!!!,内存共享和它有什么关系?所有情况都是如此。但是
clone
允许您选择一个错误的堆栈指针并受到影响,而
fork
pthread\u create
不会让您犯这个错误。