C语言:如何通过IPC在父进程和子进程(分叉)之间共享结构体(或者,如果不可能,则共享数组)?
过去两周我用谷歌搜索了这个问题,但没有得到任何答案。这就是我所拥有的:
一个父进程,它创建一个结构
myStruct
,它基本上是一个使用指针的链表(如果这是一个主要问题,我可以接受使用固定大小的数组)使用
fork()
创建的固定数量的子进程,需要对父进程创建的结构(或数组)进行读/写访问。
我不知道如何才能使变量 myStruct
在进程之间共享。
我尝试使用 SysV IPC 函数(例如 shmget()
、shmat()
等)解决问题...以便在共享内存中分配我的变量,但我不这样做不知道如何使用 void 内存指针将值读/写到 myStruct 中。
理想情况下,我希望能够在每个进程中使用点符号 (myStruct.node)->attribute = value
而不必处理指针,因为我不知道我的结构如何被组织到内存中。
这可能吗?你们中的一些人可以帮忙吗?非常感谢任何帮助。
进一步注意:我知道使用线程、管道、套接字或类似的东西会容易得多,但这项工作是出于学术目的,为此我必须模拟多个独立进程的存在。
I googled this for the past two weeks and I didn't get any answer. This is what I have:
A parent process, which creates a struct
myStruct
that is basically a linked list using pointers (if this is a major issue, I can accept to use a fixed size array instead).A fixed number of child processes created with
fork()
that need a read/write access to the struct (or array) created by the parent.
I don't know how to do in order to make the variable myStruct
become shared between processes.
I tried to solve the problem using SysV IPC functions like shmget()
, shmat()
, etc... in order to allocate my variable in shared memory, but I don't know how to work with void memory pointers to read/write the values into myStruct.
Ideally, I would like to be able to use the dot notation (myStruct.node)->attribute = value
in every process without having to deal with pointers, since I don't know how my struct is organized into memory.
Is that possible? Could some of you please help? Any help is REALLY appreciated.
Further note: I know using threads, pipes, sockets or things like that would be much easier, but this work is for academic purposes for which I have to simulate the presence of multiple independent processes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用以下方式创建共享匿名映射:
那么这些页面将不会被写入时复制,而是由所有分叉进程共享。
请注意,在这里锁定时必须小心。当(且仅当!)您使用
pthread_mutexattr_setpshared
和 < code>pthread_condattr_setpshared 将它们标记为在进程之间共享。另请注意,此技术映射固定大小的竞技场,并且必须在分叉之前完成。如何管理
p
内存的内容取决于您。创建一个可调整大小的共享内存区域并不简单;如果您想走这条路,我建议您发布第二个问题,因为可能需要不同的方法。If you create a shared anonymous mapping with:
Then these pages will not be copied-on-write, but rather will be shared by all forked processes.
Note that you have to be careful with locking here. You can use standard pthread mutexes and condvars on the shared memory segment between processes if (and only if!) you use
pthread_mutexattr_setpshared
andpthread_condattr_setpshared
to mark them as shared between processes.Note also that this technique maps a fixed size arena, and must be done before forking. How you manage the contents of the memory at
p
is up to you. It's nontrivial to create a resizable shared memory arena; if you want to go that route, I'd recommend posting a second question, as different approaches may be necessary.跨分叉共享内存的最简单方法是使用
mmap()
ed 内存区域。如果您使用匿名mmap()
(使用 MAP_ANON 和 MAP_SHARED)来存储您的结构(数组等)而不是malloc()
ed 内存,则它将由子进程。The easiest way to share memory across a fork is to use a
mmap()
ed region of memory. If you use an anonymousmmap()
(with MAP_ANON and MAP_SHARED) to store your struct (array, whatever) instead ofmalloc()
ed memory, it will be shared by the child process.