Re: communication between forked processes
by perrin (Chancellor) on Feb 24, 2003 at 17:56 UTC
|
| [reply] |
|
|
Thank you, I thought it would be something like that since the forked programs are wholly seperate copies of the main program, they get their own everything (with a few exclusions). I was just hoping there was another way. I might still do it like this but use the main program for the bulk of the work and the child processes for working on the data units.
Thanks.
| [reply] |
•Re: communication between forked processes
by merlyn (Sage) on Feb 24, 2003 at 19:04 UTC
|
| [reply] |
Re: communication between forked processes
by Pardus (Pilgrim) on Feb 24, 2003 at 17:56 UTC
|
You do not have some kind of shared memory between these processes, so basicly it won't work.
Possible hacks:
- Serialize the data and use a socket.
- Dump the data and use a file.
- Use threads, references work partially.
--
Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
>>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<< | [reply] |
Re: communication between forked processes
by jasonk (Parson) on Feb 24, 2003 at 17:57 UTC
|
It is possible to share data structures if you use threads instead of forking, but when you fork you end up with two completely separate processes. To get forked processes to share data you can use shared memory, sockets, pipes, and a few other obscure methods. A good starting point is the perlipc documentation.
| [reply] |
Re: communication between forked processes
by pg (Canon) on Feb 24, 2003 at 18:24 UTC
|
You have copies of everything in the forked processes, but those are the images taken from the moment you fork. After the fork, each process has its own memory space.
Although it is possible to share memory among processes, there are lots of articles about why it is not a good approach. For new applications, that is not a choice, unless you are touching some low level stuff.
Thread is the solution. Start to use threads, I say this to myself all the time. Let’s always ask ourselves the question whether thread is a good fit, whenever we are thinking of fork. My opinion is: use thread for tight coupling, and fork for loose coupling.
In your current case, you want to share a hash, I see it tight enough for threads. If there is other reasons unknown to me made you pick fork, go IPC’s, don’t use shared memory.
| [reply] |
|
|
Thanks for your input. I have never done any work with threads before. I program in unix and don't even know if you can use threads in unix. If so, could you please point me to an article or sight that might help clear things up?
Thanks, Chad.
| [reply] |
|
|
Definitely no problem with UNIX.
For Perl, Perl only started to really support thread in 5.8.0. For the Perl part, there are some very useful information in perldoc, check out the threads, and threads::shared pragmas.
As for online material, go google, and search for pthread (do not search for thread), it will give you whole bunch of result, I just checked it. Really useful stuffs. Those are not about Perl’s thread, but as you never used thread before, understand POSIX thread will definitely help you, and Perl’s thread mimics POSIX thread a lot, at least at the interface and concept level, so this would also help you to understand Perl thread.
I am a little bit concerned whether it is good to use Perl to learn thread (considering it just started and a little bit prime). My path is I understood thread first, then came across Perl. Check whether you have pthread lib installed on your unix box, if yes, play it with c, while you are working on Perl at the same time. (if no pthread installed, there is always the native thread, but I would think that’s less ideal.)
There is a good book from O’Relly called “Pthreads programming”, not thick, good for both beginners and experienced programmers, and it has a section about why “shared memory” is a bad choice.
Update
You might also search my write-ups for the word “threads”, I have some quick examples there. As all those examples say “use threads”, this search should work.
| [reply] |
|
|
|
|
Re: communication between forked processes
by JamesNC (Chaplain) on Feb 24, 2003 at 18:56 UTC
|
Here is an example using threads and threads::shared, I am on Win32 and ActiveState 5.8.0
You could use pipe without much effort for a parent and single child communication.. but things get really messy when you try to fork off 10 or 20 processess, and have the children all communicate async with the parent or even each other, I think merlyn or one of the other "gods" has a solution for communication and even directing multi-forked processes. You should also check out Parallel::ForkManager for a very neat way to manage forked processes:
use strict;
use threads;
use threads::shared;
my @list = qw (
www.apple.com www.ibm.com www.oracle.com www.sun.com
);
my @good: shared;
my @bad: shared;
my $thread;
my @thread;
foreach(@list){ push @thread, threads->new("ping", "$_"); }
foreach $thread( @thread){ $thread->join; }
print "Good: @good\n";
print "Bad: @bad\n";
sub ping {
my $ip = shift;
my @rv = `ping -n 1 -w 100 $ip`;
foreach(@rv){
if (/(\d+\.\d+\.\d+\.\d+)/){ $ip = $1 };
push @good, $ip if /\(0% loss\)/g;
push @bad, $ip if /\(100% loss\)/g;
}
}
Cheers,
JamesNC | [reply] [d/l] |