Sharing a global variable between forked processes

2019-09-08 04:30发布

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?

标签: c process fork
3条回答
神经病院院长
2楼-- · 2019-09-08 05:10

You cannot.

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

查看更多
聊天终结者
3楼-- · 2019-09-08 05:17

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.

查看更多
做自己的国王
4楼-- · 2019-09-08 05:28

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.

查看更多
登录 后发表回答