multithreading 进程和线程有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/200469/
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
What is the difference between a process and a thread?
提问by James Fassett
What is the technical difference between a process and a thread?
进程和线程之间的技术区别是什么?
I get the feeling a word like 'process' is overused and there are also hardware and software threads. How about light-weight processes in languages like Erlang? Is there a definitive reason to use one term over the other?
我觉得像“进程”这样的词被过度使用了,而且还有硬件和软件线程。像Erlang这样的语言中的轻量级进程怎么样?是否有明确的理由使用一个术语而不是另一个?
采纳答案by Greg Hewgill
Both processes and threads are independent sequences of execution. The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.
进程和线程都是独立的执行序列。典型的区别是(同一进程的)线程在共享内存空间中运行,而进程在单独的内存空间中运行。
I'm not sure what "hardware" vs "software" threads you might be referring to. Threads are an operating environment feature, rather than a CPU feature (though the CPU typically has operations that make threads efficient).
我不确定您可能指的是什么“硬件”与“软件”线程。线程是一种操作环境特性,而不是 CPU 特性(尽管 CPU 通常具有使线程高效的操作)。
Erlang uses the term "process" because it does not expose a shared-memory multiprogramming model. Calling them "threads" would imply that they have shared memory.
Erlang 使用术语“进程”是因为它没有公开共享内存多道程序模型。称它们为“线程”意味着它们具有共享内存。
回答by Scott Langham
Process
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
进程
每个进程提供执行程序所需的资源。进程具有虚拟地址空间、可执行代码、系统对象的开放句柄、安全上下文、唯一进程标识符、环境变量、优先级类、最小和最大工作集大小以及至少一个执行线程。每个进程都以单个线程启动,通常称为主线程,但可以从其任何线程创建附加线程。
Thread
A thread is an entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients.
线程
线程是进程中可以调度执行的实体。进程的所有线程共享其虚拟地址空间和系统资源。此外,每个线程都维护异常处理程序、调度优先级、线程本地存储、唯一的线程标识符以及系统将用来保存线程上下文直到被调度的一组结构。线程上下文包括线程的机器寄存器集、内核堆栈、线程环境块和线程进程地址空间中的用户堆栈。线程也可以有自己的安全上下文,可用于模拟客户端。
This information was found on Microsoft Docs here: About Processes and Threads
此信息可在 Microsoft Docs 上找到:关于进程和线程
Microsoft Windows supports preemptive multitasking, which creates the effect of simultaneous execution of multiple threads from multiple processes. On a multiprocessor computer, the system can simultaneously execute as many threads as there are processors on the computer.
Microsoft Windows 支持抢占式多任务处理,这会产生来自多个进程的多个线程同时执行的效果。在多处理器计算机上,系统可以同时执行与计算机上的处理器一样多的线程。
回答by Kumar
Process:
过程:
- An executing instance of a program is called a process.
- Some operating systems use the term ‘task‘ to refer to a program that is being executed.
- A process is always stored in the main memory also termed as the primary memory or random access memory.
- Therefore, a process is termed as an active entity. It disappears if the machine is rebooted.
- Several process may be associated with a same program.
- On a multiprocessor system, multiple processes can be executed in parallel.
- On a uni-processor system, though true parallelism is not achieved, a process scheduling algorithm is applied and the processor is scheduled to execute each process one at a time yielding an illusion of concurrency.
- Example:Executing multiple instances of the ‘Calculator' program. Each of the instances are termed as a process.
- 程序的一个执行实例称为进程。
- 某些操作系统使用术语“任务”来指代正在执行的程序。
- 进程始终存储在主存储器中,也称为主存储器或随机存取存储器。
- 因此,进程被称为活动实体。如果机器重新启动,它就会消失。
- 多个进程可能与同一个程序相关联。
- 在多处理器系统上,可以并行执行多个进程。
- 在单处理器系统上,虽然没有实现真正的并行性,但应用了进程调度算法,并且处理器被调度为一次执行每个进程,从而产生并发的错觉。
- 示例:执行“计算器”程序的多个实例。每个实例都称为一个过程。
Thread:
线:
- A thread is a subset of the process.
- It is termed as a ‘lightweight process', since it is similar to a real process but executes within the context of a process and shares the same resources allotted to the process by the kernel.
- Usually, a process has only one thread of control – one set of machine instructions executing at a time.
- A process may also be made up of multiple threads of execution that execute instructions concurrently.
- Multiple threads of control can exploit the true parallelism possible on multiprocessor systems.
- On a uni-processor system, a thread scheduling algorithm is applied and the processor is scheduled to run each thread one at a time.
- All the threads running within a process share the same address space, file descriptors, stack and other process related attributes.
- Since the threads of a process share the same memory, synchronizing the access to the shared data within the process gains unprecedented importance.
- 线程是进程的子集。
- 它被称为“轻量级进程”,因为它类似于真正的进程,但在进程的上下文中执行并共享内核分配给进程的相同资源。
- 通常,一个进程只有一个控制线程——一次执行一组机器指令。
- 一个进程也可以由同时执行指令的多个执行线程组成。
- 多线程控制可以利用多处理器系统上可能的真正并行性。
- 在单处理器系统上,应用线程调度算法,处理器被调度为一次运行一个线程。
- 进程内运行的所有线程共享相同的地址空间、文件描述符、堆栈和其他进程相关属性。
- 由于进程的线程共享相同的内存,因此同步对进程内共享数据的访问变得前所未有的重要。
I borrowed the above info from the Knowledge Quest! blog.
回答by Robert S. Barnes
First, let's look at the theoretical aspect. You need to understand what a process is conceptually to understand the difference between a process and a thread and what's shared between them.
首先,让我们看一下理论方面。您需要从概念上了解进程是什么,以了解进程和线程之间的区别以及它们之间共享的内容。
We have the following from section 2.2.2 The Classical Thread Modelin Modern Operating Systems 3eby Tanenbaum:
我们在Tanenbaum 的第2.2.2节现代操作系统 3e 中的经典线程模型中有以下内容:
The process model is based on two independent concepts: resource grouping and execution. Sometimes it is use-ful to separate them; this is where threads come in....
流程模型基于两个独立的概念:资源分组和执行。有时将它们分开是有用的;这就是线程进来的地方......
He continues:
他继续:
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.
查看流程的一种方式是,它是一种将相关资源组合在一起的方式。进程具有包含程序文本和数据以及其他资源的地址空间。这些资源可能包括打开的文件、子进程、挂起的警报、信号处理程序、记帐信息等。通过以流程的形式将它们组合在一起,可以更轻松地对其进行管理。进程的另一个概念是执行线程,通常简称为线程。该线程有一个程序计数器,用于跟踪下一个要执行的指令。它有寄存器,用于保存当前的工作变量。它有一个堆栈,其中包含执行历史记录,每个过程调用但尚未返回一个帧。虽然一个线程必须在某个进程中执行,线程和它的进程是不同的概念,可以分开处理。进程用于将资源组合在一起;线程是安排在 CPU 上执行的实体。
Further down he provides the following table:
再往下,他提供了下表:
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 |
Let's deal with the hardware multithreadingissue. Classically, a CPU would support a single thread of execution, maintaining the thread's state via a single program counter, and set of registers. But what happens if there's a cache miss? It takes a long time to fetch data from main memory, and while that's happening the CPU is just sitting there idle. So someone had the idea to basically have two sets of thread state ( PC + registers ) so that another thread ( maybe in the same process, maybe in a different process ) can get work done while the other thread is waiting on main memory. There are multiple names and implementations of this concept, such as HyperThreading and Simultaneous Multithreading( SMT for short ).
让我们来处理硬件多线程问题。传统上,CPU 将支持单个执行线程,通过单个程序计数器和一组寄存器维护线程的状态。但是如果缓存未命中会发生什么?从主内存中获取数据需要很长时间,而在这种情况下,CPU 只是闲置在那里。所以有人想基本上有两组线程状态(PC + 寄存器),这样另一个线程(可能在同一个进程中,可能在不同的进程中)可以在另一个线程等待主内存时完成工作。这个概念有多种名称和实现,例如超线程和同步多线程(简称 SMT)。
Now let's look at the software side. There are basically three ways that threads can be implemented on the software side.
现在让我们看看软件方面。线程在软件方面的实现基本上有三种方式。
- Userspace Threads
- Kernel Threads
- A combination of the two
- 用户空间线程
- 内核线程
- 两者的结合
All you need to implement threads is the ability to save the CPU state and maintain multiple stacks, which can in many cases be done in user space. The advantage of user space threads is super fast thread switching since you don't have to trap into the kernel and the ability to schedule your threads the way you like. The biggest drawback is the inability to do blocking I/O ( which would block the entire process and all it's user threads ), which is one of the big reasons we use threads in the first place. Blocking I/O using threads greatly simplifies program design in many cases.
实现线程所需要的只是保存 CPU 状态和维护多个堆栈的能力,这在许多情况下可以在用户空间中完成。用户空间线程的优点是超快的线程切换,因为您不必陷入内核,并且能够按照您喜欢的方式安排线程。最大的缺点是无法进行阻塞 I/O(这会阻塞整个进程及其所有用户线程),这是我们首先使用线程的重要原因之一。在许多情况下,使用线程阻塞 I/O 可以极大地简化程序设计。
Kernel threads have the advantage of being able to use blocking I/O, in addition to leaving all the scheduling issues to the OS. But each thread switch requires trapping into the kernel which is potentially relatively slow. However, if you're switching threads because of blocked I/O this isn't really an issue since the I/O operation probably trapped you into the kernel already anyway.
内核线程的优势在于能够使用阻塞 I/O,此外还可以将所有调度问题留给操作系统。但是每个线程切换都需要进入内核,这可能相对较慢。但是,如果您因为 I/O 阻塞而切换线程,这并不是真正的问题,因为无论如何 I/O 操作可能已经将您困在内核中。
Another approach is to combine the two, with multiple kernel threads each having multiple user threads.
另一种方法是将两者结合起来,使用多个内核线程,每个内核线程都有多个用户线程。
So getting back to your question of terminology, you can see that a process and a thread of execution are two different concepts and your choice of which term to use depends on what you're talking about. Regarding the term "light weight process", I don't personally see the point in it since it doesn't really convey what's going on as well as the term "thread of execution".
回到您的术语问题,您可以看到进程和执行线程是两个不同的概念,您选择使用哪个术语取决于您在说什么。关于“轻量级进程”这个术语,我个人没有看到它的意义,因为它并没有真正传达正在发生的事情以及“执行线程”这个术语。
回答by Reachgoals
To explain more with respect to concurrent programming
解释更多关于并发编程的内容
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.
Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication.
一个进程有一个自包含的执行环境。一个进程通常有一套完整的、私有的基本运行时资源;特别是,每个进程都有自己的内存空间。
线程存在于一个进程中——每个进程至少有一个。线程共享进程的资源,包括内存和打开的文件。这有助于实现高效但可能存在问题的沟通。
Keeping average person in mind,
牢记普通人,
On your computer, open Microsoft Word and web browser. We call these two processes.
在您的计算机上,打开 Microsoft Word 和 Web 浏览器。我们称这两个过程为。
In Microsoft word, you type some thing and it gets automatically saved. Now, you would have observed editing and saving happens in parallel - editing on one thread and saving on the other thread.
在 Microsoft Word 中,您输入一些内容,它会自动保存。现在,您会观察到编辑和保存并行发生 - 在一个线程上编辑并在另一个线程上保存。
回答by Node
An application consists of one or more processes. A process, in the simplest terms, is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread. A fiber is a unit of execution that must be manually scheduled by the application. Fibers run in the context of the threads that schedule them.
一个应用程序由一个或多个进程组成。一个进程,用最简单的术语来说,就是一个正在执行的程序。一个或多个线程在进程上下文中运行。线程是操作系统分配处理器时间的基本单位。一个线程可以执行进程代码的任何部分,包括当前正在由另一个线程执行的部分。纤程是必须由应用程序手动调度的执行单元。Fiber 在调度它们的线程的上下文中运行。
Stolen from here.
回答by Gerald
A process is a collection of code, memory, data and other resources. A thread is a sequence of code that is executed within the scope of the process. You can (usually) have multiple threads executing concurrently within the same process.
进程是代码、内存、数据和其他资源的集合。线程是在进程范围内执行的一系列代码。您可以(通常)在同一进程中同时执行多个线程。
回答by Ratheesh
回答by ANK
Process:
过程:
- Process is a heavy weight process.
- Process is a separate program that has separate memory,data,resources ect.
- Process are created using fork() method.
- Context switch between the process is time consuming.
- 过程是一个重量级的过程。
- 进程是一个独立的程序,具有独立的内存、数据、资源等。
- 进程是使用 fork() 方法创建的。
- 进程之间的上下文切换是耗时的。
Example:
Say, opening any browser (mozilla, Chrome, IE). At this point new process will start to execute.
示例:
假设打开任何浏览器(mozilla、Chrome、IE)。此时新进程将开始执行。
Threads:
主题:
- Threads are light weight processes.Threads are bundled inside the process.
- Threads have a shared memory,data,resources,files etc.
- Threads are created using clone() method.
- Context switch between the threads are not much time consuming as Process.
- 线程是轻量级进程。线程被捆绑在进程内。
- 线程具有共享内存、数据、资源、文件等。
- 线程是使用 clone() 方法创建的。
- 线程之间的上下文切换不像 Process 那样耗时。
Example:
Opening multiple tabs in the browser.
示例:
在浏览器中打开多个选项卡。
回答by karthikeyan_somu
- Every process is a thread (primary thread).
- But every thread is not a process. It is a part(entity) of a process.
- 每个进程都是一个线程(主线程)。
- 但是每个线程都不是一个进程。它是流程的一部分(实体)。