C语言 为什么线程被称为轻量级进程?

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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:29:37  来源:igfitidea点击:

Why are threads called lightweight processes?

cmultithreadingpthreads

提问by Vijay

A thread is "lightweight" because most of the overhead has already been accomplished through the creation of its process.

线程是“轻量级”的,因为大部分开销已经通过创建它的进程来完成。

I found this in one of the tutorials.

我在其中一个教程中发现了这一点。

Can somebody elaborate what it exactly means?

有人可以详细说明它的确切含义吗?

回答by Daniel Earwicker

The claim that threads are "lightweight" is - depending on the platform - not necessarily reliable.

线程“轻量级”的说法 - 取决于平台 - 不一定可靠。

An operating system thread has to support the execution of native code, e.g. written in C. So it has to provide a decent-sized stack, usually measured in megabytes. So if you started 1000 threads (perhaps in an attempt to support 1000 simultaneous connections to your server) you would have a memory requirement of 1 GB in your process before you even start to do any real work.

操作系统线程必须支持本地代码的执行,例如用 C 编写的代码。因此它必须提供一个合适大小的堆栈,通常以兆字节为单位。因此,如果您启动了 1000 个线程(可能是为了支持与服务器的 1000 个同时连接),那么在您开始进行任何实际工作之前,您的进程中将需要 1 GB 的内存。

This is a real problem in highly scalable servers, so they don't use threads as if they were lightweight at all. They treat them as heavyweight resources. They might instead create a limited number of threads in a pool, and let them take work items from a queue.

这在高度可扩展的服务器中是一个真正的问题,因此它们根本不使用线程,就好像它们是轻量级的一样。他们将它们视为重量级资源。他们可能会在池中创建有限数量的线程,并让他们从队列中获取工作项。

As this means that the threads are long-lived and small in number, it might be better to use processes instead. That way you get address space isolation and there isn't really an issue with running out of resources.

由于这意味着线程寿命长且数量少,因此最好改用进程。通过这种方式,您可以获得地址空间隔离,并且不会出现资源耗尽的问题。

In summary: be wary of "marketing" claims made on behalf of threads. Parallel processing is great (increasingly it's going to be essential), but threads are only one way of achieving it.

总之:提防代表线程提出的“营销”声明。并行处理很棒(越来越重要),但线程只是实现它的一种方式。

回答by Erik Funkenbusch

Process creation is "expensive", because it has to set up a complete new virtual memory space for the process with it's own address space. "expensive" means takes a lot of CPU time.

进程创建是“昂贵的”,因为它必须用自己的地址空间为进程设置一个全新的虚拟内存空间。“昂贵”意味着占用大量 CPU 时间。

Threads don't need to do this, just change a few pointers around, so it's much "cheaper" than creating a process. The reason threads don't need this is because they run in the address space, and virtual memory of the parent process.

线程不需要这样做,只需改变周围的几个指针,因此比创建进程要“便宜”得多。线程不需要它的原因是因为它们运行在父进程的地址空间和虚拟内存中。

Every process must have at least one thread. So if you think about it, creating a process means creating the process AND creating a thread. Obviously, creating only a thread will take less time and work by the computer.

每个进程必须至少有一个线程。因此,如果您考虑一下,创建进程意味着创建进程和创建线程。显然,只创建一个线程将花费更少的时间和计算机的工作。

In addition, threads are "lightweight" because threads can interact without the need of inter-process communication. Switching between threads is "cheaper" than switching between processes (again, just moving some pointers around). And inter-process communication requires more expensive communication than threads.

此外,线程是“轻量级的”,因为线程无需进程间通信即可交互。在线程之间切换比在进程之间切换“更便宜”(同样,只是移动一些指针)。并且进程间通信需要比线程更昂贵的通信。

回答by Clifford

Threads within a process share the same virtual memory space but each has a separate stack, and possibly "thread-local storage" if implemented. They are lightweightbecause a context switch is simply a case of switching the stack pointer and program counter and restoring other registers, wheras a processcontext switch involves switching the MMU context as well.

进程内的线程共享相同的虚拟内存空间,但每个线程都有一个单独的堆栈,如果实现,可能还有“线程本地存储”。它们是轻量级的,因为上下文切换只是切换堆栈指针和程序计数器并恢复其他寄存器的情况,而进程上下文切换也涉及切换 MMU 上下文。

Moreover, communication between threads within a process is lightweightbecause they share an address space.

此外,进程内线程之间的通信是轻量级的,因为它们共享地址空间。

回答by Ivan Ivanov

process:

过程:

  1. process id
  2. environment
  3. folder
  4. registers
  5. stack
  6. heap
  7. file descriptor
  8. shared libraries
  9. instruments of interprocess communications (pipes, semaphores, queues, shared memory, etc.)
  10. specific OS sources
  1. 进程号
  2. 环境
  3. 文件夹
  4. 寄存器
  5. 文件描述符
  6. 共享库
  7. 进程间通信工具(管道、信号量、队列、共享内存等)
  8. 特定的操作系统来源

thread:

线:

  1. stack
  2. registers
  3. attributes (for sheduler, like priority, policy, etc.)
  4. specific thread data
  5. specific OS sources
  1. 寄存器
  2. 属性(对于调度器,如优先级、策略等)
  3. 特定线程数据
  4. 特定的操作系统来源

回答by Piyush Chandra

A process contains one or more threads in it and a thread can do anything a process can do. Also threads within a process share the same address space because of which cost of communication between threads is low as it is using the same code section, data section and OS resources, so these all features of thread makes it a "lightweight process".

一个进程包含一个或多个线程,一个线程可以做一个进程可以做的任何事情。进程内的线程也共享相同的地址空间,因为线程之间的通信成本低,因为它使用相同的代码段、数据段和操作系统资源,因此线程的所有这些特性使其成为“轻量级进程”。

回答by SHUBHAM DHIMAN

Just because the threads share the common memory space. The memory allocated to the main thread will be shared by all other child threads. Whereas in case of Process, the child process are in need to allocate the separate memory space.

只是因为线程共享公共内存空间。分配给主线程的内存将由所有其他子线程共享。而在 Process 的情况下,子进程需要分配单独的内存空间。