为什么创建线程比创建进程更快?

发布于 2024-10-17 10:49:23 字数 127 浏览 2 评论 0原文

仅仅是因为它们只需要一个堆栈和寄存器存储,所以它们的创建成本很低吗?

线程可以共享公共数据(即它们不需要使用进程间通信)是这里的一个因素吗?这会导致对保护的需求减少吗?

或者线程是否比进程更好地利用多处理器?

Is it simply because they only need a stack and storage for registers so they are cheap to create ?

Is the fact that threads can share common data, i.e they don't need to use interprocess communication a factor here ? Would this result in less need for protection ?

Or do threads take advantage of multiprocessors better than processes ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞清仓 2024-10-24 10:49:23

谁说是的?在某些操作系统上几乎没有区别。您是否想到 Windows 中的线程比进程轻得多?

我怀疑您可以通过咨询此 堆栈溢出问题

Who says it is? On some operating systems there is little difference. Are you thinking of Windows where threads are much lighter weight than processes?

I suspect you would learn more by consulting this Stack Overflow question.

素年丶 2024-10-24 10:49:23

如果我们谈论重量级线程(例如 Windows 线程),一个进程有线程,并且它至少有一个线程(主线程),所以显然它更重或至少不轻:-)(总和是总是>=部分)

进程必须具有许多“表”(打开的文件表,显示内存如何映射的表(LDT,本地描述符表)...)。如果您创建一个进程,则必须初始化所有这些表。如果您创建一个线程,它们就不会(因为该线程使用其进程的线程)。然后一个新进程必须再次加载所有 DLL,检查它们是否重新映射......

If we speak of heavy-weight threads (Windows Threads for example), a Process has Threads, and it has at least one thread (the main thread), so clearly it's heavier or at least not-lighter :-) (the sum is always >= the part)

There are many "tables" that a Process must have (the open file table, the table that show how the memory is mapped (LDT, Local Descriptor Table)...). If you create a process all these tables have to be initialized. If you create a thread they don't (because the thread uses the ones of its process). And then a new process has to load again all the DLL, check them for remapping...

若水般的淡然安静女子 2024-10-24 10:49:23

从 Windows 角度来看,如果进程正在加载许多 DLL 并且由于基地址冲突而在内存中移动它们,则创建进程可能需要更长的时间。然后查看 David Heffernan 的答案链接中列出的所有其他原因。

From the windows perspective, a process can take longer to create if it is loading many DLLs and also moving them around in memory due to conflicts in the base address. Then see all of the other reasons listed in the link from David Heffernan's answer.

自演自醉 2024-10-24 10:49:23

进程切换需要更改 CS/DS 寄存器。这些寄存器值的更改需要从全局描述符表中获取新的描述符,这实际上是一个耗费 CPU 时间的过程。

Process switch requires change in CS/DS registers. Change in the value of these registers require fetching a new descriptor from global descriptor table which is actually expensive process in terms of CPU time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文