socketpair - problem communicating to multiple subprocesses

socketpair - problem communicating to multiple subprocesses

am 05.06.2011 10:42:17 von Honza Mach

--=-9Rqsd2D77mEbRjoAjIGj
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi everybody,

I am currently working on a project and I need parent process to fork
multiple children to do the actual work, but maintain a bidirectional
communication with each of the children to send them commands and
receive back, agregate and display summary results.=20

According to the perlipc tutorial and other resources, the best in this
case is to use either pair of unidirectional pipes, or use anonymous
sockets created by socketpair function. I chose socketpair, but I
discovered very weird behaviour and could not find any workaround so
far. Consider the following code:

#!/usr/bin/perl -w

use strict;
use warnings;
use Socket;
use IO::Handle;
use POSIX 'WNOHANG';

my $kids =3D 0; # Number of kids alive
my %kids =3D (); # kid PID =3D> Socket
my ($child, $parent);

foreach my $i (1..3)
{
socketpair($child, $parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
or die "socketpair: $!";
#pipe($parent, $child);
$child->autoflush(1);
$parent->autoflush(1);
print "Created child socket: ". fileno($child) . "\n";
print "Created parent socket: ". fileno($parent) . "\n";

die "cannot fork: $!" unless defined(my $pid =3D fork);
unless ($pid)
{
close $child;
do_child($parent);
exit(0);
}
else {
close $parent;
$kids++;
$kids{$pid} =3D $child;
}
}

$SIG{INT} =3D \&HUNTER;
$SIG{CHLD} =3D \&REAPER;

foreach my $kid (keys %kids)
{
print "Sending message to the kid $kid\n";
print {$kids{$kid}} "Parent Pid $$ is sending this\n";
}

map { close $_ } values(%kids);
sleep while($kids);

sub do_child {
my $parent =3D shift;
print "Child $$\n";
chomp(my $line =3D <$parent>);
print "Child Pid $$ just read this:
'".(defined($line)?$line:'undef')."'\n";
close $parent;
}

sub HUNTER {
print "Killing children\n";
my $count =3D kill INT =3D> keys(%kids);
print "Killed $count children\n";
}

sub REAPER {
while ((my $kid =3D waitpid(-1,WNOHANG)) > 0)
{
$kids--;
print "Reaped kid $kid\n";
}
unless ($kids)
{
print "Parent exit\n";
exit(0);
}
}

This is the working, but simplified example. For simplicity, in this
snippet I am trying just to write from parent process to all children,
not in both directions. In the parent process, I create socket pair in
loop, fork and store the handle to communicate with the newly created
child in the hash. I then try to send message to all stored sockets (to
all children). If I run this example, I get the following output:

Created child socket: 3
Created parent socket: 4
Child 4230
Created child socket: 3
Created parent socket: 4
Use of uninitialized value $line in chomp at ./test_bidirectional.pl
line 52.
Child Pid 4230 just read this: 'undef'
Created child socket: 3
Created parent socket: 4
Sending message to the kid 4232
Sending message to the kid 4231
Sending message to the kid 4230
Child 4231
Use of uninitialized value $line in chomp at ./test_bidirectional.pl
line 52.
Child Pid 4231 just read this: 'undef'
Child 4232
Child Pid 4232 just read this: 'Parent Pid 4229 is sending this'
Reaped kid 4230
Reaped kid 4231
Reaped kid 4232
Parent exit

The socketpair() function called in loop returns the same pair of
sockets each call. So all children are reading from the same socket! Is
this the correct behavior? I would expect, that the socketpair function
creates and returns new pair of sockets each call. I need to have
different pair of sockets to communicate with each child, otherwise they
are competing for the incoming data and only one receves them. In the
code is commented pipe() function. If you comment the createsocketpair
and uncomment the pipe function, result is the same. Even the pipe
function does not create new pair of handles each call.

Does anyone know, what am I doing wrong, what am I missing? What is the
correct way to establish communication with multiple subprocesses? I
really need to get this working somehow. If needed, I will gladly
provide more information.

Thanks in advance for your time

Regards

Honza Mach

P.S. My system information:

Perl: v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
WS: Linux Mint 10, Linux 2.6.35-28-generic x86_64 GNU/Linux

--=-9Rqsd2D77mEbRjoAjIGj
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Disposition: attachment; filename="smime.p7s"
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIW4TCCBGYw
ggNOoAMCAQICEFEmCpMc4n+cw6VfeeByroIwDQYJKoZIhvcNAQEFBQAwgZMx CzAJBgNVBAYTAlVT
MQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2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S/95
WytTUOsFjc0NjQZurt7+dJVOgus/+ifuNmQ5Wz1XSeDZtz4v4PQNLZCJ2HQ0 mUp+Lo9OWu/nJzB8
p+83lzOQdUpkUizFsL/jQESZL8rF8fcK+45RPRt+iS/gvMLoN8spkb2Lp+sL ySmnVjrfd9Dpd/SH
UA3TgozLVZlt03AH7GmGr1RRC7c4ZER8LwEAAAAAAAA=


--=-9Rqsd2D77mEbRjoAjIGj--

Re: socketpair - problem communicating to multiple subprocesses

am 05.06.2011 19:09:16 von Uri Guttman

>>>>> "HM" == Honza Mach writes:

HM> my ($child, $parent);

HM> foreach my $i (1..3)
HM> {
HM> socketpair($child, $parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
HM> or die "socketpair: $!";

you are using the same variables to store each socketpair in the
loop. so that will close the previous sockets and open up new ones. you
need to keep them in a hash like you do with the pids (in the
parent). then you can communicate with a unique socket to each child. of
course in the child you can keep using these vars as they only need to
track the single socket to the parent.

and there are modules that do most of this for you. see if one of them
is right for you.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: socketpair - problem communicating to multiple subprocesses

am 05.06.2011 19:36:52 von rvtol+usenet

On 2011-06-05 10:42, Honza Mach wrote:

> I am currently working on a project and I need parent process to fork
> multiple children to do the actual work, but maintain a bidirectional
> communication with each of the children to send them commands and
> receive back, aggregate and display summary results.

That is a common design flaw.

Try to redesign like this:
- start each child with all it needs to know
- let each child insert its results in for example a database
- create a new child for each decent chunk of work
- let the parent be idle most of the time
- limit the number of concurrent children to (for example) four
- let the parent merge the results after the children have finished

--
Regards, Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: socketpair - problem communicating to multiple subprocesses

am 06.06.2011 12:35:19 von Honza Mach

--=-9iuo8rPS3CRfSs4jjS/J
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thank you for your help.

Regards

Honza Mach

On Sun, 2011-06-05 at 13:09 -0400, Uri Guttman wrote:
> >>>>> "HM" == Honza Mach writes:
>=20
> HM> my ($child, $parent);
>=20
> HM> foreach my $i (1..3)
> HM> {
> HM> socketpair($child, $parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
> HM> or die "socketpair: $!";
>=20
> you are using the same variables to store each socketpair in the
> loop. so that will close the previous sockets and open up new ones. you
> need to keep them in a hash like you do with the pids (in the
> parent). then you can communicate with a unique socket to each child. of
> course in the child you can keep using these vars as they only need to
> track the single socket to the parent.
>=20
> and there are modules that do most of this for you. see if one of them
> is right for you.
>=20
> uri
>=20


--=-9iuo8rPS3CRfSs4jjS/J
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Disposition: attachment; filename="smime.p7s"
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIW4TCCBGYw
ggNOoAMCAQICEFEmCpMc4n+cw6VfeeByroIwDQYJKoZIhvcNAQEFBQAwgZMx CzAJBgNVBAYTAlVT
MQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtlIENpdHkxHjAcBgNV BAoTFVRoZSBVU0VS
VFJVU1QgTmV0d29yazEhMB8GA1UECxMYaHR0cDovL3d3dy51c2VydHJ1c3Qu Y29tMRswGQYDVQQD
ExJVVE4gLSBEQVRBQ29ycCBTR0MwHhcNMDUwNjA3MDgwOTEwWhcNMTkwNjI0 MTkwNjMwWjBvMQsw
CQYDVQQGEwJTRTEUMBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFk ZFRydXN0IEV4dGVy
bmFsIFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZG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