进程和线程的区别是什么?

时间:2023-01-20 17:35:34

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这样的语言的轻量级过程呢?是否有明确的理由使用一个术语而不是另一个术语?

31 个解决方案

#1


1043  

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).

我不确定您可能指的是什么“硬件”vs“软件”线程。线程是一个操作环境特性,而不是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使用“进程”这个术语,因为它不公开共享内存的多编程模型。称它们为“线程”将意味着它们拥有共享内存。

#2


604  

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.

线程是可以被调度执行的进程中的一个实体。进程的所有线程共享其虚拟地址空间和系统资源。此外,每个线程都维护异常处理程序、调度优先级、线程本地存储、惟一线程标识符,以及系统将用于保存线程上下文的一组结构,直到它被调度为止。线程上下文包括线程的机器寄存器组、内核堆栈、线程环境块和线程进程地址空间中的用户堆栈。线程还可以拥有自己的安全上下文,可用于模拟客户端。


Found this on MSDN here:
About Processes and Threads

在MSDN上找到这个:关于进程和线程。

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支持抢占式多任务处理,它创建多个进程同时执行多个线程的效果。在多处理器计算机上,系统可以同时执行许多线程,因为计算机上有处理器。

#3


208  

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 withing the process gains unprecedented importance.
  • 由于进程的线程共享相同的内存,因此同步访问共享数据的访问将获得前所未有的重要性。

I borrowed the above info from the Knowledge Quest! blog.

我从知识探索中借用了上述信息!博客。

#4


82  

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 Model in Modern Operating Systems 3e by Tanenbaum:

我们从第2.2.2节中得到了现代操作系统的经典线程模型:Tanenbaum:

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 multithreading issue. 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.

现在让我们看看软件方面。基本上有三种方法可以在软件方面实现线程。

  1. Userspace Threads
  2. 用户空间线程
  3. Kernel Threads
  4. 内核线程
  5. A combination of the two
  6. 两者的结合。

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".

回到你的术语问题,你可以看到一个过程和一个执行的线程是两个不同的概念你选择哪个术语取决于你说的是什么。关于“轻量级过程”这个术语,我个人并不认为它有什么意义,因为它并没有真正地传达“执行的线程”这一术语。

#5


39  

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.

应用程序由一个或多个进程组成。一个过程,用最简单的术语来说,就是一个执行程序。一个或多个线程在进程的上下文中运行。线程是操作系统分配处理器时间的基本单元。线程可以执行过程代码的任何部分,包括当前由另一个线程执行的部分。光纤是一个执行单元,必须由应用程序手动安排。纤维在调度它们的线程的上下文中运行。

Stolen from here.

从这里偷来的。

#6


35  

To explain more with respect to concurrent programming

更多地解释并发编程。

  1. 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.

    流程有一个自包含的执行环境。一个过程通常有一个完整的、私有的基本运行时资源集;特别是,每个进程都有自己的内存空间。

  2. 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.

在你的电脑上,打开微软Word和网页浏览器。我们称之为两个过程。

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.

在微软word中,你输入一些东西,然后自动保存。现在,您可以观察到编辑和保存在一个线程上并行编辑,并在另一个线程上保存。

#7


22  

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.

进程是代码、内存、数据和其他资源的集合。线程是在流程的范围内执行的代码序列。您可以(通常)在同一个进程中同时执行多个线程。

#8


20  

  • Every process is a thread (primary thread).
  • 每个进程都是一个线程(主线程)。
  • But every thread is not a process. It is a part(entity) of a process.
  • 但是每个线程都不是一个过程。它是一个过程的一部分(实体)。

#9


16  

A process is an executing instance of an application. What does that mean? Well, for example, when you double-click the Microsoft Word icon, you start a process that runs Word. A thread is a path of execution within a process. Also, a process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.

进程是应用程序的执行实例。这是什么意思?例如,当你双击Microsoft Word图标时,你会启动一个运行Word的进程。线程是进程内执行的路径。此外,一个进程可以包含多个线程。当您启动Word时,操作系统创建一个进程并开始执行该进程的主线程。

It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.

需要注意的是,一个线程可以做任何一个进程可以做的事情。但是由于一个进程可以由多个线程组成,所以线程可以被认为是一个“轻量级”过程。因此,线程和进程之间的本质区别是每个线程都用来完成的工作。线程用于小任务,而进程用于更多的“重量级”任务——基本上是应用程序的执行。

Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.

线程和进程之间的另一个区别是,同一个进程中的线程共享相同的地址空间,而不同的进程没有共享相同的地址空间。这允许线程读取和写入相同的数据结构和变量,并促进线程之间的通信。进程之间的通信——也称为IPC,或进程间通信——是相当困难和资源密集的。

#10


12  

Process:

过程:

  1. Process is a heavy weight process.
  2. 过程是一个很重的过程。
  3. Process is a separate program that has separate memory,data,resources ect.
  4. 进程是一个单独的程序,它有单独的内存、数据和资源等。
  5. Process are created using fork() method.
  6. 过程是使用fork()方法创建的。
  7. Context switch between the process is time consuming.
  8. 过程之间的上下文切换非常耗时。

Example:
Say, opening any browser (mozilla, Chrome, IE). At this point new process will start to execute.

例如:打开任何浏览器(mozilla, Chrome, IE)。此时,新流程将开始执行。

Threads:

线程:

  1. Threads are light weight processes.Threads are bundled inside the process.
  2. 线程是轻量级的过程。线程在过程中被绑定。
  3. Threads have a shared memory,data,resources,files etc.
  4. 线程具有共享内存、数据、资源、文件等。
  5. Threads are created using clone() method.
  6. 线程是使用clone()方法创建的。
  7. Context switch between the threads are not much time consuming as Process.
  8. 线程之间的上下文切换不太耗费时间。

Example:
Opening multiple tabs in the browser.

示例:在浏览器中打开多个选项卡。

#11


11  

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.

进程和线程都是独立的执行序列。典型的区别是,线程(同一进程)在共享内存空间中运行,而进程在单独的内存空间中运行。

Process

过程

Is a program in execution. it has text section i.e the program code, current activity as represented by the value of program counter & content of processors register. It also includes the process stack that contains temporary data(such as function parameters, return addressed and local variables), and a data section, which contains global variables. A process may also include a heap, which is memory that is dynamically allocated during process run time.

是一个执行中的程序。它有text section i。程序代码,当前活动以程序计数器的值和处理器寄存器的内容为代表。它还包括包含临时数据的过程堆栈(例如函数参数、返回地址和局部变量)和一个包含全局变量的数据部分。进程还可能包含堆,这是在进程运行时动态分配的内存。

