Author Topic: Child Processes And Environment Vars, Inheritance problems..  (Read 6009 times)

brainspoon

  • Guest
Hi,

I know that this question might not be in the right forum but maybe one of you smart guys knows an answer.

I have an app that basically launches another process (I'm using wxWidgets, and this uses fork() and execvp()). This new process (that is a csh script or whatever it is called) requires a certain env var to be set before it is launched - this is done by my program.

The app is portable, and under Windows, it works without problems, but under Linux it fails - the env var seems to be not inherited. We tried several methods, and all failed (like calling system("export var=...") before launching the process).

When manually setting the env var before launching my app, it seems to work - the env var is inherited. But if my app sets this var, it just doesn't want to work...

I require some advice here; maybe it's something totally stupid...

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283

frog-x

  • Guest
Re: Child Processes And Environment Vars, Inheritance proble
« Reply #2 on: June 08, 2005, 11:00:27 am »
Quote from: brainspoon

The app is portable, and under Windows, it works without problems, but under Linux it fails - the env var seems to be not inherited. We tried several methods, and all failed (like calling system("export var=...") before launching the process).


Hi brainspoon,

The reason system("export var=...") doesn't work is because system creates a seperate process and executes the export command in that process. Therefore the environment of this new process (and its children) will be changed, but the environment for your existing program (and hence any other programs it calls) will still be the same.

If you want to set environment variables using C, then including stdlib.h will give you getenv and putenv functions.
Code
putenv("var=value");


Alternatively, I expect you can use the shell to do it when you make the system call, although I suspect that is making it very unix specific.
Code
system("env VARIABLE=value script.sh");


frog-x