How to share the same variable between parent and child process?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.