最近,我在一次采访中被问到一个问题:进程和线程有什么区别?真的,我不知道答案。我想了一会儿,给出了一个非常奇怪的答案。

线程共享相同的内存,而进程不共享。回答完这个问题后,面试官对我邪恶地笑了笑,然后接连问了我几个问题:

问:你知道节目分成哪些部分吗?

我的答案是:是的(认为这很简单)堆栈,数据,代码,堆

问:那么,告诉我:线程共享哪些片段?

我无法回答这个问题,最后只能把它们都说了出来。

请问,谁能就进程和线程之间的区别给出正确的、令人印象深刻的答案?


当前回答

线程共享代码、数据段和堆,但不共享堆栈。

其他回答

线程共享数据和代码,而进程不共享。栈不是为两者共享的。

进程也可以共享内存,更精确的代码,例如Fork()之后,但这是一个实现细节和(操作系统)优化。由多个进程共享的代码将(希望)在第一次写入代码时复制—这被称为写时复制。我不确定线程代码的确切语义,但我假设是共享代码。

           Process   Thread

   Stack   private   private
   Data    private   shared
   Code    private1  shared2

代码在逻辑上是私有的,但出于性能原因可能会被共享。 我不能百分之百肯定。

通常,线程被称为轻量级进程。如果我们把内存分成三个部分,那么它将是:代码,数据和堆栈。 每个进程都有自己的代码、数据和堆栈部分,由于这种上下文切换时间有点高。为了减少上下文切换的时间,人们提出了线程的概念,它与其他线程/进程共享数据和代码段,并拥有自己的堆栈段。

线程共享代码、数据段和堆,但不共享堆栈。

真正需要指出的是,这个问题有两个方面——理论方面和实现方面。

首先,让我们看看理论方面。您需要从概念上理解进程是什么,才能理解进程和线程之间的区别以及它们之间共享的内容。

我们从Tanenbaum的2.2.2节现代操作系统3e中的经典线程模型中获得以下内容:

流程模型基于两个独立的概念:资源 分组和执行。有时把它们分开是有用的; 这就是线程....的由来

他继续说:

One way of looking at a process is that it is a way to group related resources together. A process has an address space containing program text and data, as well as other resources. These resource may include open files, child processes, pending alarms, signal handlers, accounting information, and more. By putting them together in the form of a process, they can be managed more easily. The other concept a process has is a thread of execution, usually shortened to just thread. The thread has a program counter that keeps track of which instruc­tion to execute next. It has registers, which hold its current working variables. It has a stack, which contains the execution history, with one frame for each proce­dure called but not yet returned from. Although a thread must execute in some process, the thread and its process are different concepts and can be treated sepa­rately. Processes are used to group resources together; threads are the entities scheduled for execution on the CPU.

他进一步提供了以下表格:

Per process items             | Per thread items
------------------------------|-----------------
Address space                 | Program counter
Global variables              | Registers
Open files                    | Stack
Child processes               | State
Pending alarms                |
Signals and signal handlers   |
Accounting information        |

以上就是线程工作所需要的。正如其他人指出的那样,像段这样的东西是依赖于操作系统的实现细节的。

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.