Thread

线程

A thread is a basic unit of CPU utilisation; it comprises a thread ID, a program counter, register set, and a stack. it shared with other threads belonging to the same process its code section, data section and other operating system resources such as open files and signals.

线程是CPU使用率的基本单位;它包括线程ID、程序计数器、寄存器组和堆栈。它与属于同一进程的其他线程共享它的代码段、数据段和其他操作系统资源,比如打开的文件和信号。

-- Taken from Operating System by Galvin

——由加尔文的操作系统。

#12


11  

Real world example for Process and Thread This will give you the basic idea about thread and process 进程和线程的区别是什么?

这个过程和线程的实际示例将给您提供关于线程和进程的基本思想。

I borrowed the above info from Scott Langham's Answer - thanks

我从Scott Langham的回答中借用了以上的信息——谢谢。

#13


9  

Trying to answer this question relating to Java world.

试图回答与Java世界有关的问题。

A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process.

进程是程序的执行,但线程是流程中的单个执行序列。一个进程可以包含多个线程。线程有时被称为轻量级进程。

For example:

例如:

Example 1: A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety.

示例1:JVM在一个进程中运行,而JVM中的线程共享属于该进程的堆。这就是为什么几个线程可以访问同一个对象的原因。线程共享堆,并拥有自己的堆栈空间。这是一个线程对方法及其局部变量的调用,使线程安全地从其他线程中运行。但是堆不是线程安全的,并且必须与线程安全同步。

Example 2: A program might not be able to draw pictures by reading keystrokes. The program must give its full attention to the keyboard input and lacking the ability to handle more than one event at a time will lead to trouble. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this. Here Drawing picture is a process and reading keystroke is sub process (thread).

例2:一个程序可能不能通过读击键来绘制图片。该程序必须充分注意键盘输入,同时缺乏处理多个事件的能力将导致麻烦。这个问题的理想解决方案是同时无缝地执行两个或多个程序段。线程允许我们这样做。这里绘图是一个过程,阅读击键是子进程(线程)。

#14


8  

Both threads and processes are atomic units of OS resource allocation (i.e. there is a concurrency model describing how CPU time is divided between them, and the model of owning other OS resources). There is a difference in:

