Linux fork后,全局变量是否共享?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4298678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:09:48  来源:igfitidea点击:

After forking, are global variables shared?

clinuxunixfork

提问by

Consider this simple code:

考虑这个简单的代码:

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

When child increments myvar, is the value shared with the father (like pthread)?

当 child 增加 myvar 时,该值是否与父亲共享(如 pthread)?

采纳答案by MarkR

No and yes.

不,是的。

No, they are not shared in any way which is visible to the programmer; the processes can modify their own copies of the variables independently and they will change without any noticable effect on the other process(es) which are fork() parents, siblings or descendents.

不,它们不会以程序员可见的任何方式共享;进程可以独立地修改它们自己的变量副本,并且它们将更改而不会对其他进程(即 fork() 父进程、兄弟进程或后代进程)产生任何显着影响。

But yes, the OS actually does share the pages initially, because fork implements copy-on-write which means that provided none of the processes modifies the pages, they are shared. This is, however, an optimisation which can be ignored.

但是,是的,操作系统最初确实会共享页面,因为 fork 实现了写时复制,这意味着只要没有进程修改页面,它们就会被共享。然而,这是一种可以忽略的优化。

If you wanted to have shared variables, put them in an anonymous shared mapping (see mmap()) in which case they really will get shared, with all the caveats which come with that.

如果您想拥有共享变量,请将它们放在匿名共享映射中(请参阅 mmap()),在这种情况下,它们确实会被共享,并附带所有注意事项。

回答by Tom

No, since global variables are not shared between processes unless some IPC mechanism is implemented. The memory space will be copied. As a consequence, the global variable in both processes will have the same value inmediately after fork, but if one changes it, the other wont see it changed.

不,因为除非实现了某些 IPC 机制,否则进程之间不会共享全局变量。内存空间将被复制。因此,两个进程中的全局变量在 fork 之后立即具有相同的值,但是如果一个更改它,另一个将不会看到它更改。

Threads on the other hand do share global variables.

另一方面,线程确实共享全局变量。

回答by Kevin Lacquement

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.

fork()ing在分叉时创建父进程的精确副本。但是,fork()完成后,孩子就完全不同的存在了,不会向父母汇报。

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

换句话说,不,父的全局变量不会因子的变化而改变。

回答by Tharun26

After fork(), the entire process, including all global variables, is duplicated. The child is an exact replica of the parent, except that it has a different PID(Process Id), a different parent, and fork() returned 0. Global variables are still global within its own process. So the answer is no, global variables are not shared between processes after a call to fork().

在 fork() 之后,整个过程,包括所有全局变量,都被复制了。子进程是父进程的精确副本,除了它具有不同的 PID(进程 ID)、不同的父进程,并且 fork() 返回 0。全局变量在其自己的进程中仍然是全局的。所以答案是否定的,全局变量在调用 fork() 后不会在进程之间共享。