C++ 什么是进程和线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2319924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:01:11  来源:igfitidea点击:

What is process and thread?

c++multithreadingprocessoperating-system

提问by smwikipedia

Yes, I have read many materials related to operating system. And I am still reading. But it seems all of them are describing the process and thread in a "abstract" way, which makes a lot of high level elabration on their behavior and logic orgnization. I am wondering what are they physically?In my opinion, they are just some in-memory "data structures" which are maintained and used by the kernel codes to facilitate the execution of program. For example, operating system use some process data structure (PCB)to describe the aspects of the process assigned for a certain program, such as its priority, its address space and so on. Is this all right?

是的,我已经阅读了许多与操作系统相关的材料。而且我还在读书。但似乎他们都在以“抽象”的方式描述进程和线程,这使得对其行为和逻辑组织进行了大量高层次的阐述。我想知道他们的身体是什么?在我看来,它们只是内核代码维护和使用的一些内存中的“数据结构”,以促进程序的执行。例如,操作系统使用一些进程数据结构(PCB)来描述为某个程序分配的进程的各个方面,例如它的优先级、它的地址空间等。这一切都好吗?

采纳答案by AaronLS

Normally when you run an executable like notepad.exe, this creates a single process. These process could spawn other processes, but in most cases there is a single process for each executable that you run. Within the process, there can be many threads. Usually at first there is one thread, which usually starts at the programs "entry point" which is the mainfunction usually. Instructions are executed one by one in order, like a person who only has one hand, a thread can only do one thing at a time before it moves on to the next.

通常,当您运行像 notepad.exe 这样的可执行文件时,这会创建一个进程。这些进程可能会产生其他进程,但在大多数情况下,您运行的每个可执行文件都有一个进程。在进程中,可以有很多线程。通常一开始有一个线程,它通常从程序“入口点”开始,通常是main函数。指令是依次执行的,就像一个人只有一只手一样,一个线程一次只能做一件事,然后才能继续执行。

That first thread can create additional threads. Each additional thread has it's own entry point, which is usually defined with a function. The process is like a container for all the threads that have been spawned within it.

第一个线程可以创建其他线程。每个附加线程都有自己的入口点,通常用一个函数定义。该进程就像一个容器,用于存放在其中生成的所有线程。

That is a pretty simplistic explanation. I could go into more detail but probably would overlap with what you will find in your textbooks.

这是一个非常简单的解释。我可以更详细地介绍,但可能会与您在教科书中找到的内容重叠。

EDIT: You'll notice there are lot's of "usually"'s in my explanation, as there are occasionally rare programs that do things drastically different.

编辑:你会注意到在我的解释中有很多“通常”,因为偶尔会有一些罕见的程序做的事情完全不同。

回答by Dmitry

First thing you need to know to understand the difference between a process and a thread, is a fact, that processes do not run, threads do.

要了解进程和线程之间的区别,您首先需要知道的是,进程不会运行,线程会运行

So, what is a thread? Closest I can get explaining it is an execution state, as in: a combination of CPU registers, stack, the lot. You can see a proof of that, by breaking in a debugger at anygiven moment. What do you see? A call stack, a set of registers. That's pretty much it. That's the thread.

那么,什么是线程?我能解释的最接近的是执行状态,例如:CPU 寄存器、堆栈、批次的组合。您可以通过在任何给定时刻插入调试器来查看证明。你看到了什么?一个调用栈,一组寄存器。差不多就是这样。这就是线程。

Now, then, what is a process. Well, it's a like an abstract "container" entity for running threads. As far as OS is concerned in a first approximation, it's an entity OS allocates some VM to, assigns some system resources to (like file handles, network sockets), &c.

那么,什么是过程。嗯,它就像一个抽象的“容器”实体,用于运行线程。就操作系统而言,它是一个实体操作系统,它分配一些虚拟机,分配一些系统资源(如文件句柄、网络套接字)等。

