#!/usr/bin/perl -w #preload.pl use IPC::Shareable; my $newvalue; tie $newvalue,'IPC::Shareable','kali',{create => 1,size => 65536}; $newvalue=0; #### #!/usr/bin/perl -w #ipcprocesses.pm. #you may find some of the variables are not defined here #that is fine for demo purpose. package ipcprocesses; use POSIX; use IPC::Shareable; my $newvalue; my $num_children = 0; sub new { my $class = shift; my $this = { 'field' => 1 }; bless ($this,$class); tie $newvalue,'IPC::Shareable','kali'; return ($this); } $SIG{CHLD} = \&DO_AirGREAPER; $SIG{INT} = $SIG{TERM} = \&DO_AirGStopHandler; $SIG{ALRM} = sub {}; sub DO_AirGStopHandler { local( $SIG{CHLD} ) = 'IGNORE'; kill 'INT' => keys %H_children; exit; } sub DO_AirGREAPER { # takes care of dead children my ( $N_pid ); # If a second child dies while in the signal handler #caused by the # first death, we won't get another signal. So must # loop here else # we will leave the unreaped child as a zombie. And the # next time # two children die we get another zombie. And so on. while ( ($N_pid = waitpid(-1, &WNOHANG)) > 0 ) { if (WIFEXITED $?){ $num_children--; } } $SIG{CHLD} = \&DO_AirGREAPER; } sub DO_Initprocesspool { my $this = shift; # Fork off our children. for ( 1 .. 2 ) { $this->DO_newchild(); } } sub DO_newchild { my $this = shift; if ($pid = fork) { #parent $this->{'field'} = $newvalue; $num_children++; print 'parent'.$$.'|'.$this->{'field'}."\n"; return; } else{ my $newvalue; tie $newvalue,"IPC::Shareable",'kali'; #Children can not return from this subroutine $SIG{INT} = 'DEFAULT'; #make SIGINT kill us as it did before while(1) { sleep 2; print 'child'.$$.'|'.$this->{'field'}."\n"; $newvalue = time(); } } exit; } sub DO_MaintainPopulation { my $this = shift; for ( my $i = $num_children; $i < 2; $i++ ) { # top up the child pool $this->DO_newchild(); } } 1; #### #!/usr/bin/perl -w #ipcprocesses.pl use strict; use lib qw (/somepath); use ipcprocesses; my $fork = ipcprocesses->new(); while ( 1 ) { $fork->DO_MaintainPopulation(); }