我们可以从父进程中读取子环境变量

时间:2022-12-25 22:57:32

I know that fork will create a child process and then execve in the child process will execute the program setting some environment variables.

我知道fork会创建一个子进程,然后execve在子进程中将执行程序设置一些环境变量。

My task here is, My parent process parent1 has initially created a child process Child1 following the fork/exec procedure and set an Environment variable "CHILD=first".

我的任务是,我的父进程parent1最初在fork / exec过程之后创建了一个子进程Child1,并设置了一个环境变量“CHILD = first”。

Later in the execution of parent process, I have created another child process Child2 following the same fork/exec procedure. This child2 has an environment variable set as "CHILD=second".

稍后在执行父进程时,我在同一个fork / exec过程之后创建了另一个子进程Child2。该child2的环境变量设置为“CHILD = second”。

Now, later in the execution of parent process, I have to read all the environment variables of child1 and child2 from Parent1 for some operations based on the env values. Is it possible to read these child environment variables from parent process? If so, how can I do it?

现在,稍后在执行父进程时,我必须从Parent1读取child1和child2的所有环境变量,以便根据env值进行某些操作。是否可以从父进程读取这些子环境变量?如果是这样,我该怎么办?

1 个解决方案

#1


1  

You can read the environment from /proc/<pid>/environ, on Linux. The variables are separated by nul-characters.

您可以在Linux上从/ proc / / environ读取环境。变量由空字符分隔。

#1


1  

You can read the environment from /proc/<pid>/environ, on Linux. The variables are separated by nul-characters.

您可以在Linux上从/ proc / / environ读取环境。变量由空字符分隔。