How do they work together? The OS creates a "process" by reserving some resources to it, and starting a "main" thread. That thread then can spawn more threads. Those are the threads in one process. They more or less can share those resources one way or another (say, locking might be needed for them not to spoil the fun for others &c). From there on, OS is normally responsible for maintaining those threads "inside" that VM (detecting and preventing attempts to access memory which doesn't "belong" to that process), providing some type of scheduling those threads, so that they can run "one-after-another-and-not-just-one-all-the-time".

它们如何协同工作?操作系统通过为其保留一些资源并启动一个“主”线程来创建一个“进程”。然后该线程可以产生更多线程。这些是一个进程中的线程。他们或多或少可以以一种或另一种方式共享这些资源(例如,他们可能需要锁定,以免破坏他人的乐趣等)。从那时起,操作系统通常负责维护该虚拟机“内部”的那些线程(检测并防止尝试访问不“属于”该进程的内存),提供某种类型的调度这些线程,以便它们可以运行“一个接一个,而不只是一个一个”。

回答by J?rg W Mittag

One of the reasons why it is pretty much impossible to describe threads and processes in a non-abstract way is that they areabstractions.

几乎不可能以非抽象方式描述线程和进程的原因之一是它们抽象的。

Their concrete implementations differ tremendously.

它们的具体实现有很大的不同。

Compare for example an Erlang Process and a Windows Process: an Erlang Process is very lightweight, often less than 400 Bytes. You can start 10 million processes on a not very recent laptop without any problems. They start up veryquickly, they die veryquickly and you are expected to be able to use them for very short tasks. Every Erlang Process has its own Garbage Collector associated with it. Erlang Processes can never share memory, ever.

例如比较一个 Erlang 进程和一个 Windows 进程:一个 Erlang 进程非常轻量级,通常小于 400 字节。您可以在不太新的笔记本电脑上启动 1000 万个进程而不会出现任何问题。它们启动得非常快,它们死得非常快,并且您应该能够将它们用于非常短的任务。每个 Erlang 进程都有自己的垃圾收集器与之关联。Erlang 进程永远不能共享内存,永远。

Windows Processes are very heavy, sometimes hundreds of MiBytes. You can start maybe a couple of thousand of them on a beefy server, if you are lucky. They start up and die pretty slowly. Windows Processes are the units of Applications such as IDEs or Text Editors or Word Processors, so they are usually expected to live quite a long time (at least several minutes). They have their own Address Space, but no Garbage Collector. Windows Processes can share memory, although by default they don't.

Windows 进程非常繁重,有时有数百 MiBytes。如果幸运的话,您可以在强大的服务器上启动几千个。它们开始和死亡非常缓慢。Windows 进程是诸如 IDE 或文本编辑器或字处理器等应用程序的单元,因此它们通常会存活很长时间(至少几分钟)。他们有自己的地址空间,但没有垃圾收集器。Windows 进程可以共享内存,但默认情况下它们不共享。

Threads are a similar matter: an NPTL Linux Thread on x86 can be as small as 4 KiByte and with some tricks you can start 800000+ on a 32 Bit x86 machine. The machine will certainly be useable with thousands, maybe tens of thousands of threads. A .NET CLR Thread has a minimum size of about 1 MiByte, which means that just 4000 of those will eat up your entire address space on a 32 Bit machine. So, while 4000 NPTL Linux Threads is generally not a problem, you can't even start4000 .NET CLR Threads because you will run out of memory before that.

线程是一个类似的问题:x86 上的 NPTL Linux 线程可以小到 4 KiByte,通过一些技巧,您可以在 32 位 x86 机器上启动 800000+。该机器肯定可以使用数千甚至数万个线程。.NET CLR 线程的最小大小约为 1 MiByte,这意味着其中只有 4000 个将占用您在 32 位机器上的整个地址空间。因此,虽然 4000 个 NPTL Linux 线程通常不是问题,但您甚至无法启动4000 个 .NET CLR 线程,因为在此之前您将耗尽内存。

OS Processes and OS Threads are also implemented very differently between different Operating Systems. The main two approaches are: the kernel knows only about processes. Threads are implemented by a Userspace Library, without any knowledge of the kernel at all. In this case, there are again two approaches: 1:1 (every Thread maps to one Kernel Process) or m:n (m Threads map to n Processes, where usually m > n and often n == #CPUs). This was the early approach taken on many Operating Systems after Threads were invented. However, it is usually deemed inefficient and has been replaced on almost all systems by the second approach: Threads are implemented (at least partially) in the kernel, so that the kernel now knows about two distinct entities, Threads and Processes.

操作系统进程和操作系统线程在不同操作系统之间的实现也大不相同。主要的两种方法是:内核只知道进程。线程由用户空间库实现,根本不了解内核。在这种情况下,还有两种方法:1:1(每个线程映射到一个内核进程)或 m:n(m 个线程映射到 n 个进程,其中通常 m > n 并且通常 n == #CPUs)。这是线程被发明后在许多操作系统上采用的早期方法。然而,它通常被认为效率低下,并且在几乎所有系统上都被第二种方法取代:线程在内核中实现(至少部分地),因此内核现在知道两个不同的实体,线程和进程。

One Operating System that goes a third route, is Linux. In Linux, Threads are neither implemented in Userspace nor in the Kernel. Instead, the Kernel provides an abstraction of botha Thread and a Process (and indeed a couple of more things), called a Task. A Task is a Kernel Scheduled Entity, that carries with it a set of flags that determine which resources it shares with its siblings and which ones are private.

走第三条路的一种操作系统是 Linux。在 Linux 中,线程既不在用户空间也不在内核中实现。相反,内核提供了一个抽象两种线程和进程(实际上一对夫妇的更多的东西),称为任务。任务是一个内核调度实体,它带有一组标志,用于确定它与其兄弟姐妹共享哪些资源以及哪些是私有的。

Depending on how you set those flags, you get either a Thread (share pretty much everything) or a Process (share all system resources like the system clock, the filesystem namespace, the networking namespace, the user ID namespace, the process ID namespace, but do not sharethe Address Space). But you can also get some other pretty interesting things, too. You can trivially get BSD-style jails (basically the same flags as a Process, but don't share the filesystem or the networking namespace). Or you can get what other OSs call a Virtualization Container or Zone (like a jail, but don't share the UID and PID namespaces and system clock). Since a couple of years ago via a technology called KVM (Kernel Virtual Machine) you can even get a full-blown Virtual Machine (share nothing, not even the processor's Page Tables). [The cool thing about this is that you get to reuse the highly-tuned mature Task Scheduler in the kernel for all of these things. One of the things the Xen Virtual Machine has often criticized for, was the poor performance of its scheduler. The KVM developers have a much superior scheduler than Xen, and the best thing is they didn't even have to write a single line of code for it!]

根据您设置这些标志的方式,您将获得一个线程(共享几乎所有内容)或一个进程(共享所有系统资源,如系统时钟、文件系统命名空间、网络命名空间、用户 ID 命名空间、进程 ID 命名空间、但不分享地址空间)。但是你也可以得到一些其他非常有趣的东西。您可以轻松获得 BSD 风格的 jails(基本上与进程相同的标志,但不共享文件系统或网络命名空间)。或者您可以获得其他操作系统称为虚拟化容器或区域的内容(如监狱,但不共享 UID 和 PID 命名空间以及系统时钟)。几年前,通过一种称为 KVM(内核虚拟机)的技术,您甚至可以获得一个成熟的虚拟机(不共享任何内容,甚至不共享处理器的页表)。[关于这个很酷的事情是你可以在内核中重用高度调整的成熟任务调度程序来处理所有这些事情。Xen 虚拟机经常批评的一件事是其调度程序的性能不佳。

So, on Linux, the performance of Threads and Processes is much closer than on Windows and many other systems, because on Linux, they are actually the same thing. Which means that the usage patterns are very different: on Windows, you typically decide between using a Thread and a Process based on their weight: can I afford a Process or should I use a Thread, even though I actually don't want to share state? On Linux (and usually Unix in general), you decide based on their semantics: do I actually want to share state or not?

所以,在 Linux 上,线程和进程的性能比在 Windows 和许多其他系统上更接近,因为在 Linux 上,它们实际上是同一个东西。这意味着使用模式非常不同:在 Windows 上,您通常会根据它们的权重决定使用线程和进程:我可以负担得起进程还是应该使用线程,即使我实际上不想共享状态?在 Linux(通常通常是 Unix)上,您根据它们的语义来决定:我是否真的想要共享状态?

One reason whyProcesses tend to be lighter on Unix than on Windows, is different usage: on Unix, Processes are the basic unit of both concurrency and functionality. If you want to use concurrency, you use multiple Processes. If your application can be broken down into multiple independent pieces, you use multiple Processes. Every Process does exactly one thing and onlythat one thing. Even a simple one-line shell script often involves dozens or hundreds of Processes. Applications usually consist of many, often short-lived Processes.

其中一个原因为什么过程往往是在Unix上比在Windows更轻,是不同的使用:在Unix上,过程都是并发性和功能的基本单位。如果要使用并发,则使用多个进程。如果您的应用程序可以分解为多个独立的部分,则您可以使用多个进程。每个进程只做一件事,而且只做一件事。即使是一个简单的单行 shell 脚本,也经常涉及几十个或数百个进程。应用程序通常由许多通常是短暂的进程组成。

On Windows, Threads are the basic units of concurrency and COM components or .NET objects are the basic units of functionality. Applications usually consist of a single long-running Process.

在 Windows 上,线程是并发的基本单位,COM 组件或 .NET 对象是功能的基本单位。应用程序通常由一个长期运行的进程组成。

Again, they are used for very different purposes and have very different design goals. It's not that one or the other is better or worse, it's just that they are sodifferent that the common characteristics can only be described very abstractly.

同样,它们用于非常不同的目的并且具有非常不同的设计目标。并不是说哪一个好坏,只是它们差异太大,共同的特征只能非常抽象地描述。

Pretty much the only few things you can say about Threads and Processes are that:

关于线程和进程,您可以说的几乎只有几件事:

  • Threads belong to Processes
  • Threads are lighter than Processes
  • Threads share most state with each other
  • Processes share significantly less state than Threads (in particular, they generally share no memory, unless specifically requested)
  • 线程属于进程
  • 线程比进程更轻
  • 线程彼此共享大部分状态
  • 进程共享的状态明显少于线程(特别是,它们通常不共享内存,除非特别要求)

回答by t0mm13b

Have a look at the detailed answer I gave previously here on SO. It gives an insight into a toy kernel structure responsible for maintaining processes and the threads...

看看我之前在SO上给出的详细答案。它深入了解了负责维护进程和线程的玩具内核结构......

Hope this helps, Best regards, Tom.

希望这会有所帮助,最好的问候,汤姆。

回答by Ben

I would say that :

我会这样说 :

A process has a memory space, opened files,..., and one or more threads.

一个进程有一个内存空间、打开的文件……以及一个或多个线程。

A thread is an instruction stream that can be scheduled by the system on a processor.

线程是可以由系统在处理器上调度的指令流。

回答by DOK

We have discussed this very issue a number of times here. Perhaps you will find some helpful information here:

我们在这里已经多次讨论过这个问题。也许您会在这里找到一些有用的信息:

What is the difference between a process and a thread

进程和线程有什么区别

Process vs Thread

进程与线程

Thread and Process

线程和进程

回答by David Morton

A process is a container for a set of resources used while executing a program.

进程是执行程序时使用的一组资源的容器。

A process includes the following:

一个过程包括以下内容:

  • Private virtual address space
  • A program.
  • A list of handles.
  • An access token.
  • A unique process ID.
  • At least one thread.
  • A pointer to the parent process, whether or not the process still exists or not.
  • 私有虚拟地址空间
  • 一个程序。
  • 句柄列表。
  • 访问令牌。
  • 唯一的进程 ID。
  • 至少一根线。
  • 指向父进程的指针,无论该进程是否仍然存在。

That being said, a process can contain multiple threads.

也就是说,一个进程可以包含多个线程。

Processes themselves can be grouped into jobs, which are containers for processes and are executed as single units.

进程本身可以分组为作业,作业是进程的容器并作为单个单元执行。

A threadis what windows uses to schedule execution of instructions on the CPU. Every process has at least one.

一个线程就是Windows用来调度执行的CPU上的说明。每个进程至少有一个。

I have a couple of pages on my wiki you could take a look at:

我的维基上有几页你可以看看:

Process

过程

Thread

线

回答by Ana Betts

Physically:

身体上:

  • Process is a structure that maintains the owning credentials, the thread list, and an open handle list

  • A Thread is a structure containing a context(i.e. a saved register set + a location to execute), a set of PTEs describing what pages are mapped into the process's Virtual Address space, and an owner.

  • 进程是一个维护拥有凭证、线程列表和打开句柄列表的结构

  • 线程是一个结构,它包含一个上下文(即一个保存的寄存器集+一个要执行的位置)、一组描述哪些页面被映射到进程的虚拟地址空间的 PTE,以及一个所有者。

This is of course an extremely simplified explanation, but it gets the important bits. The fundamental unit of execution on both Linux and Windows is the Thread - the kernel scheduler doesn't care about processes (much). This is why on Linux, a thread is just a process who happens to share PTEs with another process.

这当然是一个极其简化的解释,但它得到了重要的部分。Linux 和 Windows 上的基本执行单元是线程——内核调度程序不关心进程(很多)。这就是为什么在 Linux 上,线程只是一个碰巧与另一个进程共享 PTE 的进程。

回答by Chris Dennett

Threads are memory structures in the scheduler of the operating system, as you say. Threads point to the start of some instructions in memory and process these when the scheduler decides they should be. While the thread is executing, the hardware timer will run. Once it hits the desired time, an interrupt will be invoked. After this, the hardware will then stop execution of the current program, and will invoke the registered interrupt handler function, which will be part of the scheduler, to inform that the current thread has finished execution.

正如您所说,线程是操作系统调度程序中的内存结构。线程指向内存中一些指令的开始,并在调度程序决定它们应该是时处理这些指令。当线程正在执行时,硬件计时器将运行。一旦达到所需时间,就会调用中断。此后,硬件将停止当前程序的执行,并调用注册的中断处理程序函数,该函数将成为调度程序的一部分,以通知当前线程已完成执行。

回答by Kangkan

A process is a area in memory managed by the OS to run an application. Thread is a small area in memory within a process to run a dedicated task.

进程是操作系统管理的内存区域,用于运行应用程序。线程是进程内内存中的一个小区域,用于运行专用任务。