Sharing a global variable between forked processes

Deadly 提交于 2019-12-12 02:52:44

问题


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.



来源:https://stackoverflow.com/questions/9143869/sharing-a-global-variable-between-forked-processes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!