Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Getting messages from child processes via pipes

by Frizoker (Initiate)
on Dec 31, 2017 at 13:08 UTC ( #1206479=perlquestion: print w/replies, xml ) Need Help??
Frizoker has asked for the wisdom of the Perl Monks concerning the following question:

Greetings!

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 (Monsignor) on Dec 31, 2017 at 13:37 UTC

    Hi, one thing not to do is to disable strict and warnings!

    After reenabling:

    parent: 12158 parent 12158 child 12159 child 12160 child 12161 <<<<<<<<<<<<<<<<<<<121591216012161 child 12160 child 12159 child 12161 print() on closed filehandle WRITER at 1206479.pl line 29. print() on closed filehandle WRITER at 1206479.pl line 29. print() on closed filehandle WRITER at 1206479.pl line 29. parent 12158 readline() on closed filehandle READER at 1206479.pl line 20. child 12160 Use of uninitialized value $line in chomp at 1206479.pl line 20. print() on closed filehandle WRITER at 1206479.pl line 29. Use of uninitialized value $line in concatenation (.) or string at 120 +6479.pl line 21. <<<<<<<<<<<<<<<<<<<

    Fix the errors, don't silence the warnings!


    The way forward always starts with a minimal test.
Re: Getting messages from child processes via pipes
by tybalt89 (Priest) on Dec 31, 2017 at 18:18 UTC

    Here a tweak, there a tweak, everywhere a code tweak :)

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1206479 use strict; use warnings; pipe (my $reader, my $writer); $writer->autoflush(1); my $parent=$$; print "parent: $parent\n"; for (0..2) # Forking children { defined fork or die "Cant fork: $!"; } if($$ == $parent) # Master process { close $writer; print "parent $$\n"; while (1) { my $line = <$reader>; print "<<<<<<<<<<<<<<<<<<< $line"; } } else # Fork process { close $reader; while(1) { print "\tchild ", $$ - $parent, "\n"; print $writer $$ - $parent, "\n"; sleep 1; } exit 0; }

    Outputs

    parent: 4030 parent 4030 child 1 <<<<<<<<<<<<<<<<<<< 1 child 2 <<<<<<<<<<<<<<<<<<< 2 child 5 <<<<<<<<<<<<<<<<<<< 5 child 4 <<<<<<<<<<<<<<<<<<< 4 child 3 <<<<<<<<<<<<<<<<<<< 3 child 7 child 6 <<<<<<<<<<<<<<<<<<< 7 <<<<<<<<<<<<<<<<<<< 6 child 1 <<<<<<<<<<<<<<<<<<< 1 child 5 <<<<<<<<<<<<<<<<<<< 5 child 4 <<<<<<<<<<<<<<<<<<< 4 child 2 <<<<<<<<<<<<<<<<<<< 2 child 3 <<<<<<<<<<<<<<<<<<< 3 child 7 <<<<<<<<<<<<<<<<<<< 7 child 6 <<<<<<<<<<<<<<<<<<< 6 child 1 <<<<<<<<<<<<<<<<<<< 1 child 4 <<<<<<<<<<<<<<<<<<< 4 child 5 child 7 <<<<<<<<<<<<<<<<<<< 5 child 3 child 2 <<<<<<<<<<<<<<<<<<< 7 <<<<<<<<<<<<<<<<<<< 3 child 6 <<<<<<<<<<<<<<<<<<< 6 <<<<<<<<<<<<<<<<<<< 2
      Dear monks,

      many thanks! Especially thanks to tybalt89! Now it is as simple as it should be and works correctly.

      Best regards!

Re: Getting messages from child processes via pipes (using mighty MCE)
by 1nickt (Monsignor) on Dec 31, 2017 at 14:26 UTC

    Hi, here is a demonstration using MCE by marioroy. We are using MCE::Shared to implement shared data between child processes. We are also using MCE::Flow to manage the child processes, but you could use native fork() or any other technique.

    use strict; use warnings; use feature 'say'; use MCE::Shared; use MCE::Flow; tie my $foo, 'MCE::Shared', 0; tie my @bar, 'MCE::Shared'; tie my %baz, 'MCE::Shared'; my $mutex = MCE::Mutex->new; mce_flow { max_workers => 4 }, sub { my ( $mce ) = @_; my ( $pid, $wid ) = ( MCE->pid, MCE->wid ); # Locking is necessary when multiple workers update the same eleme +nt $mutex->enter( sub { $foo += 1 } ); # Otherwise, locking is optional for unique elements. $bar[ $wid - 1 ] = $pid; $baz{ $pid } = $wid; return; }; say "scalar :"; say " count is now $foo"; say "array :"; say " pid $_ was here" for @bar; say "hash :"; say " worker $baz{ $_ } had pid $_" for sort keys %baz; __END__
    Output:
    scalar : count is now 4 array : pid 13826 was here pid 13827 was here pid 13828 was here pid 13829 was here hash : worker 1 had pid 13826 worker 2 had pid 13827 worker 3 had pid 13828 worker 4 had pid 13829

    See also: MCE Cookbook, MCE Examples.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Getting messages from child processes via pipes
by Anonymous Monk on Jan 01, 2018 at 13:30 UTC

    Regarding your question whether it is possible to get by with a single pipe— read the documentation that applies to your platform. On linux, refer to man 7 pipe.

    You need atomic writes so that the messages don't intermingle. This may be guaranteed, provided (a) your OS adheres to POSIX or otherwise makes the same allowances, and (b) the messages are smaller than some fixed limit (such as 512).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1206479]
Approved by Athanasius
Front-paged by haukex
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-06-24 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?