线程和进程都是OS资源分配的原子单元(也就是说,有一个描述CPU时间是如何在它们之间分配的并发模型,以及拥有其他OS资源的模型)。有一个区别:

  • Shared resources (threads are sharing memory by definition, they do not own anything except stack and local variables; processes could also share memory, but there is a separate mechanism for that, maintained by OS)
  • 共享资源(线程按照定义共享内存,除了堆栈和局部变量之外,它们不拥有任何东西;进程也可以共享内存,但是有一个单独的机制,由操作系统维护。
  • Allocation space (kernel space for processes vs. user space for threads)
  • 分配空间(进程的内核空间和线程的用户空间)

Greg Hewgill above was correct about the Erlang meaning of the word "process", and here there's a discussion of why Erlang could do processes lightweight.

上面的Greg Hewgill对“过程”这个词的Erlang含义是正确的,这里有一个关于Erlang为什么可以做轻量级过程的讨论。

#15


8  

Difference between Thread and Process?

线程与进程的区别?

A process is an executing instance of an application and A thread is a path of execution within a process. Also, a process can contain multiple threads.It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.

进程是应用程序的执行实例,而线程是流程中的执行路径。此外,一个进程可以包含多个线程。需要注意的是,一个线程可以做任何一个进程可以做的事情。但是由于一个进程可以由多个线程组成,所以线程可以被认为是一个“轻量级”过程。因此,线程和进程之间的本质区别是每个线程都用来完成的工作。线程用于小任务,而进程用于更多的“重量级”任务——基本上是应用程序的执行。

Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.

线程和进程之间的另一个区别是,同一个进程中的线程共享相同的地址空间,而不同的进程没有共享相同的地址空间。这允许线程读取和写入相同的数据结构和变量,并促进线程之间的通信。进程之间的通信——也称为IPC,或进程间通信——是相当困难和资源密集的。

Here’s a summary of the differences between threads and processes:

下面是线程和进程之间的差异的总结:

  1. Threads are easier to create than processes since they don't require a separate address space.

    线程比进程更容易创建,因为它们不需要单独的地址空间。

  2. Multithreading requires careful programming since threads share data strucures that should only be modified by one thread at a time. Unlike threads, processes don't share the same address space.

    多线程需要仔细的编程,因为线程共享数据结构,而这些数据结构一次只能被一个线程修改。与线程不同,进程不共享相同的地址空间。

  3. Threads are considered lightweight because they use far less resources than processes.

    线程被认为是轻量级的,因为它们使用的资源远远少于流程。

  4. Processes are independent of each other. Threads, since they share the same address space are interdependent, so caution must be taken so that different threads don't step on each other.
    This is really another way of stating #2 above.

    过程是相互独立的。线程,因为它们共享同一个地址空间是相互依赖的,所以必须谨慎,这样不同的线程才不会彼此同步。这是另一种表述2的方法。

  5. A process can consist of multiple threads.

    一个进程可以由多个线程组成。

#16


7  

From the point of view of an interviewer, there are basically just 3 main things that I want to hear, besides obvious things like a process can have multiple threads:

从面试官的角度来看,基本上只有三件事是我想听的,除了一些显而易见的事情,比如一个过程可以有多个线程:

  1. Threads share same memory space, which means a thread can access memory from other's thread memory. Processes normally can not.
  2. 线程共享相同的内存空间,这意味着线程可以从其他线程内存访问内存。过程通常不能。
  3. Resources. Resources (memory, handles, sockets, etc) are release at process termination, not thread termination.
  4. 资源。资源(内存、句柄、套接字等)在进程终止时释放,而不是线程终止。
  5. Security. A process has a fixed security token. A thread, on the other hand, can impersonate different users/tokens.
  6. 安全。进程有一个固定的安全令牌。另一方面,线程可以模拟不同的用户/令牌。

If you want more, Scott Langham's response pretty much covers everything. All these are from the perspective of an operating system. Different languages can implement different concepts, like tasks, light-wigh threads and so on, but they are just ways of using threads (of fibers on Windows). There are no hardware and software threads. There are hardware and software exceptions and interrupts, or user-mode and kernel threads.

如果你想要更多,斯科特·朗廷的回答几乎涵盖了一切。所有这些都是从操作系统的角度来看的。不同的语言可以实现不同的概念,比如任务、光导线程等等,但它们只是使用线程(在Windows上的纤维)的方法。没有硬件和软件线程。有硬件和软件异常和中断,或用户模式和内核线程。

#17


7  

  1. Basically, a thread is a part of a process without process thread wouldn't able to work.
  2. 基本上,线程是进程的一部分,没有进程线程无法工作。
  3. A thread is lightweight whereas the process is heavyweight.
  4. 线程是轻量级的,而过程是重量级的。
  5. communication between process requires some Time whereas thread requires less time.
  6. 进程之间的通信需要一些时间,而线程需要的时间更少。
  7. Threads can share the same memory area whereas process lives in separate.
  8. 线程可以共享相同的内存区域,而进程则是分开的。

#18


5  

  1. A thread runs in a shared memory space, but a process runs in a separate memory space
  2. 线程在共享内存空间中运行,但是进程在一个单独的内存空间中运行。
  3. A thread is a light-weight process, but a process is a heavy-weight process.
  4. 一个线程是一个轻量级过程,但是过程是一个重权过程。
  5. A thread is a subtype of process.
  6. 线程是进程的子类型。

#19


5  

The following is what I got from one of the articles on The Code Project. I guess it explains everything needed clearly.

下面是我从代码项目的一篇文章中得到的内容。我想它能清楚地解释一切。

A thread is another mechanism for splitting the workload into separate execution streams. A thread is lighter weight than a process. This means, it offers less flexibility than a full blown process, but can be initiated faster because there is less for the Operating System to set up. When a program consists of two or more threads, all the threads share a single memory space. Processes are given separate address spaces. all the threads share a single heap. But each thread is given its own stack.

线程是另一种将工作负载分割为独立执行流的机制。一个线程比一个进程轻。这意味着,它提供的灵活性比完全吹制的过程要小,但是可以更快地启动,因为操作系统的设置要少一些。当一个程序由两个或多个线程组成时,所有的线程共享一个内存空间。进程被给予单独的地址空间。所有线程共享一个堆。但是每个线程都有自己的堆栈。

#20


4  

Process: program under execution is known as process

Thread:  Thread is a functionality which is executed with the other part of the 
program     based on the concept of "one with other"

so thread is a part of process..

#21


4  

Coming from the embedded world, I would like to add that the concept of processes only exists in "big" processors (desktop CPUs, ARM Cortex A-9) that have MMU (memory management unit) , and operating systems that support using MMUs (such as Linux). With small/old processors and microcontrollers and small RTOS operating system (real time operating system), such as freeRTOS, there is no MMU support and thus no processes but only threads.

从嵌入式的世界来看,我想补充的是,流程的概念只存在于“大型”处理器(桌面cpu、ARM皮质A-9)中,这些处理器有MMU(内存管理单元),以及支持使用MMUs(如Linux)的操作系统。使用小的/旧的处理器和微控制器和小的RTOS操作系统(实时操作系统),例如freeRTOS,没有MMU支持,因此没有进程,只有线程。

Threads can access each others memory, and they are scheduled by OS in an interleaved manner so they appear to run in parallel (or with multi-core they really run in parallel).

线程可以访问彼此的内存,它们以交叉的方式被操作系统调度,因此它们看起来是并行运行的(或者是多核的,它们实际上是并行运行的)。

Processes, on the other hand, live in their private sandbox of virtual memory, provided and guarded by MMU. This is handy because it enables:

另一方面,进程生活在虚拟内存的私有沙箱中,由MMU提供和保护。这很方便,因为它使:

  1. keeping buggy process from crashing the entire system.
  2. 防止错误的过程破坏整个系统。
  3. Maintaining security by making other processes data invisible and unreachable. The actual work inside the process is taken care by one or more threads.
  4. 通过使其他进程数据不可见和不可访问来维护安全性。过程中的实际工作由一个或多个线程负责。

#22


4  

Trying to answer it from Linux Kernel's OS View

尝试从Linux内核的OS视图中回答它。

A program becomes a process when launched into memory. A process has its own address space meaning having various segments in memory such as .text segement for storing compiled code, .bss for storing uninitialized static or global variables, etc. Each process would have its own program counter and user-spcae stack. Inside kernel, each process would have its own kernel stack (which is separated from user space stack for security issues) and a structure named task_struct which is generally abstracted as the process control block, storing all the information regarding the process such as its priority, state,(and a whole lot of other chunk). A process can have multiple threads of execution.

程序在进入内存时就变成了一个进程。一个进程有它自己的地址空间,这意味着在内存中有不同的段,例如。text segement,用于存储已编译的代码,.bss用于存储未初始化的静态或全局变量,等等。每个进程都有自己的程序计数器和用户spcae栈。在内核中,每个过程都有自己的内核堆栈(用户空间栈分开安全问题)和一个名叫task_struct结构通常是抽象的进程控制块,存储过程有关的所有信息,如它的优先级,状态,(以及很多其他的块)。进程可以有多个执行线程。

Coming to threads, they reside inside a process and share the address space of the parent process along with other resources which can be passed during thread creation such as filesystem resources, sharing pending signals, sharing data(variables and instructions) therefore making threads lightweight and hence allowing faster context switching. Inside kernel, each thread has its own kernel stack along with the task_struct structure which defines the thread. Therefore kernel views threads of same process as different entities and are schedulable in themselves. Threads in same process share a common id called as thread group id(tgid), also they have a unique id called as the process id (pid).

在线程中,它们驻留在进程中,并与其他资源共享父进程的地址空间,这些资源可以在线程创建时传递,比如文件系统资源、共享挂起的信号、共享数据(变量和指令),从而使线程更轻量级,从而允许更快的上下文切换。在内核中,每个线程都有自己的内核堆栈,以及定义线程的task_struct结构。因此,内核将相同进程的线程视为不同的实体,并且是可调度的。同一进程中的线程共享一个名为thread group id(tgid)的通用id,它们也有一个惟一的id,称为进程id(pid)。

#23


3  

While building an algorithm in Python (interpreted language) that incorporated multi-threading I was surprised to see that execution time was not any better when compared to the sequential algorithm I had previously built. In an effort to understand the reason for this result I did some reading, and believe what I learned offers an interesting context from which to better understand the differences between multi-threading and multi-processes.

虽然在Python(解释语言)中构建了一个包含多线程的算法,但我很惊讶地发现,与我之前构建的顺序算法相比,执行时间并没有任何改善。为了理解这个结果的原因,我做了一些阅读,并且相信我所学到的东西提供了一个有趣的上下文,以便更好地理解多线程和多进程之间的区别。

Multi-core systems may exercise multiple threads of execution, and so Python should support multi-threading. But Python is not a compiled language and instead is an interpreted language1. This means that the program must be interpreted in order to run, and the interpreter is not aware of the program before it begins execution. What it does know, however, are the rules of Python and it then dynamically applies those rules. Optimizations in Python must then be principally optimizations of the interpreter itself, and not the code that is to be run. This is in contrast to compiled languages such as C++, and has consequences for multi-threading in Python. Specifically, Python uses the Global Interpreter Lock to manage multi-threading.

多核系统可以执行多个执行线程,因此Python应该支持多线程。但是Python不是一种编译语言,而是一种解释语言。这意味着程序必须被解释为运行,而解释器在开始执行之前不知道程序。但是,它所知道的是Python的规则,然后动态地应用这些规则。在Python中优化必须主要是对解释器本身的优化,而不是要运行的代码。这与编译语言(如c++)形成了对比,并对Python中的多线程产生了影响。具体来说,Python使用全局解释器锁来管理多线程。

On the other hand a compiled language is, well, compiled. The program is processed "entirely", where first it is interpreted according to its syntactical definitions, then mapped to a language agnostic intermediate representation, and finally linked into an executable code. This process allows the code to be highly optimized because it is all available at the time of compilation. The various program interactions and relationships are defined at the time the executable is created and robust decisions about optimization can be made.

另一方面,编译好的语言是编译好的。该程序是“完全”处理的,首先根据其语法定义进行解释,然后映射到一种语言不可知的中间表示,最后链接到可执行代码中。这个过程允许代码高度优化,因为它在编译时都可用。在创建可执行文件时,定义了各种程序交互和关系,并可以做出关于优化的健壮决策。

In modern environments Python's interpreter must permit multi-threading, and this must both be safe and efficient. This is where the difference between being an interpreted language versus a compiled language enters the picture. The interpreter must not to disturb internally shared data from different threads, while at the same time optimizing the use of processors for computations.

在现代环境中,Python的解释器必须允许多线程,这必须既安全又高效。这就是解释语言与编译语言之间的区别。解释器不能干扰来自不同线程的内部共享数据,同时优化计算程序的使用。

As has been noted in the previous posts both a process and a thread are independent sequential executions with the primary difference being that memory is shared across multiple threads of a process, while processes isolate their memory spaces.

正如前面的文章所指出的,一个进程和一个线程都是独立的顺序执行,主要的区别是内存是在进程的多个线程之间共享的,而进程则隔离了它们的内存空间。

In Python data is protected from simultaneous access by different threads by the Global Interpreter Lock. It requires that in any Python program only one thread can be executed at any time. On the other hand it is possible to run multiple processes since the memory for each process is isolated from any other process, and processes can run on multiple cores.

在Python数据中,由全局解释器锁保护不同线程同时访问。它要求在任何Python程序中,只有一个线程可以在任何时候执行。另一方面,可以运行多个进程,因为每个进程的内存与任何其他进程隔离,进程可以在多个内核上运行。


1 Donald Knuth has a good explanation of interpretive routines in The Art of Computer Programming: Fundamental Algorithms.

Donald Knuth对计算机编程艺术中的解释例程有很好的解释:基本算法。

#24


2  

A process is an executing instance of an application.

进程是应用程序的执行实例。

What does that mean? Well, for example, when you double-click the Microsoft Word icon, you start a process that runs Word.

这是什么意思?例如,当你双击Microsoft Word图标时,你会启动一个运行Word的进程。

A thread is a path of execution within a process. Also, a process can contain multiple threads.

线程是进程内执行的路径。此外,一个进程可以包含多个线程。

When you start Word, the operating system creates a process and begins executing the primary thread of that process.

当您启动Word时,操作系统创建一个进程并开始执行该进程的主线程。

#25


2  

Threads within the same process share the Memory, but each thread has its own stack and registers, and threads store thread-specific data in the heap. Threads never execute independently, so the inter-thread communication is much faster when compared to inter-process communication.

相同进程中的线程共享内存,但是每个线程都有自己的堆栈和寄存器,并且线程在堆中存储线程特定的数据。线程从不独立执行,因此与进程间通信相比,线程间通信要快得多。

Processes never share the same memory. When a child process creates it duplicates the memory location of the parent process. Process communication is done by using pipe, shared memory, and message parsing. Context switching between threads is very slow.

进程从不共享相同的内存。当子进程创建时,它将复制父进程的内存位置。过程通信是通过使用管道、共享内存和消息解析完成的。线程之间的上下文切换非常缓慢。

#26


2  

They are almost as same... But the key difference is a thread is lightweight and a process is heavy-weight in terms of context switching, work load and so on.

它们几乎一样……但关键的区别在于线程是轻量级的,而进程在上下文切换、工作负载等方面是重量级的。

#27


2  

The best short-ish definition I've seen so far comes from Michael Kerrisk's 'The Linux Programming Interface':

迄今为止我见过的最好的简短定义来自Michael Kerrisk的“Linux编程接口”:

In modern UNIX implementations, each process can have multiple threads of execution. One way of envisaging threads is as a set of processes that share the same virtual memory, as well as a range of other attributes. Each thread is executing the same program code and shares the same data area and heap. However, each thread has it own stack containing local variables and function call linkage information. [LPI 2.12]

在现代的UNIX实现中,每个进程可以有多个执行线程。设想线程的一种方法是一组共享相同虚拟内存的进程,以及其他一些属性。每个线程执行相同的程序代码并共享相同的数据区域和堆。但是,每个线程都有自己的堆栈,其中包含局部变量和函数调用链接信息。(LPI 2.12)

#28


1  

Example 1: A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety.

示例1:JVM在一个进程中运行,而JVM中的线程共享属于该进程的堆。这就是为什么几个线程可以访问同一个对象的原因。线程共享堆,并拥有自己的堆栈空间。这是一个线程对方法及其局部变量的调用,使线程安全地从其他线程中运行。但是堆不是线程安全的,并且必须与线程安全同步。

#29


1  

Consider process like a unit of ownership or what resources are needed by a task. A Process can have resources like memory space, specific input/output, specific files, and priority etc.

考虑过程,比如所有权的单位或任务需要什么资源。一个进程可以拥有像内存空间、特定输入/输出、特定文件和优先级等资源。

A thread is a dispatchable unit of execution or in simple words the progress through a sequence of instructions

一个线程是一个可分派的执行单元,或者简单地说,通过一系列指令的进展。

#30


1  

A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files and signals. A traditional (or heavyweight) process has a single thread of control. Tf a process has multiple threads of control, it can perform more than one task at a time

线程是CPU利用率的基本单位;它包括一个线程ID、一个程序计数器、一个寄存器集和一个堆栈。它与属于同一进程的其他线程共享其代码段、数据部分和其他操作系统资源,比如打开的文件和信号。传统的(或重量级的)进程有一个单一的控制线程。一个进程有多个控制线程,它可以一次执行多个任务。

#1


1043  

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).

