LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using Shell variable inside the C program (https://www.linuxquestions.org/questions/linux-newbie-8/using-shell-variable-inside-the-c-program-649053/)

Varsha_vdd 06-13-2008 09:06 AM

Using Shell variable inside the C program
 
Hi,
I m trying to use shell variables which are created/updated by a shell script, in my C program.
I m trying this by using thrid argument to main function of my C Program i.e. array of environment variables.

eg:
char *newVar="PROJECT=Sim";

int main(int argc, char **argv, char **env)
{ int i=0;
pid_t child_pid=0;
char *VarValue=NULL;

//putting new environment variable
if(putenv(newVar)!=0) printf("Eror in putenv\n");

// env contains all predefined environment variables.
for (i=0;i<25;i++)
printf("env[%i]: %s",i,env[i]);

switch(child_pid=fork())
{
case (pid_t)-1: perror("Error in child creation");break;

case (pid_t)0: printf ("child process\n");
execle("/home/vdd/script1.bash",NULL,env);
break;

default: printf("Parent process\n");
//waiting for all child to exit.
//wait(.....

VarValue=getenv(newVar);
printf("VarValue=%s\n",VarValue);
}
return 0;
}

The above C code creates a environment variable 'newVar' and put it into environment using C library funtion 'putenv'. I cross checked that variable is put into environment by using 'echo' command. I am not able to see this newly added variable in the array of environment variable which is third argument to my C program. Will the new variable available to this array?
The C code creates a child process which calls 'execle' system call. execle executes a script1.bash which updates my newly created variable.
Parent process tries to get the updated value of this variable, but it is getting the old value only. Am i missing some important point here?
Can I use the variable which are created/updated by shell script in my C program by some other way but with out using IPC?

-Varsha

theNbomr 06-13-2008 03:49 PM

I wish I had a dime for every time I've seen this question asked...
Simple answer: environment variables are private. No other process, including child processes can modify them. Child processes do not get the parent's environment, merely a private copy of it.
If you think about the process tree, and the fact that, ultimately, all processes are children of the init process, it would be clear that your notion of child processes being able to modify the parent's environment would ripple all the way up the tree to the init process itself. Along the way, any process which had spawned a child (as most shells would have done), would be in jeopardy of having its environment spontaneously modified by one of its progeny.
IPC is there for a good reason.

--- rod.

Varsha_vdd 06-16-2008 08:05 AM

Thank you! :)
I m thinking of going for PIPEs for sharing the data amongs two processes.
P1 is a program (process 1), which wants to share the data to P2.

P1 has following codelet:

if (fork_result == 0) {
//child process of P1
sprintf(buffer, “%d”, file_pipes[0]);
(void)execl(“P2”, “P2”, buffer, (char *)0);
exit(EXIT_FAILURE);
}
else {
//Process P1
data_processed = write(file_pipes[1], some_data,
strlen(some_data));
printf(“%d - wrote %d bytes\n”, getpid(), data_processed);
}

Process P1 passes 'file_pipes[0]' to process P2. P2 is another C program which takes buffer as its command line argument, modifies the data, and passes to the P1 in the same way as the P1 has done. If P2 would be the shell script, it can get the data from the P1 through commad line(using $1...). But how can it pass the data back to P1? Shell script can not create(or use existing pipe) the pipe the way C program creates/uses. Execuse me if i m asking very basic question. ;)

-Varsha

theNbomr 06-16-2008 10:44 AM

Pipes are a one-way deal. If you need the medium for bidirectional data transfer, you can use shared memory or message queues. For a good primer on these matters, consult Beej's Guide.
BTW, when posting source code or other formatted text, please enclose it in [CODE] tags to preserve its formatting and to give it the emphasis it deserves.
--- rod.

Varsha_vdd 06-17-2008 04:25 AM

IPC can be used, if both the communicating processes are C programs in execution. How to achive communication between two processes, when one is a C program in execution and other is a shell script?
Unidirectional communication, the C program passing variable to the shell script, can be done by using 'execl' system call. But how to achive reverse data flow, i.e. shell script passing data(shell variables) to the another process which is a C program in execution?

-Varsha

theNbomr 06-17-2008 09:42 AM

I don't know of any direct way of accessing IPC methods/APIs from Bash. It is simple enough to create a small utility to stuff message queues with data, which could be called from Bash, passing the data and queue identifier(s) as arguments. I suppose the same could be done for shared memory. Either could be done with C or Perl code, probably others like Python or Java.
--- rod.


All times are GMT -5. The time now is 07:59 PM.