#!/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;