Reputation: 257
How does a child process modify or read data in parent process after vfork()? Are the variables declared in parent process directly 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.
Upvotes: 4
Views: 476
Reputation: 19044
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:
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.
Upvotes: 3