我不确定您可能指的是什么“硬件”vs“软件”线程。线程是一个操作环境特性,而不是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使用“进程”这个术语,因为它不公开共享内存的多编程模型。称它们为“线程”将意味着它们拥有共享内存。

#2


604  

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.

线程是可以被调度执行的进程中的一个实体。进程的所有线程共享其虚拟地址空间和系统资源。此外,每个线程都维护异常处理程序、调度优先级、线程本地存储、惟一线程标识符,以及系统将用于保存线程上下文的一组结构,直到它被调度为止。线程上下文包括线程的机器寄存器组、内核堆栈、线程环境块和线程进程地址空间中的用户堆栈。线程还可以拥有自己的安全上下文,可用于模拟客户端。


Found this on MSDN here:
About Processes and Threads

在MSDN上找到这个:关于进程和线程。

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支持抢占式多任务处理,它创建多个进程同时执行多个线程的效果。在多处理器计算机上,系统可以同时执行许多线程,因为计算机上有处理器。

#3


208  

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 withing the process gains unprecedented importance.
  • 由于进程的线程共享相同的内存,因此同步访问共享数据的访问将获得前所未有的重要性。

I borrowed the above info from the Knowledge Quest! blog.

我从知识探索中借用了上述信息!博客。

#4


82  

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 Model in Modern Operating Systems 3e by Tanenbaum:

