Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
go ahead... be a heretic
 
PerlMonks  

forking with Storable and IPC::ShareLite

by andThenThereWasPERL (Monk)
on Feb 02, 2006 at 15:56 UTC ( #527357=perlquestion: print w/ replies, xml ) Need Help??
andThenThereWasPERL has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
Please help me understand why the following code does not give consistent output.
#!/usr/bin/perl use strict; use Parallel::ForkManager; use IPC::ShareLite; use Storable qw( freeze thaw ); my $fork_manager = new Parallel::ForkManager(5); my $share = new IPC::ShareLite( -key => 1971, -create => 'yes', -destroy => 'yes' ) or die $!; my $hash = {'PARENT' => $$}; $share->store( freeze( $hash ) ); foreach my $child ( 1 .. 10 ) { my $pid = $fork_manager->start($child) and next; $share->destroy( 0 ); my $hash = thaw( $share->fetch ); while (1) { if ($share->lock()) { last; } else { print "$$ cant lock share\n"; sleep 1; } }; for my $id (1 .. 10) { my $key = $child . '-' . $id; $$hash{$key} = qq{|Kid $child pushed $id}; } $share->store( freeze( $hash ) ); $share->unlock; $fork_manager->finish($child); } print "Waiting for Children...\n"; $fork_manager->wait_all_children; my %final_parent_hash = %{ thaw( $share->fetch ) }; foreach my $key ( sort{( (split /-/,$a)[0] <=> (split /-/,$b)[0] ) || ( (split /-/,$a)[1] <=> (split /-/,$b)[1] ) } keys %final_parent_hash ) { print "$key = $final_parent_hash{$key}\n"; } exit;
Waiting for Children... PARENT = 3661 1-1 = |Kid 1 pushed 1 1-2 = |Kid 1 pushed 2 1-3 = |Kid 1 pushed 3 1-4 = |Kid 1 pushed 4 1-5 = |Kid 1 pushed 5 1-6 = |Kid 1 pushed 6 1-7 = |Kid 1 pushed 7 1-8 = |Kid 1 pushed 8 1-9 = |Kid 1 pushed 9 1-10 = |Kid 1 pushed 10 3-1 = |Kid 3 pushed 1 3-2 = |Kid 3 pushed 2 3-3 = |Kid 3 pushed 3 3-4 = |Kid 3 pushed 4 3-5 = |Kid 3 pushed 5 3-6 = |Kid 3 pushed 6 3-7 = |Kid 3 pushed 7 3-8 = |Kid 3 pushed 8 3-9 = |Kid 3 pushed 9 3-10 = |Kid 3 pushed 10 5-1 = |Kid 5 pushed 1 5-2 = |Kid 5 pushed 2 5-3 = |Kid 5 pushed 3 5-4 = |Kid 5 pushed 4 5-5 = |Kid 5 pushed 5 5-6 = |Kid 5 pushed 6 5-7 = |Kid 5 pushed 7 5-8 = |Kid 5 pushed 8 5-9 = |Kid 5 pushed 9 5-10 = |Kid 5 pushed 10 6-1 = |Kid 6 pushed 1 6-2 = |Kid 6 pushed 2 6-3 = |Kid 6 pushed 3 6-4 = |Kid 6 pushed 4 6-5 = |Kid 6 pushed 5 6-6 = |Kid 6 pushed 6 6-7 = |Kid 6 pushed 7 6-8 = |Kid 6 pushed 8 6-9 = |Kid 6 pushed 9 6-10 = |Kid 6 pushed 10 8-1 = |Kid 8 pushed 1 8-2 = |Kid 8 pushed 2 8-3 = |Kid 8 pushed 3 8-4 = |Kid 8 pushed 4 8-5 = |Kid 8 pushed 5 8-6 = |Kid 8 pushed 6 8-7 = |Kid 8 pushed 7 8-8 = |Kid 8 pushed 8 8-9 = |Kid 8 pushed 9 8-10 = |Kid 8 pushed 10 10-1 = |Kid 10 pushed 1 10-2 = |Kid 10 pushed 2 10-3 = |Kid 10 pushed 3 10-4 = |Kid 10 pushed 4 10-5 = |Kid 10 pushed 5 10-6 = |Kid 10 pushed 6 10-7 = |Kid 10 pushed 7 10-8 = |Kid 10 pushed 8 10-9 = |Kid 10 pushed 9 10-10 = |Kid 10 pushed 10
where is the output of the second child.
Thanks,
Aveek

Comment on forking with Storable and IPC::ShareLite
Select or Download Code
Re: forking with Storable and IPC::ShareLite
by zentara (Archbishop) on Feb 02, 2006 at 16:21 UTC
    I get output from the second child on every run, but maybe I don't understand the problem.

    I'm not really a human, but I play one on earth. flash japh
      sorry if I was not clear, in each run the output one of the child is missing.
      e.g. in the run I have posted the output of 2nd child is missing
      Thanks
Re: forking with Storable and IPC::ShareLite
by zentara (Archbishop) on Feb 03, 2006 at 15:00 UTC
    I'm glad to see you got it working. I was going to mention, that in testing your first version with ShareLite, that it left shared memory segments intact after the program exited. These could be security holes.

    It's important in your testing to do an "ipcs" before and after you run your script; and remove any segments you created. You can remove them with ipcrm.

    The problem of "leftover" shared memory segments is probably one of the reasons people shy away from using shared memory. It certainly makes me leary of them.


    I'm not really a human, but I play one on earth. flash japh
      yup already using ipcs during test,
      thanks once again for the pointers :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://527357]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-04-19 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (477 votes), past polls