Preface:翻译水平渣,阅读需仔细。有错误欢迎指正。
What is the difference between a thread and a process?
线程和进程之间有何区别?
Processes vs Threads
进程VS线程
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.
进程是一个应用的实例,并且此实例处于运行状态。这意味着什么?举个例子,当你双击微软的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(Inter Process Communication)是相当困难并且资源密集的。
MultiThreading
多线程
Threads, of course, allow for multi-threading. A common example of the advantage of multithreading is the fact that you can have a word processor that prints a document using a background thread, but at the same time another thread is running that accepts user input, so that you can type up a new document.
当然,线程不唯一。多线程优势的一个常见例子是你可以让你的字处理工具使用后台线程打印文档的同时,另一个线程可以接受用户的输入,以便你可以键入一篇新的文档。
If we were dealing with an application that uses only one thread, then the application would only be able to do one thing at a time – so printing and responding to user input at the same time would not be possible in a single threaded application.
如果我们使用的应用仅仅使用了一个线程,那么此应用将一次只能做一件事情-在单线程里应用里,打印的同时响应用户的输入将是不可能的。
Each process has it’s own address space, but the threads within the same process share that address space. Threads also share any other resources within that process. This means that it’s very easy to share data amongst threads, but it’s also easy for the threads to step on each other, which can lead to bad things.
每个进程拥有自己的自己的地址空间,但是同一进程内的线程共享地址空间。线程也共享进程内的其他资源。这意味着在线程之间共享数据是很容易的,但是容易导致线程互踩(我认为说的是线程之间的干扰),这个可能导致不好的事情发生。
Multithreaded programs must be carefully programmed to prevent those bad things from happening. Sections of code that modify data structures shared by multiple threads are called critical sections. When a critical section is running in one thread it’s extremely important that no other thread be allowed into that critical section. This is called synchronization, which we wont get into any further over here. But, the point is that multithreading requires careful programming.
为了防止不好的事情发生,多线程编程必须小心。修改多线程共享的数据结构的代码段被称为临界区。当临界区在一个线程中运行时,十分重要的是一点是要保证没有其它线程被允许进入临界区。这就是所谓的同时性,我们不会更进一步。但是,问题是多线程要求仔细的编程。
Also, context switching between threads is generally less expensive than in processes. And finally, the overhead (the cost of communication) between threads is very low relative to processes.
线程之间的上下文切换相较于进程之间上下文切换也是通常来说成本更低些。线程之间通信的负担相对于进程也是很低的。
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.
1。线程相比于进程更容易创建因为线程不要求单独的地址空间。
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.
3.线程被认为是轻量级的因为线程相较于进程使用了更少的资源。
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.
4. 进程相互独立。由于线程共享同样的地址空间,线程是内部相互依赖的。所以一定要小心不同线程之间的踩踏。
5. A process can consist of multiple threads.
5.进程可以由多个线程组成。
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.
进程总是被存储咋主存或者RAM中。因此,进程被称为是一个活动的实体。当机器重启时,进程消失。一个程序可能和多个进程相关联。在多核处理器系统上,多个进程可以被并行执行。
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.
在同一进程能运行的所以线程共享同样的地址空间,文件描述符,栈和其它进程相关的属性。
由于进程的线程共享相同的内存,在进程内同步访问共享数据获得前所未有的重要性。
于 2015/8/2 日整理