简体   繁体   中英

global variable access synchronization between two processes

I have a global boolean variable that both parent and child process can write to. The child process is just forked (no exec() called).

In C/Linux, how do I go about synchronizing access to this global? In C/C++ threading world, I could have used a mutex.

After calling fork , each process has its own copy of any variables in use. The global variable in the parent and the global in the child are completely distinct from each other, so they can't be used as a common variable.

If you want the two processes to share data, you would need to either create a pipe using the pipe function to pass data between the two, or you would need to create a shared memory segment that both processes would have access to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM