How to share memory between processes created by fork()?

前端 未结 3 1868
傲寒
傲寒 2020-11-22 15:30

In fork child, if we modify a global variable, it will not get changed in the main program.

Is there a way to change a global variable in child fork?



        
3条回答
  •  执笔经年
    2020-11-22 16:07

    Changing a global variable is not possible because the new created process (child)is having it's own address space.

    So it's better to use shmget(),shmat() from POSIX api

    Or You can use pthread , since pthreadsare sharing the globaldata and the changes in global variable is reflected in parent.

    Then read some Pthreads tutorial.

提交回复
热议问题