线程/进程/任务之间有什么区别?
线程/进程/任务之间有什么区别?
What is the difference between a thread/process/task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
线程/进程/任务之间有什么区别?
What is the difference between a thread/process/task?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
进程:
进程是正在执行的计算机程序的实例。
它包含程序代码及其当前活动。
根据操作系统 (OS),进程可能由同时执行指令的多个执行线程组成。
基于进程的多任务处理使您能够在使用文本编辑器的同时运行 Java 编译器。
在使用单个CPU的多个进程时,使用不同存储器上下文之间的上下文切换。
每个进程都有一套完整的自己的变量。
线程:
线程是CPU使用的基本单位,由程序计数器、堆栈和一组寄存器组成。
执行线程由计算机程序的分支产生两个或多个并发运行的任务。
线程和进程的实现因操作系统而异,但在大多数情况下,线程包含在进程内。多个线程可以存在于同一个进程中并共享内存等资源,而不同的进程不共享这些资源。
同一进程中的线程示例是自动拼写检查和写入时自动保存文件。
线程基本上是在同一内存上下文中运行的进程。
线程在执行时可以共享相同的数据。
线程图,即单线程与多线程
任务:
任务是一组加载的程序指令记忆中。
Process:
A process is an instance of a computer program that is being executed.
It contains the program code and its current activity.
Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.
Process-based multitasking enables you to run the Java compiler at the same time that you are using a text editor.
In employing multiple processes with a single CPU,context switching between various memory context is used.
Each process has a complete set of its own variables.
Thread:
A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of registers.
A thread of execution results from a fork of a computer program into two or more concurrently running tasks.
The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
Example of threads in same process is automatic spell check and automatic saving of a file while writing.
Threads are basically processes that run in the same memory context.
Threads may share the same data while execution.
Thread Diagram i.e. single thread vs multiple threads
Task:
A task is a set of program instructions that are loaded in memory.
简短回答:
线程是一个调度概念,它是 CPU 实际“运行”的内容(您不运行进程)。一个进程至少需要一个 CPU/OS 执行的线程。
流程是数据组织概念。为进程分配资源(例如,用于保存状态的存储器、允许的地址空间等)。
Short answer:
A thread is a scheduling concept, it's what the CPU actually 'runs' (you don't run a process). A process needs at least one thread that the CPU/OS executes.
A process is data organizational concept. Resources (e.g. memory for holding state, allowed address space, etc) are allocated for a process.
用更简单的术语解释
进程:进程是一组指令,作为对相关数据进行操作的代码,进程有自己的各种状态,睡眠,运行,停止等。当程序加载到内存中时,它变成过程。当分配CPU时,每个进程至少有一个线程,称为单线程程序。
线程:线程是进程的一部分。进程中可以存在多个线程。线程有自己的程序区和内存区。一个进程内的多个线程不能互相访问数据。进程必须处理线程同步才能实现所需的行为。
任务:任务并不是在世界范围内广泛使用的概念。当程序指令加载到内存中时,人们确实将其称为进程或任务。如今,任务和流程是同义词。
To explain on simpler terms
Process: process is the set of instruction as code which operates on related data and process has its own various state, sleeping, running, stopped etc. when program gets loaded into memory it becomes process. Each process has atleast one thread when CPU is allocated called sigled threaded program.
Thread: thread is a portion of the process. more than one thread can exist as part of process. Thread has its own program area and memory area. Multiple threads inside one process can not access each other data. Process has to handle sycnhronization of threads to achieve the desirable behaviour.
Task: Task is not widely concept used worldwide. when program instruction is loaded into memory people do call as process or task. Task and Process are synonyms nowadays.
进程
调用或启动程序。它是一个程序的实例,可以是多个并且运行相同的应用程序。线程
是进程内的最小执行单元。一个进程可以运行多个线程。线程的执行会产生任务。因此,在多线程环境中,就会发生多线程。执行中的
程序
称为进程
。一个程序可以有任意数量的进程。每个进程都有自己的地址空间。线程使用进程的地址空间。线程和进程的区别在于,当CPU从一个进程切换到另一个进程时,需要将当前信息保存在进程描述符中,并加载新进程的信息。从一个线程切换到另一个线程很简单。
任务
只是加载到内存中的一组指令。线程本身可以将自己分成两个或多个同时运行的任务。有关更多了解,请参阅链接:http://www.careerride.com/操作系统线程进程和任务.aspx
A
process
invokes or initiates a program. It is an instance of a program that can be multiple and running the same application. Athread
is the smallest unit of execution that lies within the process. A process can have multiple threads running. An execution of thread results in a task. Hence, in a multithreading environment, multithreading takes place.A
program
in execution is known asprocess
. A program can have any number of processes. Every process has its own address space.Threads uses address spaces of the process. The difference between a thread and a process is, when the CPU switches from one process to another the current information needs to be saved in Process Descriptor and load the information of a new process. Switching from one thread to another is simple.
A
task
is simply a set of instructions loaded into the memory. Threads can themselves split themselves into two or more simultaneously running tasks.for more Understanding refer the link: http://www.careerride.com/os-thread-process-and-task.aspx
来自wiki的清晰解释
1:1(内核级线程)
用户创建的线程与内核中的可调度实体一一对应。 [3]这是最简单的线程实现。 Win32 从一开始就使用了这种方法。在 Linux 上,通常的 C 库实现了这种方法(通过 NPTL 或更旧的 LinuxThreads)。 Solaris、NetBSD 和 FreeBSD 使用相同的方法。
N:1(用户级线程)
N:1 模型意味着所有应用程序级线程映射到单个内核级调度实体;[3] 内核不了解应用程序线程。通过这种方法,上下文切换可以非常快速地完成,此外,甚至可以在不支持线程的简单内核上实现。然而,主要缺点之一是它无法从多线程处理器或多处理器计算机上的硬件加速中受益:同时调度的线程永远不会超过一个。 [3]例如:如果其中一个线程需要执行I/O请求,那么整个进程就会被阻塞,线程优势就无法发挥出来。 GNU 可移植线程使用用户级线程,状态线程也是如此。
M:N(混合线程)
M:N 将 M 个应用程序线程映射到 N 个内核实体,[3] 或“虚拟处理器”。这是内核级(“1:1”)和用户级(“N:1”)线程之间的折衷。一般来说,“M:N”线程系统的实现比内核或用户线程更复杂,因为需要更改内核和用户空间代码。在M:N实现中,线程库负责在可用的可调度实体上调度用户线程;这使得线程的上下文切换非常快,因为它避免了系统调用。然而,这会增加复杂性和优先级反转的可能性,以及在用户态调度程序和内核调度程序之间没有广泛(且昂贵)协调的情况下实现次优调度。
from wiki clear explanation
1:1 (Kernel-level threading)
Threads created by the user are in 1-1 correspondence with schedulable entities in the kernel.[3] This is the simplest possible threading implementation. Win32 used this approach from the start. On Linux, the usual C library implements this approach (via the NPTL or older LinuxThreads). The same approach is used by Solaris, NetBSD and FreeBSD.
N:1 (User-level threading)
An N:1 model implies that all application-level threads map to a single kernel-level scheduled entity;[3] the kernel has no knowledge of the application threads. With this approach, context switching can be done very quickly and, in addition, it can be implemented even on simple kernels which do not support threading. One of the major drawbacks however is that it cannot benefit from the hardware acceleration on multi-threaded processors or multi-processor computers: there is never more than one thread being scheduled at the same time.[3] For example: If one of the threads needs to execute an I/O request, the whole process is blocked and the threading advantage cannot be utilized. The GNU Portable Threads uses User-level threading, as does State Threads.
M:N (Hybrid threading)
M:N maps some M number of application threads onto some N number of kernel entities,[3] or "virtual processors." This is a compromise between kernel-level ("1:1") and user-level ("N:1") threading. In general, "M:N" threading systems are more complex to implement than either kernel or user threads, because changes to both kernel and user-space code are required. In the M:N implementation, the threading library is responsible for scheduling user threads on the available schedulable entities; this makes context switching of threads very fast, as it avoids system calls. However, this increases complexity and the likelihood of priority inversion, as well as suboptimal scheduling without extensive (and expensive) coordination between the userland scheduler and the kernel scheduler.
Wikipedia 总结得很好:
线程与进程的比较
线程与传统的多任务处理不同操作系统进程的特点是:
进程的子集进程
进程内的线程共享状态
以及内存和其他资源
地址空间
沟通机制。
通常比上下文更快
进程之间的切换。
像 Windows NT 和 OS/2 这样的系统据说具有“廉价”线程和“昂贵”进程;在其他操作系统中,除了地址空间切换的成本(意味着 TLB 刷新)之外,没有太大区别。
任务和进程作为同义词使用。
Wikipedia sums it up quite nicely:
Threads compared with processes
Threads differ from traditional multitasking operating system processes in that:
subsets of a process
threads within a process share state
as well as memory and other resources
address space
communication mechanisms.
typically faster than context
switching between processes.
Systems like Windows NT and OS/2 are said to have "cheap" threads and "expensive" processes; in other operating systems there is not so great a difference except the cost of address space switch which implies a TLB flush.
Task and process are used synonymously.