Question about global environment variables & fork() exec()


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Question about global environment variables & fork() exec()
# 1  
Old 12-18-2018
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?

Thanks to everyone for any assistance!

Last edited by bodisha; 12-18-2018 at 04:21 PM..
# 2  
Old 12-18-2018
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.
# 3  
Old 12-18-2018
Quote:
Originally Posted by bodisha
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:

Code:
export var=foo
export var=foo:bar
export var=foo:bar:baz

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 hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array - Export/Import in global environment variables.

Hello. During startup /etc/bash.bashrc.local generates some array ..... source /.../.../system_common_general_array_env_var ..... The file system_common_general_array_env_var contains : LEAP_VERSION='42.3' ARRAY_MAIN_REPO_LEAP=('zypper_local' 'openSUSE-Leap-'"$LEAP_VERSION"'-Non-Oss' ... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

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)
Discussion started by: ginowms
3 Replies

3. UNIX for Dummies Questions & Answers

fork with exec

What is is difference between 'fork with exec' and 'fork without exec'? How both are related? (1 Reply)
Discussion started by: kkalyan
1 Replies

4. Programming

Newbie question on exec,fork, wait,pipe C

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)
Discussion started by: Cuervo
1 Replies

5. Programming

How forbid use fork() in exec() program.

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)
Discussion started by: kzi
3 Replies

6. Solaris

How to access ENV variables of non global zones in global zone???

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)
Discussion started by: vijaysachin
1 Replies

7. Shell Programming and Scripting

fork and exec

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)
Discussion started by: looza
1 Replies

8. Solaris

fork and exec ftp

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)
Discussion started by: KittyJ
2 Replies

9. UNIX for Dummies Questions & Answers

FORK/EXEC technique

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)
Discussion started by: marshmallow
4 Replies

10. Programming

Fork and exec

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)
Discussion started by: driki
3 Replies
Login or Register to Ask a Question