我们从第2.2.2节中得到了现代操作系统的经典线程模型:Tanenbaum:

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 multithreading issue. 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.

现在让我们看看软件方面。基本上有三种方法可以在软件方面实现线程。

  1. Userspace Threads
  2. 用户空间线程
  3. Kernel Threads
  4. 内核线程
  5. A combination of the two
  6. 两者的结合。

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".

回到你的术语问题,你可以看到一个过程和一个执行的线程是两个不同的概念你选择哪个术语取决于你说的是什么。关于“轻量级过程”这个术语,我个人并不认为它有什么意义,因为它并没有真正地传达“执行的线程”这一术语。

#5


39  

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.

应用程序由一个或多个进程组成。一个过程,用最简单的术语来说,就是一个执行程序。一个或多个线程在进程的上下文中运行。线程是操作系统分配处理器时间的基本单元。线程可以执行过程代码的任何部分,包括当前由另一个线程执行的部分。光纤是一个执行单元,必须由应用程序手动安排。纤维在调度它们的线程的上下文中运行。

Stolen from here.

从这里偷来的。

#6


35  

To explain more with respect to concurrent programming

更多地解释并发编程。

  1. 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.

    流程有一个自包含的执行环境。一个过程通常有一个完整的、私有的基本运行时资源集;特别是,每个进程都有自己的内存空间。

  2. 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.

在你的电脑上,打开微软Word和网页浏览器。我们称之为两个过程。

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.

在微软word中,你输入一些东西,然后自动保存。现在,您可以观察到编辑和保存在一个线程上并行编辑,并在另一个线程上保存。

#7


22  

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.

进程是代码、内存、数据和其他资源的集合。线程是在流程的范围内执行的代码序列。您可以(通常)在同一个进程中同时执行多个线程。

#8


20  

  • Every process is a thread (primary thread).
  • 每个进程都是一个线程(主线程)。
  • But every thread is not a process. It is a part(entity) of a process.
  • 但是每个线程都不是一个过程。它是一个过程的一部分(实体)。

#9


16  

A process is an executing instance of an application. What does that mean? Well, for example, when you double-click the Microsoft Word icon, you start a process that runs Word. A thread is a path of execution within a process. Also, a process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.

进程是应用程序的执行实例。这是什么意思?例如,当你双击Microsoft Word图标时,你会启动一个运行Word的进程。线程是进程内执行的路径。此外,一个进程可以包含多个线程。当您启动Word时,操作系统创建一个进程并开始执行该进程的主线程。

It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.

需要注意的是,一个线程可以做任何一个进程可以做的事情。但是由于一个进程可以由多个线程组成,所以线程可以被认为是一个“轻量级”过程。因此,线程和进程之间的本质区别是每个线程都用来完成的工作。线程用于小任务,而进程用于更多的“重量级”任务——基本上是应用程序的执行。

Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.

线程和进程之间的另一个区别是,同一个进程中的线程共享相同的地址空间,而不同的进程没有共享相同的地址空间。这允许线程读取和写入相同的数据结构和变量,并促进线程之间的通信。进程之间的通信——也称为IPC,或进程间通信——是相当困难和资源密集的。

#10


12  

Process:

过程:

  1. Process is a heavy weight process.
  2. 过程是一个很重的过程。
  3. Process is a separate program that has separate memory,data,resources ect.
  4. 进程是一个单独的程序,它有单独的内存、数据和资源等。
  5. Process are created using fork() method.
  6. 过程是使用fork()方法创建的。
  7. Context switch between the process is time consuming.
  8. 过程之间的上下文切换非常耗时。

Example:
Say, opening any browser (mozilla, Chrome, IE). At this point new process will start to execute.

例如:打开任何浏览器(mozilla, Chrome, IE)。此时,新流程将开始执行。

Threads:

线程:

  1. Threads are light weight processes.Threads are bundled inside the process.
  2. 线程是轻量级的过程。线程在过程中被绑定。
  3. Threads have a shared memory,data,resources,files etc.
  4. 线程具有共享内存、数据、资源、文件等。
  5. Threads are created using clone() method.
  6. 线程是使用clone()方法创建的。
  7. Context switch between the threads are not much time consuming as Process.
  8. 线程之间的上下文切换不太耗费时间。

Example:
Opening multiple tabs in the browser.

示例:在浏览器中打开多个选项卡。

#11


11  

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.

进程和线程都是独立的执行序列。典型的区别是,线程(同一进程)在共享内存空间中运行,而进程在单独的内存空间中运行。

Process

过程

Is a program in execution. it has text section i.e the program code, current activity as represented by the value of program counter & content of processors register. It also includes the process stack that contains temporary data(such as function parameters, return addressed and local variables), and a data section, which contains global variables. A process may also include a heap, which is memory that is dynamically allocated during process run time.

是一个执行中的程序。它有text section i。程序代码,当前活动以程序计数器的值和处理器寄存器的内容为代表。它还包括包含临时数据的过程堆栈(例如函数参数、返回地址和局部变量)和一个包含全局变量的数据部分。进程还可能包含堆,这是在进程运行时动态分配的内存。

Thread

线程

