After forking, are global variables shared?

前端 未结 4 576
既然无缘
既然无缘 2020-11-29 00:50

Consider this simple code:

 int myvar = 0;
 int main() {
     if (fork()>0) {
       myvar++;
     } else {
       // father do nothing
     }
 }
<         


        
4条回答
  •  旧时难觅i
    2020-11-29 01:21

    fork()ing creates an exact copy of the parent process at the time of forking. However, after the fork() is completed, the child has a completely different existence, and will not report back to the parent.

    In other words, no, the parent's global variables will not be altered by changes in the child.

提交回复
热议问题