LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2007, 09:28 AM   #1
zwylinux
LQ Newbie
 
Registered: May 2006
Posts: 27

Rep: Reputation: 15
How to share the same variable between parent and child process?


There is a variable named "i", which equals 0
Now I want to do this:

for example ,i=0;
in the child process i=1;
Then I want to make i equals to 2 in the parent process.

How can i do that?
 
Old 03-24-2007, 09:57 AM   #2
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
Are you reffering to shell scripting?
As you probably know, child processes can't pass variables to their parents directly. But there are some ways to have interprocess communication, even between shell scripts (processes).

1. sending signals. Have the child send a signal to its parent, and (inside the parent script or function or whatever) trap that signal and make it set the variable. Something like this:
Code:
#!/bin/bash
# parent script

var_change() {
    i=$(($i+1))
}

trap var_change INT
export parent=$$
i=0

./child
........
trap - INT
Code:
#!/bin/bash
# child script

kill -s SIGINT $parent
........
2. use files; make the child output to a file, and the parent read from that file.
3. If you launch a subshell (like in a loop, or using a command list in parentheses and don't launch in background (with "&")), echo that variable inside the subshell and use command substitution, setting a variable to it, in the main script. Like this:
Code:
# main script
i=0

some_fnc() {
    while true; do
        ...
        i=1
        echo $i
        ...
    done
}

i=$(some_fnc)
And probably there are some other ways of doing this.

Last edited by omnio; 03-24-2007 at 11:27 AM.
 
Old 03-24-2007, 12:26 PM   #3
zwylinux
LQ Newbie
 
Registered: May 2006
Posts: 27

Original Poster
Rep: Reputation: 15
I forgot to say that I want to make it in C Language

Last edited by zwylinux; 03-24-2007 at 12:29 PM.
 
Old 03-24-2007, 12:38 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
I don't know much about C (or C++ for that matter), but you might be able to pass variables back and forth between parent and child processes using memory pointers.

Going hand-in-hand with my lack of knowledge of C, I am not sure if the parents and children processes can directly interact with each other, but if they can't, you could always build up all the needed variables, and pass the memory pointers as arguments to the child process when it's spawned, that way, it's modifying the value, not the reference, and will affect the parent process' values it obtains from those memory locations as well.

Last edited by indienick; 03-24-2007 at 12:42 PM.
 
Old 03-24-2007, 07:18 PM   #5
kourama
LQ Newbie
 
Registered: Sep 2006
Distribution: Mint64 w/KDE
Posts: 17

Rep: Reputation: 0
There are a few of ways to do IPC (inter-process comunication), but what you're describing sounds like shared memory.

Here's a link to a page with some code and good descriptions:
http://www.cs.cf.ac.uk/Dave/C/node27.html

On a side note: I found that shared memory doesn't work for me in cygwin, but the last time I tried it was a while ago, maybe there has been an update since then.

good luck.
 
Old 03-25-2007, 06:22 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Or perhaps it would be appropriate to use threads, where all variables are 'shared' among peer threads.
--- rod.
 
Old 03-26-2007, 02:47 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
in short, no you can't.
a fork produces an exact copy of the parent.
Like a clone, it looks the same, acts the same but if you poke it in
the eye the other won't feel it.

If you must then I would open a socket between them,
you can communicate all you like then.
The good thing about sockets is you can plug them in anywhere.
So they are a good thing to learn about.

I personally have never felt
the need to share memory or use threads.

maybe your design could be simpler?
 
  


Reply



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
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
GCC fork() - Can a child change a parent variable? johnhardey Programming 4 03-10-2005 10:49 AM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM
child and parent process error jdevanand Programming 1 04-29-2002 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration