Frizoker has asked for the wisdom of the Perl Monks concerning the following question:
I'm studying parallel processes and would like all child processes to send some messages to parent process. But I do not understand is it possible to do it via one pipe, or there should be separate pipes for each child process. Also, messages are being delievered to parent process just one time. Could you please advice how to fix it?
Thank you in advance!
#!/usr/bin/perl #use strict; #use warnings; use IO::Handle; pipe (READER, WRITER); WRITER->autoflush(1); my $parent=$$; print "parent: $parent\n"; # Forking childs for (my $count = 0; $count < 2; $count++) { defined (my $pid = fork) or die "Cant fork: $!"; } if ($$ == $parent) { # Master process while (1) { print "parent $$\n"; close WRITER; chomp (my $line = <READER>); print "<<<<<<<<<<<<<<<<<<<$line\n"; close READER; sleep 2; } } else { # Fork process while (1) { print "\tchild $$\n"; close READER; print WRITER "$$"; close WRITER; sleep 1; } exit 0; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting messages from child processes via pipes
by 1nickt (Canon) on Dec 31, 2017 at 13:37 UTC | |
|
Re: Getting messages from child processes via pipes
by tybalt89 (Monsignor) on Dec 31, 2017 at 18:18 UTC | |
by Frizoker (Initiate) on Jan 01, 2018 at 17:14 UTC | |
|
Re: Getting messages from child processes via pipes (using mighty MCE)
by 1nickt (Canon) on Dec 31, 2017 at 14:26 UTC | |
|
Re: Getting messages from child processes via pipes
by Anonymous Monk on Jan 01, 2018 at 13:30 UTC |