线程与进程的一般情况
1)为什么线程创建比进程创建便宜?
2)线程和子进程有什么区别?这与上述问题有何关系?
1) Why is thread creation less expensive than process creation?
2) What is the difference between a thread and a subprocess? How would this relate to the above question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当一个进程被创建时,它被分配堆和栈内存。另一方面,线程只获得一个堆栈并与父进程共享该堆。这意味着即使您只考虑内存分配,创建进程也比创建线程更“昂贵”。
子流程只是由另一个流程创建的流程。它们在其他方面是独立的并拥有自己的存储空间。这是一篇文章,其中介绍了更多细节。
When a process is created it is allocated heap and stack memory. Threads on the other hand only get a stack and share the heap with the parent process. This means that even if you just consider memory allocation it is more "expensive" to create a process than a thread.
Subprocesses are simply processes that were created by another process. They are otherwise independent and get their own memory space. Here's an article that goes into some more detail.
这个问题的答案可能非常依赖于操作系统,但有一个一般规则:
进程的线程与其父进程位于同一虚拟内存空间中。因此,创建线程比创建进程更便宜,因为底层操作系统不需要创建完整的虚拟内存空间。
子进程只是父进程生成的另一个进程。
The answer to this question is probably very OS dependent but a general rule :
Threads of a process live in the same virtual memory space as their parent. So a thread creation is less expensive than a process creation because the underlying operating system doesn't need to create a full virtual memory space.
A subprocess is just another process spawned by a parent.