#!/usr/bin/perl # sharetest - test shared variables across forks use strict; use warnings; use IPC::Shareable; my %buffer; my $handle = tie %buffer, 'IPC::Shareable', undef, { destroy => 1 }; $buffer{test} = "inoangrnglkdtghnlkhnl"; $buffer{test1} = "inoangrnglkdtghnlkhnl"; $buffer{test2} = "inoangrnglkdtghnlkhnl"; $buffer{test3} = "inoangrnglkdtghnlkhnl"; $buffer{test4} = "inoangrnglkdtghnlkhnl"; $SIG{INT} = sub { die "$$ dying\n" }; my @kids; for ( 1 .. 10 ) { my $child; unless ( $child = fork ) { # i'm the child die "cannot fork: $!" unless defined $child; squabble(); exit; } push @kids, $child; # in case we care about their pids } while (1) { print "Buffer is $buffer{test}\n"; sleep 1; } die "Not reached"; sub squabble { my $i = 0; while (1) { next if $buffer{test} =~ /^$$\b/o; $handle->shlock(); $i++; $buffer{test} = "$$ $i"; $handle->shunlock(); } }