在 Linux 上,是否可以让进程的线程在不同的虚拟地址空间上运行?如果是这样,怎么做?
3 回答
不 ,根据定义,线程共享一个地址空间。如果您需要单独的地址空间,您应该使用多个进程。
尽管不可能在具有不同虚拟地址空间的同一进程中拥有线程,但仍然可以拥有Thread Local Storage。你可能对此感兴趣。
It is not possible. Linux uses the clone
system call to implement threads. The flags it takes include CLONE_THREAD
, meaning the new process is placed in the same thread group as the calling process, and CLONE_VM
, meaning the two processes/threads share virtual memory. Since Linux kernel version 2.6.0-test6, you cannot specify CLONE_THREAD
without specifying CLONE_VM
. (See the errors section of that link.)
However, depending on your exact motivation, you may be able to find a combination of flags for clone
that do what you want.