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..
10 More Discussions You Might Find Interesting
1. Programming
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
2. UNIX for Dummies Questions & Answers
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
3. Solaris
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
4. Shell Programming and Scripting
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
5. Solaris
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
6. Programming
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
7. Programming
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
8. UNIX for Dummies Questions & Answers
What is is difference between 'fork with exec' and 'fork without exec'?
How both are related? (1 Reply)
Discussion started by: kkalyan
1 Replies
9. Shell Programming and Scripting
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
10. Shell Programming and Scripting
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
LEARN ABOUT SUNOS
pthread_atfork
pthread_atfork(3C) Standard C Library Functions pthread_atfork(3C)
NAME
pthread_atfork - register fork handlers
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
int pthread_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void));
DESCRIPTION
The pthread_atfork() function declares fork handlers to be called prior to and following fork(2), within the thread that called fork(). The
order of calls to pthread_atfork() is significant.
Before fork() processing begins, the prepare fork handler is called. The prepare handler is not called if its address is NULL.
The parent fork handler is called after fork() processing finishes in the parent process, and the child fork handler is called after fork()
processing finishes in the child process. If the address of parent or child is NULL, then its handler is not called.
The prepare fork handler is called in LIFO (last-in first-out) order, whereas the parent and child fork handlers are called in FIFO
(first-in first-out) order. This calling order allows applications to preserve locking order.
RETURN VALUES
Upon successful completion, pthread_atfork() returns 0. Otherwise, an error number is returned.
ERRORS
The pthread_atfork() function will fail if:
ENOMEM Insufficient table space exists to record the fork handler addresses.
USAGE
Solaris threads do not offer pthread_atfork() functionality (there is no thr_atfork() interface). However, a Solaris threads application
can call pthread_atfork() to ensure fork()-safety, since the two thread APIs are interoperable. Seefork(2) for information relating to
fork() in a Solaris threads environment in Solaris 10 relative to previous releases.
EXAMPLES
Example 1: mMake a library safe with respect to fork().
All multithreaded applications that call fork() in a POSIX threads program and do more than simply call exec(2) in the child of the fork
need to ensure that the child is protected from deadlock.
Since the "fork-one" model results in duplicating only the thread that called fork(), it is possible that at the time of the call another
thread in the parent owns a lock. This thread is not duplicated in the child, so no thread will unlock this lock in the child. Deadlock
occurs if the single thread in the child needs this lock.
The problem is more serious with locks in libraries. Since a library writer does not know if the application using the library calls
fork(), the library must protect itself from such a deadlock scenario. If the application that links with this library calls fork() and
does not call exec() in the child, and if it needs a library lock that may be held by some other thread in the parent that is inside the
library at the time of the fork, the application deadlocks inside the library.
The following describes how to make a library safe with respect to fork() by using pthread_atfork().
1. Identify all locks used by the library (for example {L1,...Ln}). Identify also the locking order for these locks (for example
{L1...Ln}, as well.)
2. Add a call to pthread_atfork(f1, f2, f3) in the library's .init section. f1, f2, f3 are defined as follows:
f1()
{
/* ordered in lock order */
pthread_mutex_lock(L1);
pthread_mutex_lock(...);
pthread_mutex_lock(Ln);
}
f2()
{
pthread_mutex_unlock(L1);
pthread_mutex_unlock(...);
pthread_mutex_unlock(Ln);
}
f3()
{
pthread_mutex_unlock(L1);
pthread_mutex_unlock(...);
pthread_mutex_unlock(Ln);
}
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
+-----------------------------+-----------------------------+
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
+-----------------------------+-----------------------------+
|Interface Stability |Standard |
+-----------------------------+-----------------------------+
|MT-Level |MT-Safe |
+-----------------------------+-----------------------------+
SEE ALSO
exec(2), fork(2), atexit(3C), attributes(5), standards(5)
SunOS 5.10 12 Dec 2003 pthread_atfork(3C)