C++:是否可以通过分叉进程共享指针?

发布于 2024-07-14 16:35:40 字数 157 浏览 3 评论 0原文

我有一个计数变量,应该由我分叉并由母进程使用/读取的几个进程进行计数。

我尝试在母进程的 main() 函数中创建一个指针,并在分叉的子进程中向上计数该指针。 那不行! 尽管每个进程中的地址都是相同的,但每个孩子似乎都有自己的副本。

最好的方法是什么?

I have a count variable that should get counted up by a few processes I forked and used/read by the mother process.

I tried to create a pointer in my main() function of the mother process and count that pointer up in the forked children. That does not work! Every child seems to have it's own copy even though the address is the same in every process.

What is the best way to do that?

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

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

发布评论

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

评论(7

多彩岁月 2024-07-21 16:35:40

每个子进程都会获得自己的父进程内存副本(至少在它尝试修改任何内容时)。 如果您需要在进程之间共享,您需要查看共享内存或一些类似的 IPC 机制。

顺便说一句,为什么你要把它变成一个社区维基 - 你这样做可能会限制响应。

Each child gets its own copy of the parent processes memory (at least as soon as it trys to modify anything). If you need to share betweeen processes you need to look at shared memory or some similar IPC mechanism.

BTW, why are you making this a community wiki - you may be limiting responses by doing so.

枫以 2024-07-21 16:35:40

2个进程不能共享相同的内存。 确实,分叉的子进程在分叉后将共享相同的底层内存,但尝试对此进行写入将导致操作系统在其他地方为其分配新的可写空间。

查看另一种形式的 IPC 来使用。

2 processes cannot share the same memory. It is true that a forked child process will share the same underlying memory after forking, but an attempt to write to this would cause the operating system to allocate a new writeable space for it somewhere else.

Look into another form of IPC to use.

只有影子陪我不离不弃 2024-07-21 16:35:40

我的经验是,如果你想在至少两个进程之间共享信息,你几乎永远不想只共享一些 void* 指针到内存中。 您可能想看看

Boost Interprocess

它可以让您了解如何在进程之间共享结构化数据(阅读“类”和“结构”)。

My experience is, that if you want to share information between at least two processes, you almost never want to share just some void* pointer into memory. You might want to have a look at

Boost Interprocess

which can give you an idea, how to share structured data (read "classes" and "structs") between processes.

捶死心动 2024-07-21 16:35:40

不,使用 IPC 或线程。 仅共享文件描述符(但不共享查找指针)。

No, use IPC or threads. Only file descriptors are shared (but not the seek pointer).

海拔太高太耀眼 2024-07-21 16:35:40

您可能想检查共享内存。

You might want to check out shared memory.

潜移默化 2024-07-21 16:35:40

指针总是位于同一个进程中。 相对于进程的基地址,它是进程私有的。任何操作系统中都可以使用不同类型的 IPC 机制。 您可以选择Windows Messaging、共享内存、套接字、管道等。根据您的要求和数据大小选择一种。 另一种机制是使用可用的虚拟内存API在目标进程中写入数据,并用相应的指针通知进程。

the pointers are always lies in the same process. It's private to the process, relative to the process's base address. There different kind of IPC mechanisms available in any operating systems. You can opt for Windows Messaging, Shared memory, socket, pipes etc. Choose one according to your requirement and size of data. Another mechanism is to write data in target process using Virtual memory APIs available and notify the process with corresponding pointer.

琉璃繁缕 2024-07-21 16:35:40

对于共享计数来说,一种简单但有限形式的 IPC 选项是“共享数据段”。 在 Windows 上,这是使用 #pragma data_seg 指令实现的。

有关示例,请参阅这篇文章

One simple option but limited form of IPC that would work well for a shared count is a 'shared data segment'. On Windows this is implemented using the #pragma data_seg directive.

See this article for an example.

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