#!/usr/bin/perl -w use strict; use Data::Dumper; use Parallel::ForkManager; my %sess_hist = (); # Session history hash my %hash = (); # session flow information my $session; # ID of current session my $hist_file = '~/hist.dat'; # Session history file ## Check for sessions that are due/overdue and run them for (keys %{$hash{Session}}) { $session = $_; my $pid = $pm->start($session) and next; if (&check_overdue) { if (open SESSLOCK, '>', "~/$session.l") { my $stime = time; flock SESSLOCK, 2; &parse_hist; if ($stime > $sess_hist{$session}{last}) { &run_session; } else { logit "Aborting to avoid simultaneous sessions"; } close SESSLOCK; unlink "~/$session.l"; } else { logit "Can't open session lock file, aborting"; } } exit(0); $pm->finish($session); } $pm->wait_all_children; unlink "$hist_file.l"; sub parse_hist { # Opens hist file and pulls info into %sess_hist if (-s $hist_file) { # If the history file has data, local $/ = undef; # set slurp mode, open HFL, '>', "$hist_file.l"; # History File Lock flock HFL, 2; # establish an exclusive lock, %sess_hist = %{do($hist_file)}; # and load hist file as %sess_hist close HFL; } } sub merge_hist_changes { # Create copy of current session data my %temp_hash = %{$sess_hist{$session}}; # Reload hist file after establishing lock on .l file open HFL, '>', "$hist_file.l"; flock HFL, 2; local $/ = undef; %sess_hist = %{do($hist_file)} if -s $hist_file; # Change reference to point to copy made earlier $sess_hist{$session} = \%temp_hash; # Dump it local $Data::Dumper::Indent = 1; open HF, '>', $hist_file; print HF Dumper \%sess_hist; close HF; close HFL; # Release lock and let next child update $hist_file }