最近,我在一次采访中被问到一个问题:进程和线程有什么区别?真的,我不知道答案。我想了一会儿,给出了一个非常奇怪的答案。
线程共享相同的内存,而进程不共享。回答完这个问题后,面试官对我邪恶地笑了笑,然后接连问了我几个问题:
问:你知道节目分成哪些部分吗?
我的答案是:是的(认为这很简单)堆栈,数据,代码,堆
问:那么,告诉我:线程共享哪些片段?
我无法回答这个问题,最后只能把它们都说了出来。
请问,谁能就进程和线程之间的区别给出正确的、令人印象深刻的答案?
最近,我在一次采访中被问到一个问题:进程和线程有什么区别?真的,我不知道答案。我想了一会儿,给出了一个非常奇怪的答案。
线程共享相同的内存,而进程不共享。回答完这个问题后,面试官对我邪恶地笑了笑,然后接连问了我几个问题:
问:你知道节目分成哪些部分吗?
我的答案是:是的(认为这很简单)堆栈,数据,代码,堆
问:那么,告诉我:线程共享哪些片段?
我无法回答这个问题,最后只能把它们都说了出来。
请问,谁能就进程和线程之间的区别给出正确的、令人印象深刻的答案?
当前回答
您说得很对,但是线程共享除堆栈之外的所有段。线程有独立的调用堆栈,但是其他线程堆栈中的内存仍然是可访问的,理论上你可以在其他线程的本地堆栈框架中保存内存指针(尽管你可能应该找到一个更好的地方来放置内存!)。
其他回答
您说得很对,但是线程共享除堆栈之外的所有段。线程有独立的调用堆栈,但是其他线程堆栈中的内存仍然是可访问的,理论上你可以在其他线程的本地堆栈框架中保存内存指针(尽管你可能应该找到一个更好的地方来放置内存!)。
线程共享所有内容[1]。整个进程只有一个地址空间。
每个线程都有自己的堆栈和寄存器,但所有线程的堆栈都在共享地址空间中可见。
如果一个线程在它的堆栈上分配了某个对象,并将该地址发送给另一个线程,它们对该对象的访问权是相等的。
实际上,我刚刚注意到一个更广泛的问题:我认为你混淆了segment这个词的两种用法。
可执行文件(如ELF)的文件格式有不同的部分,可以称为段,包含编译的代码(文本)、初始化的数据、链接器符号、调试信息等。这里没有堆段或堆栈段,因为它们是仅运行时结构。
这些二进制文件段可以分别映射到进程地址空间,具有不同的权限(例如,对于代码/文本,只读可执行;对于初始化的数据,写时复制不可执行)。
根据约定(由语言运行库强制执行),这个地址空间的区域用于不同的目的,如堆分配和线程堆栈。不过,这些都只是内存,并且可能没有分段,除非您在虚拟8086模式下运行。每个线程的堆栈是在线程创建时分配的内存块,当前堆栈顶部地址存储在堆栈指针寄存器中,每个线程保留自己的堆栈指针和其他寄存器。
好的,我知道:信号掩码,TSS/TSD等。地址空间,包括它所有映射的程序段,仍然是共享的。
线程共享堆(有一个关于线程特定堆的研究),但当前的实现共享堆。(当然还有代码)
A process has code, data, heap and stack segments. Now, the Instruction Pointer (IP) of a thread OR threads points to the code segment of the process. The data and heap segments are shared by all the threads. Now what about the stack area? What is actually the stack area? Its an area created by the process just for its thread to use... because stacks can be used in a much faster way than heaps etc. The stack area of the process is divided among threads, i.e. if there are 3 threads, then the stack area of the process is divided into 3 parts and each is given to the 3 threads. In other words, when we say that each thread has its own stack, that stack is actually a part of the process stack area allocated to each thread. When a thread finishes its execution, the stack of the thread is reclaimed by the process. In fact, not only the stack of a process is divided among threads, but all the set of registers that a thread uses like SP, PC and state registers are the registers of the process. So when it comes to sharing, the code, data and heap areas are shared, while the stack area is just divided among threads.
来自维基百科(我认为这对面试官来说是个很好的回答:P)
Threads differ from traditional multitasking operating system processes in that: processes are typically independent, while threads exist as subsets of a process processes carry considerable state information, whereas multiple threads within a process share state as well as memory and other resources processes have separate address spaces, whereas threads share their address space processes interact only through system-provided inter-process communication mechanisms. Context switching between threads in the same process is typically faster than context switching between processes.