简直崩溃,有个家伙跟我将fork()函数父子进程共享全局变量!是么?

teleinfor 2011-11-23 03:45:37
简直崩溃,有个家伙跟我将fork()函数父子进程共享全局变量!是么?
我专门测试了下,的确不行。我只知道fork()函数创建了一个调用者进程的副本,只是共享代码段而已。

再求证下。
...全文
927 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
昵称很不好取 2011-11-23
  • 打赏
  • 举报
回复
这个编个简单的例子验证下就好了
qq120848369 2011-11-23
  • 打赏
  • 举报
回复
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.
qq120848369 2011-11-23
  • 打赏
  • 举报
回复
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).
teleinfor 2011-11-23
  • 打赏
  • 举报
回复
嗯。不是同事,是我在网上看fork的资料的时候看到一个家伙的文章,这么说。让我大吃一惊,头回听说。我还以为是linux的新内核支持了这个高级功能特性呢?!不可思议,同步都成大问题了。

谢谢楼上几位高人。
「已注销」 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yph007595 的回复:]

不是的
只是fork后子进程中是父进程的完全复制(其中有写时复制技术),不管什么变量,fork后父子进程中都是一样的,但两者之间没有关系,任何一个进程修改变量后,在另一个进程中都不能知道,更不能访问另一个进程中的变量,即使是全局变量。

你同事说的是错的
[/Quote]
++
yph007595 2011-11-23
  • 打赏
  • 举报
回复
不是的
只是fork后子进程中是父进程的完全复制(其中有写时复制技术),不管什么变量,fork后父子进程中都是一样的,但两者之间没有关系,任何一个进程修改变量后,在另一个进程中都不能知道,更不能访问另一个进程中的变量,即使是全局变量。

你同事说的是错的

squiffy 2011-11-23
  • 打赏
  • 举报
回复
你说的没错。

但是,子进程继承父进程的全局变量。

子进程创建以后,可以读取原来父进程的全局变量的值。

但是创建以后,子进程修改了变量,或者是父进程修改了变量值,互相都不影响了。

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