A thread is a basic unit of CPU utilisation; it comprises a thread ID, a program counter, register set, and a stack. it shared with other threads belonging to the same process its code section, data section and other operating system resources such as open files and signals.

线程是CPU使用率的基本单位;它包括线程ID、程序计数器、寄存器组和堆栈。它与属于同一进程的其他线程共享它的代码段、数据段和其他操作系统资源,比如打开的文件和信号。

-- Taken from Operating System by Galvin

——由加尔文的操作系统。

#12


11  

Real world example for Process and Thread This will give you the basic idea about thread and process 进程和线程的区别是什么?

这个过程和线程的实际示例将给您提供关于线程和进程的基本思想。

I borrowed the above info from Scott Langham's Answer - thanks

我从Scott Langham的回答中借用了以上的信息——谢谢。

#13


9  

Trying to answer this question relating to Java world.

试图回答与Java世界有关的问题。

A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process.

进程是程序的执行,但线程是流程中的单个执行序列。一个进程可以包含多个线程。线程有时被称为轻量级进程。

For example:

例如:

Example 1: A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety.

示例1:JVM在一个进程中运行,而JVM中的线程共享属于该进程的堆。这就是为什么几个线程可以访问同一个对象的原因。线程共享堆,并拥有自己的堆栈空间。这是一个线程对方法及其局部变量的调用,使线程安全地从其他线程中运行。但是堆不是线程安全的,并且必须与线程安全同步。

Example 2: A program might not be able to draw pictures by reading keystrokes. The program must give its full attention to the keyboard input and lacking the ability to handle more than one event at a time will lead to trouble. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this. Here Drawing picture is a process and reading keystroke is sub process (thread).

例2:一个程序可能不能通过读击键来绘制图片。该程序必须充分注意键盘输入,同时缺乏处理多个事件的能力将导致麻烦。这个问题的理想解决方案是同时无缝地执行两个或多个程序段。线程允许我们这样做。这里绘图是一个过程,阅读击键是子进程(线程)。

#14


8  

Both threads and processes are atomic units of OS resource allocation (i.e. there is a concurrency model describing how CPU time is divided between them, and the model of owning other OS resources). There is a difference in:

