Operating System - HP-UX
1830899 Members
3380 Online
110017 Solutions
New Discussion

Getting refreshed Env Variable value in Child process??

 
amit_19
Occasional Contributor

Getting refreshed Env Variable value in Child process??

I have a process mfasys_call (code given below) which calls a application "mfasys". I have to set some Environment Variable in mfasys_call which should be available to the application mfasys. I make use of putenv function to set the environment variable in mfasys_call and use system() call in mfasys to get that environment variable value. This works fine normally.

Now my requirement is that mfasys sends SIGUSR1 which mfasys_call traps and changes the value of environment variable. However this changed value is not accessible within the mfasys. It retrieves the original value only. Is there any way to make the mfasys process get the latest updated value of environment variable set in mfasys_call (mfasys_call is parent of mfasys process).

void main()
{
char sEnvStr[30];

sprintf(sEnvStr, "MYPID=%d", getpid());
putenv(sEnvStr);

signal(SIGUSR1, sig_catch);

system("mfasys");
}

static void sig_catch(int sig)
{
char sEnvStr2[30];

sprintf(sEnvStr2, "MYPID=1111");
putenv(sEnvStr2);

}
2 REPLIES 2
Deepak Extross
Honored Contributor

Re: Getting refreshed Env Variable value in Child process??

The simple answer is NO. At the moment a child process is created, it inherits A COPY OF the perent's environment variables. From then on, they are two separate processes, and changes made to one's environment do not, in any way, affect the other.
A small kludge would be to communicate the new parameters across to your child process using IPC - shared memory or named pipe. An easier but less reliable workaround would be to use a scratch file accessible by both processes.
Tom Jackson
Valued Contributor

Re: Getting refreshed Env Variable value in Child process??

Hi Amit:

Using a temp file would be an easy way to do it.

It looks like the shmget(2)and shmctl(2) commands should work if you want to use shared memory.

Tom