Sharing a global variable between forked processes

2019-09-08 04:34发布

问题:

I have a global variable, X. I then fork and modify X from the child. I want those changes to show up in the parent, but I don't want the parent to have to wait on the child.

How can I do this?

回答1:

You need to put the variable in shared memory. There are many ways to create shared memory. I'd probably just use mmap, but you could also check out shmget or shm_open.



回答2:

When you fork a new process that is a separate copy of the address space. It can only see the changes made before the fork.

If you want shared memory for communication between the processes, you have to create that explicitly.



回答3:

You cannot.

After forking, those are two separate processes. You will have to make use of some IPC.



标签: c process fork