开发者

can a vfork child access parent variables?

How does a child process modify or read data in parent process after vfork()? Are the variables declared in parent process d开发者_开发百科irectly accessible to the child?

I have a process which creates some data structures. I then need to fork a child process which needs to read/write these data structures. The child will be an exec'ed process different from the parent.


One process cannot directly modify another's memory. What you would typically do is create a pipe or other mechanism that can cross process boundaries. The open descriptors will be inherited by the child process if you use fork(). It can then send messages to the parent instructing it to modify the data structures as required.

The form of the messages can be the difficult part of this design. You can:

  1. Design a protocol that carries values and instructions on what to do with them.
  2. Use an existing marshaling tool such as Google Protocol Buffers.
  3. Use Remote Procedure Calls with one of the existing RPC mechanisms (i.e. SUN or ONC-RPC).

You can also use a manually set-up shared memory scheme that would allow both processes to access common memory. The parent process would allocate storage for its data structures in that shared memory. The child process would map that also into its space and access those structures. You would need to use some sort of sync mechanism depending on how you use the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