LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-13-2008, 09:06 AM   #1
Varsha_vdd
LQ Newbie
 
Registered: Jun 2008
Posts: 5

Rep: Reputation: 0
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

Last edited by Varsha_vdd; 06-13-2008 at 09:10 AM. Reason: didnt added sign
 
Old 06-13-2008, 03:49 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 06-16-2008, 08:05 AM   #3
Varsha_vdd
LQ Newbie
 
Registered: Jun 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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

Last edited by Varsha_vdd; 06-16-2008 at 08:07 AM. Reason: didnt added sign
 
Old 06-16-2008, 10:44 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 06-17-2008, 04:25 AM   #5
Varsha_vdd
LQ Newbie
 
Registered: Jun 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Question

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
 
Old 06-17-2008, 09:42 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
  


Reply

Tags
program, scripts, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A problem with a shell variable visibility inside a while crisostomo_enrico Programming 7 11-23-2007 05:29 AM
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 06:48 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM
Pipe inside variable isn't working in bash Reginald0 Linux - General 5 01-30-2004 09:43 AM
Standard inside a shell program sammathew Linux - Software 1 09-17-2003 08:32 PM


All times are GMT -5. The time now is 11:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration