Question about global environment variables & fork() exec()
Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck
What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec() system call overwrites environment variable created by the fork()... But how does exec() cause global environment variables to be included with a newly created child process if all the environment variables are overwritten? My best guess is that global environment variables are somehow excluded from being overwritten... But I can't confirm this
Also... when does the fork() assign a PID to a child process? Before or after it copies itself into a new region of memory?
Nothing prevents the child from mangling its own environment variables before exec(), which is how execle, execvpe work IIRC. Otherwise, the child receives copies.
Whether it's before or after is kind of a moot point. It's done by copy-on-write, which copies on write.
But how does exec() cause global environment variables to be included with a newly created child process if all the environment variables are overwritten?
I am not sure what you exactly want to know, so maybe i will tell you something you already know. Bear with me, i hope to make things clearer and if this is not the case please ask again:
Part of what a process gets from the OS when it is created is a "process environment". This is a place in memory with a collection of strings that look all like name=value, i.e. PATH=/usr/bin:/usr/local/bin. You can inspect the environment of a shell (ksh or bash) process with the command set but all other processes have a similar environment, even if you can't inspect it that easily.
When a process is created it is created from some "father" process, which also have some environment. All processes in a system are organised in a tree-like hierarchy which goes back to init - the process to start all other processes. All these processes (init included) have such an environment, although the environment of init is legendarily poor. This (just as an aside) is why cron jobs often fail: cron is started from init and since cron itself doesn't need any environment it can work with what it inherits from init. This might not be the case at all for the processes started by cron, aka cron-jobs, though.
Now, every process can add to the environment it inherits - or, to be precise: it can add to its own copy of inherited environment and change/delete existing or even define new name=value pairs. This only affects the processes own environment. Now there is a keyword in the shell, which reflects a certain property of these name=value-pairs (or "environment variables"): export. export will cause an environent variable to be passed to children. So every process can inherit from its father, change its envirment and then pass this changed environment to its own children.
This in fact is done i.e. when you login: the login process have a certain environment already set, then will ask you for name and password, then start your login shell as a child process (so it iherits from login) and run ~/.profile. The shell itself will run (in its own environment) ~/.bashrc or ~/.kshrc or some similar file and further change its environment. If you now start a program in there this process will inherit whatever you have exported from this environment.
Notice that if you do not export a variable it will not be included in the environment a child process inherits. Also notice that a variable is either exported or not. Re-exporting it every time you change it like it is often seen:
will not hurt but is not necessary. Once the variable is exported it remains so because it is a property of the variable itself, not just its current value.
So, to answer your initial question: whenever a new process is created a process environment is created as a part of this creation. This process environment is filled with a copy of what is exported in the environment of the father process before the process itself is started.
I have the following bash script lines in a file named test.sh.
#!/bin/bash
#
# Write Date to cron.log
#
echo "Begin SSI Load $(date +%d%b%y_%T)"
#
# Get the latest rates file for processing.
#
d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1)
filename=$d
export filename... (3 Replies)
Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine.
So, i want to... (1 Reply)
Hello World!
I am writing code in C++ which have to launch another application X using exec().
I would like to set some limits on it using setrlimit etc...
My problem is that i don't know how to forbid using fork() and strlimit by application X.
How can i do it? (3 Replies)
Hi Guys,
My requirement is I have file called /opt/orahome/.profile in non global zone.
PATH=/usr/bin:/usr/ucb:/etc:/usr/sbin:/usr/local/bin:/usr/openwin/bin:.
export PATH
PS1="\${ORACLE_SID}:`hostname`:\$PWD$ "
export PS1
EDITOR=vi
export EDITOR
ENV=/opt/orahome/.kshrc
export ENV... (1 Reply)
I need to ssh to a remote server and run my script there. This is my script.
$ssh = "ssh username@host";
$cmd = "$ssh 'cd <my dir> && < sudo Run_exe>'";
my $pid = fork;
if ($pid == 0){
exec $cmd;
}
When I run this I get:
pccons_getchar: got r == 0 (1 Reply)
Hi,
I need to find/implement an application that FTPs (puts) all new files in a certain directory to an external storage unit. This application should check for new files every 10 seconds (leaving the FTP connection open in between the 10 seconds).
The easiest way would be if there are... (2 Replies)
Hi!
Can someone explain me exactly this technique? Why a process (PARENT) creates a copy of itself with FORK (CHILD)? What's the reason of this behaviour? Sorry, but I cannot understand the logic behind it.
Thanks. (4 Replies)
Hello!
I am working on a server where I should have 4 (resident)processes, one of them being "the father" of the others, so I do 3 forks. The problem that I have is that I do an accept (for sockets) in the "father" process and I want to transmit the job to one of the processes "child" with... (3 Replies)