子进程会影响父进程的环境吗? [英] Can the child process affect parent process' environment?

查看:25
本文介绍了子进程会影响父进程的环境吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

孩子继承了父母的环境"是什么意思?通过复制整个环境来继承,还是通过接收指向同一环境的指针(以某种方式)继承?

What does "the child inherits the parent's environment" mean? Inherits by copying the whole environment, or inherits by receiving pointer to the same environment (somehow)?

这是我的场景:

  1. 我有一个正在运行的进程 P 有自己的环境(变量)
  2. 在某些时候,P 执行 fork
  3. if-statement(也就是子进程 C)的 0-clone 中,一个 execv被执行
  4. 两个进程继续独立运行.
  1. I have a running process P with its own environment (variables)
  2. At some point, P executes fork
  3. In the 0-clone of the if-statement (a.k.a. in the child process C), an execv is executed
  4. Both processes continue running independently.

因此,在某些时候,应用程序停止正常工作.原因是 - 破碎"的环境.

So, in some moment, the application stops working fine. And the reason is - "broken" environment.

有趣的部分是,两个环境都发生了变化......当我启动父进程并执行

The interesting part is, that both environments are changed.. When I start the parent process and execute

$ cat /proc/PID/environ

对于父进程和进程,一切都很好.几个小时后,应用程序停止工作,当我再次执行上面的行(以检查环境)时,两者都已更改,并且缺少许多环境变量 - 只有标准变量存在(例如 PWDHOMEUSER 等).

for both - the parent and the process, everything is fine. Some hours later, the app stops working and when I execute the line above again (to check the environment), both are changed and a lot of environment variables are missing - only the standard ones are there (like PWD, HOME, USER, etc.).

这怎么可能?问题出在哪里——在孩子身上还是在父母身上?

How is this possible? And where could the the problem - in the child or in the parent?

感谢大家的回答,我的 +1,因为他们都是正确的(@caf、@Saphrosit 和 @R..).这个问题的原因真的很傻..

Thanks all for the answers, +1 from me, as they were all correct ( @caf, @Saphrosit and @R..). The reason for this issue is really silly..

所有环境变量都放在 /etc/profile 中,它在登录后执行(我不知道).

All environment variables were placed in /etc/profile which is executed AFTER LOGIN (that.. I didn't know).

好吧,看来问题是在机器重新启动时发生的.因此,在启动时,应用程序再次启动,但 /etc/profile/ 没有执行/读取.这会导致不良行为.这就是问题在手动重启时消失的原因 - 一旦 root 登录(通过 ssh),来自 /etc/profiles 的环境变量被读取,当父进程重新启动时(通过 root),一切都很好 - 环境变量被继承.

Well, it appeared, that the issue have happened on restart of the machine. So, on start-up, the application is started again, but /etc/profile/ is not executed/read. And this causes the bad behavior. And that's why the problem disappears on manual restart - once a root is logged in (through ssh), the environment variables from /etc/profiles are read, and when the parent process is restarted (by root), it's all fine - the environment variables are inherited.

愚蠢的错误.

推荐答案

fork() 时刻,子级继承了父级环境的副本.任何一个过程中对环境的后续变化都不会影响另一个.

The child inherits a copy of the parent's environment at the moment of the fork(). Subsequent changes to the environment in either process do not affect the other.

改变这一点的唯一方法是做一些非常奇怪的事情,比如将环境放在 MAP_SHARED 区域中,或者使用 ptrace().不过,如果你做了这样的事情,你就会知道.

The only way you could alter this is by doing something very strange, like placing the environment in a MAP_SHARED area, or using ptrace(). You'd know it if you did something like this, though.

这篇关于子进程会影响父进程的环境吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