当前位置: 技术问答>linux和unix
简直崩溃,有个家伙跟我将fork()函数父子进程共享全局变量!是么?
来源: 互联网 发布时间:2017-01-19
本文导语: 简直崩溃,有个家伙跟我将fork()函数父子进程共享全局变量!是么? 我专门测试了下,的确不行。我只知道fork()函数创建了一个调用者进程的副本,只是共享代码段而已。 再求证下。 | 你说...
简直崩溃,有个家伙跟我将fork()函数父子进程共享全局变量!是么?
我专门测试了下,的确不行。我只知道fork()函数创建了一个调用者进程的副本,只是共享代码段而已。
再求证下。
我专门测试了下,的确不行。我只知道fork()函数创建了一个调用者进程的副本,只是共享代码段而已。
再求证下。
|
你说的没错。
但是,子进程继承父进程的全局变量。
子进程创建以后,可以读取原来父进程的全局变量的值。
但是创建以后,子进程修改了变量,或者是父进程修改了变量值,互相都不影响了。
但是,子进程继承父进程的全局变量。
子进程创建以后,可以读取原来父进程的全局变量的值。
但是创建以后,子进程修改了变量,或者是父进程修改了变量值,互相都不影响了。
|
不是的
只是fork后子进程中是父进程的完全复制(其中有写时复制技术),不管什么变量,fork后父子进程中都是一样的,但两者之间没有关系,任何一个进程修改变量后,在另一个进程中都不能知道,更不能访问另一个进程中的变量,即使是全局变量。
你同事说的是错的
只是fork后子进程中是父进程的完全复制(其中有写时复制技术),不管什么变量,fork后父子进程中都是一样的,但两者之间没有关系,任何一个进程修改变量后,在另一个进程中都不能知道,更不能访问另一个进程中的变量,即使是全局变量。
你同事说的是错的
|
++
|
man vfork.
vfork() is a special case of clone(2). It is used to create new processes without copying the page tables of the parent process. It may be use‐
ful in performance-sensitive applications where a child is created which then immediately issues an execve(2).
vfork() differs from fork(2) in that the parent is suspended until the child terminates (either normally, by calling _exit(2), or abnormally,
after delivery of a fatal signal), or it makes a call to execve(2). Until that point, the child shares all memory with its parent, including the
stack. The child must not return from the current function or call exit(3), but may call _exit(2).
vfork() is a special case of clone(2). It is used to create new processes without copying the page tables of the parent process. It may be use‐
ful in performance-sensitive applications where a child is created which then immediately issues an execve(2).
vfork() differs from fork(2) in that the parent is suspended until the child terminates (either normally, by calling _exit(2), or abnormally,
after delivery of a fatal signal), or it makes a call to execve(2). Until that point, the child shares all memory with its parent, including the
stack. The child must not return from the current function or call exit(3), but may call _exit(2).
|
The vfork() function differs from fork() only in that the child process can share code and data with the calling process (parent process). This
speeds cloning activity significantly at a risk to the integrity of the parent process if vfork() is misused.
The use of vfork() for any purpose except as a prelude to an immediate call to a function from the exec family, or to _exit(), is not advised.
speeds cloning activity significantly at a risk to the integrity of the parent process if vfork() is misused.
The use of vfork() for any purpose except as a prelude to an immediate call to a function from the exec family, or to _exit(), is not advised.
|
这个编个简单的例子验证下就好了