线程和进程都是OS资源分配的原子单元(也就是说,有一个描述CPU时间是如何在它们之间分配的并发模型,以及拥有其他OS资源的模型)。有一个区别:

  • Shared resources (threads are sharing memory by definition, they do not own anything except stack and local variables; processes could also share memory, but there is a separate mechanism for that, maintained by OS)
  • 共享资源(线程按照定义共享内存,除了堆栈和局部变量之外,它们不拥有任何东西;进程也可以共享内存,但是有一个单独的机制,由操作系统维护。
  • Allocation space (kernel space for processes vs. user space for threads)
  • 分配空间(进程的内核空间和线程的用户空间)

Greg Hewgill above was correct about the Erlang meaning of the word "process", and here there's a discussion of why Erlang could do processes lightweight.

上面的Greg Hewgill对“过程”这个词的Erlang含义是正确的,这里有一个关于Erlang为什么可以做轻量级过程的讨论。

#15


8  

Difference between Thread and Process?

线程与进程的区别?

A process is an executing instance of an application and A thread is a path of execution within a process. Also, a process can contain multiple threads.It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.

进程是应用程序的执行实例,而线程是流程中的执行路径。此外,一个进程可以包含多个线程。需要注意的是,一个线程可以做任何一个进程可以做的事情。但是由于一个进程可以由多个线程组成,所以线程可以被认为是一个“轻量级”过程。因此,线程和进程之间的本质区别是每个线程都用来完成的工作。线程用于小任务,而进程用于更多的“重量级”任务——基本上是应用程序的执行。

Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.

线程和进程之间的另一个区别是,同一个进程中的线程共享相同的地址空间,而不同的进程没有共享相同的地址空间。这允许线程读取和写入相同的数据结构和变量,并促进线程之间的通信。进程之间的通信——也称为IPC,或进程间通信——是相当困难和资源密集的。

Here’s a summary of the differences between threads and processes:

下面是线程和进程之间的差异的总结:

  1. Threads are easier to create than processes since they don't require a separate address space.

    线程比进程更容易创建,因为它们不需要单独的地址空间。

  2. Multithreading requires careful programming since threads share data strucures that should only be modified by one thread at a time. Unlike threads, processes don't share the same address space.

    多线程需要仔细的编程,因为线程共享数据结构,而这些数据结构一次只能被一个线程修改。与线程不同,进程不共享相同的地址空间。

  3. Threads are considered lightweight because they use far less resources than processes.

    线程被认为是轻量级的,因为它们使用的资源远远少于流程。

  4. Processes are independent of each other. Threads, since they share the same address space are interdependent, so caution must be taken so that different threads don't step on each other.
    This is really another way of stating #2 above.

    过程是相互独立的。线程,因为它们共享同一个地址空间是相互依赖的,所以必须谨慎,这样不同的线程才不会彼此同步。这是另一种表述2的方法。

  5. A process can consist of multiple threads.

    一个进程可以由多个线程组成。

#16


7  

From the point of view of an interviewer, there are basically just 3 main things that I want to hear, besides obvious things like a process can have multiple threads:

从面试官的角度来看,基本上只有三件事是我想听的,除了一些显而易见的事情,比如一个过程可以有多个线程:

  1. Threads share same memory space, which means a thread can access memory from other's thread memory. Processes normally can not.
  2. 线程共享相同的内存空间,这意味着线程可以从其他线程内存访问内存。过程通常不能。
  3. Resources. Resources (memory, handles, sockets, etc) are release at process termination, not thread termination.
  4. 资源。资源(内存、句柄、套接字等)在进程终止时释放,而不是线程终止。
  5. Security. A process has a fixed security token. A thread, on the other hand, can impersonate different users/tokens.
  6. 安全。进程有一个固定的安全令牌。另一方面,线程可以模拟不同的用户/令牌。

If you want more, Scott Langham's response pretty much covers everything. All these are from the perspective of an operating system. Different languages can implement different concepts, like tasks, light-wigh threads and so on, but they are just ways of using threads (of fibers on Windows). There are no hardware and software threads. There are hardware and software exceptions and interrupts, or user-mode and kernel threads.

如果你想要更多,斯科特·朗廷的回答几乎涵盖了一切。所有这些都是从操作系统的角度来看的。不同的语言可以实现不同的概念,比如任务、光导线程等等,但它们只是使用线程(在Windows上的纤维)的方法。没有硬件和软件线程。有硬件和软件异常和中断,或用户模式和内核线程。

#17


7  

  1. Basically, a thread is a part of a process without process thread wouldn't able to work.
  2. 基本上,线程是进程的一部分,没有进程线程无法工作。
  3. A thread is lightweight whereas the process is heavyweight.
  4. 线程是轻量级的,而过程是重量级的。
  5. communication between process requires some Time whereas thread requires less time.
  6. 进程之间的通信需要一些时间,而线程需要的时间更少。
  7. Threads can share the same memory area whereas process lives in separate.
  8. 线程可以共享相同的内存区域,而进程则是分开的。

#18


5  

  1. A thread runs in a shared memory space, but a process runs in a separate memory space
  2. 线程在共享内存空间中运行,但是进程在一个单独的内存空间中运行。
  3. A thread is a light-weight process, but a process is a heavy-weight process.
  4. 一个线程是一个轻量级过程,但是过程是一个重权过程。
  5. A thread is a subtype of process.
  6. 线程是进程的子类型。

#19


5  

The following is what I got from one of the articles on The Code Project. I guess it explains everything needed clearly.

下面是我从代码项目的一篇文章中得到的内容。我想它能清楚地解释一切。

A thread is another mechanism for splitting the workload into separate execution streams. A thread is lighter weight than a process. This means, it offers less flexibility than a full blown process, but can be initiated faster because there is less for the Operating System to set up. When a program consists of two or more threads, all the threads share a single memory space. Processes are given separate address spaces. all the threads share a single heap. But each thread is given its own stack.

线程是另一种将工作负载分割为独立执行流的机制。一个线程比一个进程轻。这意味着,它提供的灵活性比完全吹制的过程要小,但是可以更快地启动,因为操作系统的设置要少一些。当一个程序由两个或多个线程组成时,所有的线程共享一个内存空间。进程被给予单独的地址空间。所有线程共享一个堆。但是每个线程都有自己的堆栈。

#20


4  

Process: program under execution is known as process

Thread:  Thread is a functionality which is executed with the other part of the 
program     based on the concept of "one with other"

so thread is a part of process..

#21


4  

Coming from the embedded world, I would like to add that the concept of processes only exists in "big" processors (desktop CPUs, ARM Cortex A-9) that have MMU (memory management unit) , and operating systems that support using MMUs (such as Linux). With small/old processors and microcontrollers and small RTOS operating system (real time operating system), such as freeRTOS, there is no MMU support and thus no processes but only threads.

从嵌入式的世界来看,我想补充的是,流程的概念只存在于“大型”处理器(桌面cpu、ARM皮质A-9)中,这些处理器有MMU(内存管理单元),以及支持使用MMUs(如Linux)的操作系统。使用小的/旧的处理器和微控制器和小的RTOS操作系统(实时操作系统),例如freeRTOS,没有MMU支持,因此没有进程,只有线程。

Threads can access each others memory, and they are scheduled by OS in an interleaved manner so they appear to run in parallel (or with multi-core they really run in parallel).

线程可以访问彼此的内存,它们以交叉的方式被操作系统调度,因此它们看起来是并行运行的(或者是多核的,它们实际上是并行运行的)。

Processes, on the other hand, live in their private sandbox of virtual memory, provided and guarded by MMU. This is handy because it enables:

另一方面,进程生活在虚拟内存的私有沙箱中,由MMU提供和保护。这很方便,因为它使:

  1. keeping buggy process from crashing the entire system.
  2. 防止错误的过程破坏整个系统。
  3. Maintaining security by making other processes data invisible and unreachable. The actual work inside the process is taken care by one or more threads.
  4. 通过使其他进程数据不可见和不可访问来维护安全性。过程中的实际工作由一个或多个线程负责。

#22


4  

Trying to answer it from Linux Kernel's OS View

尝试从Linux内核的OS视图中回答它。

A program becomes a process when launched into memory. A process has its own address space meaning having various segments in memory such as .text segement for storing compiled code, .bss for storing uninitialized static or global variables, etc. Each process would have its own program counter and user-spcae stack. Inside kernel, each process would have its own kernel stack (which is separated from user space stack for security issues) and a structure named task_struct which is generally abstracted as the process control block, storing all the information regarding the process such as its priority, state,(and a whole lot of other chunk). A process can have multiple threads of execution.

程序在进入内存时就变成了一个进程。一个进程有它自己的地址空间,这意味着在内存中有不同的段,例如。text segement,用于存储已编译的代码,.bss用于存储未初始化的静态或全局变量,等等。每个进程都有自己的程序计数器和用户spcae栈。在内核中,每个过程都有自己的内核堆栈(用户空间栈分开安全问题)和一个名叫task_struct结构通常是抽象的进程控制块,存储过程有关的所有信息,如它的优先级,状态,(以及很多其他的块)。进程可以有多个执行线程。

Coming to threads, they reside inside a process and share the address space of the parent process along with other resources which can be passed during thread creation such as filesystem resources, sharing pending signals, sharing data(variables and instructions) therefore making threads lightweight and hence allowing faster context switching. Inside kernel, each thread has its own kernel stack along with the task_struct structure which defines the thread. Therefore kernel views threads of same process as different entities and are schedulable in themselves. Threads in same process share a common id called as thread group id(tgid), also they have a unique id called as the process id (pid).

在线程中,它们驻留在进程中,并与其他资源共享父进程的地址空间,这些资源可以在线程创建时传递,比如文件系统资源、共享挂起的信号、共享数据(变量和指令),从而使线程更轻量级,从而允许更快的上下文切换。在内核中,每个线程都有自己的内核堆栈,以及定义线程的task_struct结构。因此,内核将相同进程的线程视为不同的实体,并且是可调度的。同一进程中的线程共享一个名为thread group id(tgid)的通用id,它们也有一个惟一的id,称为进程id(pid)。

#23


3  

While building an algorithm in Python (interpreted language) that incorporated multi-threading I was surprised to see that execution time was not any better when compared to the sequential algorithm I had previously built. In an effort to understand the reason for this result I did some reading, and believe what I learned offers an interesting context from which to better understand the differences between multi-threading and multi-processes.

虽然在Python(解释语言)中构建了一个包含多线程的算法,但我很惊讶地发现,与我之前构建的顺序算法相比,执行时间并没有任何改善。为了理解这个结果的原因,我做了一些阅读,并且相信我所学到的东西提供了一个有趣的上下文,以便更好地理解多线程和多进程之间的区别。

Multi-core systems may exercise multiple threads of execution, and so Python should support multi-threading. But Python is not a compiled language and instead is an interpreted language1. This means that the program must be interpreted in order to run, and the interpreter is not aware of the program before it begins execution. What it does know, however, are the rules of Python and it then dynamically applies those rules. Optimizations in Python must then be principally optimizations of the interpreter itself, and not the code that is to be run. This is in contrast to compiled languages such as C++, and has consequences for multi-threading in Python. Specifically, Python uses the Global Interpreter Lock to manage multi-threading.

多核系统可以执行多个执行线程,因此Python应该支持多线程。但是Python不是一种编译语言,而是一种解释语言。这意味着程序必须被解释为运行,而解释器在开始执行之前不知道程序。但是,它所知道的是Python的规则,然后动态地应用这些规则。在Python中优化必须主要是对解释器本身的优化,而不是要运行的代码。这与编译语言(如c++)形成了对比,并对Python中的多线程产生了影响。具体来说,Python使用全局解释器锁来管理多线程。

On the other hand a compiled language is, well, compiled. The program is processed "entirely", where first it is interpreted according to its syntactical definitions, then mapped to a language agnostic intermediate representation, and finally linked into an executable code. This process allows the code to be highly optimized because it is all available at the time of compilation. The various program interactions and relationships are defined at the time the executable is created and robust decisions about optimization can be made.

另一方面,编译好的语言是编译好的。该程序是“完全”处理的,首先根据其语法定义进行解释,然后映射到一种语言不可知的中间表示,最后链接到可执行代码中。这个过程允许代码高度优化,因为它在编译时都可用。在创建可执行文件时,定义了各种程序交互和关系,并可以做出关于优化的健壮决策。

In modern environments Python's interpreter must permit multi-threading, and this must both be safe and efficient. This is where the difference between being an interpreted language versus a compiled language enters the picture. The interpreter must not to disturb internally shared data from different threads, while at the same time optimizing the use of processors for computations.

在现代环境中,Python的解释器必须允许多线程,这必须既安全又高效。这就是解释语言与编译语言之间的区别。解释器不能干扰来自不同线程的内部共享数据,同时优化计算程序的使用。

As has been noted in the previous posts both a process and a thread are independent sequential executions with the primary difference being that memory is shared across multiple threads of a process, while processes isolate their memory spaces.

正如前面的文章所指出的,一个进程和一个线程都是独立的顺序执行,主要的区别是内存是在进程的多个线程之间共享的,而进程则隔离了它们的内存空间。

In Python data is protected from simultaneous access by different threads by the Global Interpreter Lock. It requires that in any Python program only one thread can be executed at any time. On the other hand it is possible to run multiple processes since the memory for each process is isolated from any other process, and processes can run on multiple cores.

在Python数据中,由全局解释器锁保护不同线程同时访问。它要求在任何Python程序中,只有一个线程可以在任何时候执行。另一方面,可以运行多个进程,因为每个进程的内存与任何其他进程隔离,进程可以在多个内核上运行。


1 Donald Knuth has a good explanation of interpretive routines in The Art of Computer Programming: Fundamental Algorithms.

Donald Knuth对计算机编程艺术中的解释例程有很好的解释:基本算法。

#24


2  

A process is an executing instance of an application.

进程是应用程序的执行实例。

What does that mean? Well, for example, when you double-click the Microsoft Word icon, you start a process that runs Word.

这是什么意思?例如,当你双击Microsoft Word图标时,你会启动一个运行Word的进程。

A thread is a path of execution within a process. Also, a process can contain multiple threads.

线程是进程内执行的路径。此外,一个进程可以包含多个线程。

When you start Word, the operating system creates a process and begins executing the primary thread of that process.

当您启动Word时,操作系统创建一个进程并开始执行该进程的主线程。

#25


2  

Threads within the same process share the Memory, but each thread has its own stack and registers, and threads store thread-specific data in the heap. Threads never execute independently, so the inter-thread communication is much faster when compared to inter-process communication.

相同进程中的线程共享内存,但是每个线程都有自己的堆栈和寄存器,并且线程在堆中存储线程特定的数据。线程从不独立执行,因此与进程间通信相比,线程间通信要快得多。

Processes never share the same memory. When a child process creates it duplicates the memory location of the parent process. Process communication is done by using pipe, shared memory, and message parsing. Context switching between threads is very slow.

进程从不共享相同的内存。当子进程创建时,它将复制父进程的内存位置。过程通信是通过使用管道、共享内存和消息解析完成的。线程之间的上下文切换非常缓慢。

#26


2  

They are almost as same... But the key difference is a thread is lightweight and a process is heavy-weight in terms of context switching, work load and so on.

它们几乎一样……但关键的区别在于线程是轻量级的,而进程在上下文切换、工作负载等方面是重量级的。

#27


2  

The best short-ish definition I've seen so far comes from Michael Kerrisk's 'The Linux Programming Interface':

迄今为止我见过的最好的简短定义来自Michael Kerrisk的“Linux编程接口”:

In modern UNIX implementations, each process can have multiple threads of execution. One way of envisaging threads is as a set of processes that share the same virtual memory, as well as a range of other attributes. Each thread is executing the same program code and shares the same data area and heap. However, each thread has it own stack containing local variables and function call linkage information. [LPI 2.12]

在现代的UNIX实现中,每个进程可以有多个执行线程。设想线程的一种方法是一组共享相同虚拟内存的进程,以及其他一些属性。每个线程执行相同的程序代码并共享相同的数据区域和堆。但是,每个线程都有自己的堆栈,其中包含局部变量和函数调用链接信息。(LPI 2.12)

#28


1  

Example 1: A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety.

示例1:JVM在一个进程中运行,而JVM中的线程共享属于该进程的堆。这就是为什么几个线程可以访问同一个对象的原因。线程共享堆,并拥有自己的堆栈空间。这是一个线程对方法及其局部变量的调用,使线程安全地从其他线程中运行。但是堆不是线程安全的,并且必须与线程安全同步。

#29


1  

Consider process like a unit of ownership or what resources are needed by a task. A Process can have resources like memory space, specific input/output, specific files, and priority etc.

考虑过程,比如所有权的单位或任务需要什么资源。一个进程可以拥有像内存空间、特定输入/输出、特定文件和优先级等资源。

A thread is a dispatchable unit of execution or in simple words the progress through a sequence of instructions

一个线程是一个可分派的执行单元,或者简单地说,通过一系列指令的进展。

#30


1  

A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files and signals. A traditional (or heavyweight) process has a single thread of control. Tf a process has multiple threads of control, it can perform more than one task at a time

线程是CPU利用率的基本单位;它包括一个线程ID、一个程序计数器、一个寄存器集和一个堆栈。它与属于同一进程的其他线程共享其代码段、数据部分和其他操作系统资源,比如打开的文件和信号。传统的(或重量级的)进程有一个单一的控制线程。一个进程有多个控制线程,它可以一次执行多个任务。