C中的pthread和子进程数据共享

发布于 2024-08-27 18:13:23 字数 212 浏览 7 评论 0原文

父进程的数据如何与 fork() 调用创建的子进程或由 pthread_create() 创建的线程共享

我的问题有点概念化,例如, 全局变量直接传递到子进程中,如果是这样,子进程对该变量所做的修改是否会影响父进程中的值?

我感谢提前提供部分和完整的答案,如果我缺少任何现有资源,我很抱歉,我已经在谷歌上进行了一些搜索,但找不到好的结果,

再次感谢您的时间和答案

my question is somewhat conceptual, how is parent process' data shared with child process created by a fork() call or with a thread created by pthread_create()

for example, are global variables directly passed into child process and if so, does modification on that variable made by child process effect value of it in parent process?

i appreciate partial and complete answers in advance, if i'm missing any existing resource, i'm sorry, i've done some search on google but couldn't find good results

thanks again for your time and answers

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

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

发布评论

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

评论(3

浮光之海 2024-09-03 18:13:23

fork() 和 pthread_create() 的语义略有不同。

fork() 将创建一个新进程,其中父进程和子进程之间的全局变量将是分开的。大多数操作系统实现将使用写时复制语义,这意味着父进程和子进程都将为所有全局变量使用相同的物理内存页,直到其中一个进程尝试编辑物理内存,此时该进程的副本页面已创建,这样现在每个进程都会获得自己的副本,并且看不到其他进程的副本,从而使进程被隔离。

另一方面,pthread_create() 在同一进程中创建一个新线程。新线程将拥有与同一进程的其他正在运行的线程分开的堆栈空间,但是全局变量和堆空间在同一进程的所有线程之间共享。这就是为什么您经常需要互斥体来协调同一进程的多个线程之间对共享内存块的访问。

TL;DR 版本:使用 fork(),你看不到其他人的更改;使用 pthread_create() 即可。

The semantics of fork() and pthread_create() are a little different.

fork() will create a new process, where the global variables will be separate between the parent and children. Most OS implementations will use copy-on-write semantics, meaning that both the parent and child process will use the same physical memory pages for all global variables until one of the processes attempts to edit the physical memory, at which point a copy of that page is made, so that now each process gets its own copy and does not see the other process's, so that the processes are isolated.

pthread_create() on the other hand, creates a new thread within the same process. The new thread will have a separate stack space from the other running threads of the same process, however the global variables and heap space are shared between all threads of the same process. This is why you often need a mutex to coordinate access to a shared piece of memory between multiple threads of the same process.

TL;DR version: with fork(), you don't see the other guy's changes; with pthread_create() you do.

隱形的亼 2024-09-03 18:13:23

分叉创建调用进程的几乎完全相同的副本,包括内存和文件描述符。全局变量与其他所有内容一起复制,但它们不以任何方式链接到父进程。由于文件描述符也被复制,父级和子级可以通过这些进行交互(只要它们设置正确,通常通过管道或套接字对)。

A fork creates an almost exact copy of the calling process, including memory and file descriptors. Global variables are copied along with everything else, but they are not in any way linked to the parent process. Since file descriptors are also copied, parent and child can interact via these (as long as they're setup properly, usually via pipe or socketpair).

合久必婚 2024-09-03 18:13:23

fork 创建的进程与由 pthread_create 创建的线程之间存在很大差异。进程不共享全局变量,应该通过管道、套接字或操作系统提供的其他工具进行通信。一个很好的解决方案是 MPI——它是一个用于进程间通信的消息传递库。

线程完全不同。使用pthread_create创建的线程与其调用者共享所有全局变量。而且,调用者可以将任意结构传递到线程中,并且该结构也将被共享。这意味着在使用线程进行编程时应该非常小心 - 如此大量的共享是危险的并且容易出错。 pthread API 为线程之间的稳健同步提供互斥体和条件(尽管仍需要实践和专业知识才能正确实现)。

There's a big difference between processes created by fork and between threads created with pthread_create. Processes don't share global variables and should communicate through pipes, sockets, or other tools provided by the OS. A good solution is MPI - which is a message-passing library for inter-process communication.

Threads are quite different. A thread created with pthread_create shares all the global variables with its caller. Moreover, the caller can pass an arbitrary structure into the thread, and this structure will also be shared. This means that one should be extremely careful when programming with threads - such amounts of sharing are dangerous and error prone. The pthread API provides mutexes and conditions for robust synchronization between threads (although it still requires practice and expertise to implement correctly).

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