gnu/Linux 上 pthread 和 fork 的区别
pthread 和 fork wrt linux 在以下方面的基本区别是什么 实现差异以及调度如何变化(有变化吗?)
我在两个类似的程序上运行 strace,一个使用 pthreads,另一个使用 fork, 最后都使用不同的参数进行clone()系统调用,所以我猜测 这两者在 Linux 系统上本质上是相同的,但 pthreads 更容易 在代码中处理。
有人可以给出深刻的解释吗?
编辑:另请参阅相关的问题
What is the basic difference between a pthread and fork w.r.t. linux in terms of
implementation differences and how the scheduling varies (does it vary ?)
I ran strace on two similar programs , one using pthreads and another using fork,
both in the end make clone() syscall with different arguments, so I am guessing
the two are essentially the same on a linux system but with pthreads being easier
to handle in code.
Can someone give a deep explanation?
EDIT : see also a related question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然而,在 C 中存在一些差异:
fork()
目的是创建一个新进程,该进程成为调用者的子进程
两个进程都会执行fork()系统调用后的下一条指令
为父进程和子进程创建两个相同的计算机地址空间、代码和堆栈副本。
把叉子想象成一个人;分叉会导致程序(进程)的克隆,即运行它复制的代码。
pthread_create()
目的是在程序中创建一个新线程,并赋予调用者相同的进程
同一进程内的线程可以使用共享内存进行通信。 (小心!)
第二个线程将共享数据、打开文件、信号处理程序和信号配置、当前工作目录、用户和组 ID。新线程将获得自己的堆栈、线程 ID 和寄存器。
继续类比;当你的程序(进程)创建一个连接到同一个大脑的新线程时,它会长出第二条手臂。
性能差异 默认
情况下,分叉进程不与父进程共享内存空间和其他资源(例如文件句柄),而同一进程内的线程则共享这些资源。就线程间通信而言,共享内存可以更高效、更快,但需要仔细同步以防止竞争条件。
由于进程内的线程共享内存空间,因此它们比分叉进程具有更高的内存效率,每个分叉进程都有自己的内存空间
In C there are some differences however:
fork()
Purpose is to create a new process, which becomes the child process of the caller
Both processes will execute the next instruction following the fork() system call
Two identical copies of the computer's address space,code, and stack are created one for parent and child.
Thinking of the fork as it was a person; Forking causes a clone of your program (process), that is running the code it copied.
pthread_create()
Purpose is to create a new thread in the program which is given the same process of the caller
Threads within the same process can communicate using shared memory. (Be careful!)
The second thread will share data, open files, signal handlers and signal dispositions, current working directory, user and group ID's. The new thread will get its own stack, thread ID, and registers though.
Continuing the analogy; your program (process) grows a second arm when it creates a new thread, connected to the same brain.
Performance Differences
Forked processes do not share memory space and other resources (such as file handles) with the parent process by default, while threads within the same process share these resources. Sharing memory can be more efficient and faster in terms of inter-thread communication, but it requires careful synchronization to prevent race conditions.
Because threads within a process share memory space, they can be more memory efficient than forked processes, which each have their own memory space
在 Linux 上,系统调用
clone
克隆任务,并具有可配置的共享级别。fork()
调用clone(最少共享)
,pthread_create()
调用clone(最多共享)
。由于复制表和为内存创建 COW 映射,fork 的成本比 pthread_createing 稍高一点。
On Linux, the system call
clone
clones a task, with a configurable level of sharing.fork()
callsclone(least sharing)
andpthread_create()
callsclone(most sharing)
.forking costs a tiny bit more than pthread_createing because of copying tables and creating COW mappings for memory.
您应该查看
clone
联机帮助页。特别是,它列出了所有可能的克隆模式以及它们如何影响进程/线程、虚拟内存空间等......
你说“线程更容易在代码中处理”:这是非常有争议的。编写无错误、无死锁的多线程代码可能是一个相当大的挑战。有时,拥有两个独立的流程会使事情变得更加简单。
You should look at the
clone
manpage.In particular, it lists all the possible clone modes and how they affect the process/thread, virtual memory space etc...
You say "threads easier to handle in code": that's very debatable. Writing bug-free, deadlock-free multi-thread code can be quite a challenge. Sometimes having two separate processes makes things much simpler.