#!/usr/bin/perl use strict; use warnings; use Fcntl qw(:flock); my $testFile1 = "/tmp/test.txt"; my $lck = "/tmp/l.lck"; my $wantToQuit = 0; while (1) { while ( !$wantToQuit ) { $SIG{INT} = 'wait'; my $pid = fork(); if ( !defined( $pid ) ) { die("can't fork sub proc\n"); } if ( $pid == 0 ) { if ( ! -e $lck ) { print "\n\ntestFile1 does not exist, file READY for reading\n"; open(LCK, ">$lck") || die "Could not open $lck $!\n"; flock(LCK, LOCK_EX); open(FH, "+<$testFile1") || die "Could not open $testFile1 $!\n"; close(FH); close(LCK); print "$testFile1 file CREATED\n"; &test1; &test2; } else { print "$testFile1 exits, must wait til it's gone\n"; print "Sleeping for 10 seconds, then checking for lock file\n"; countDown(10); } $SIG{INT} = 'DEFAULT'; } waitpid($pid,0); } exit if $wantToQuit; } sub wait { print "\n\n\n===============WAIT SUB=================\n"; print "CTRL-C signal was been detected please hold while we finish running the script\n"; $wantToQuit = 1; print "\n\n\n===============END WAIT SUB=================\n"; } sub test1 { print "\n\n\n===============TEST1 SUB=================\n"; print "Sleeping for 3 seconds In sub test1\n"; countDown(3); print "===============END TEST1 SUB=================\n"; } sub test2 { print "\n\n\n===============TEST2 SUB=================\n"; print "Sleeping for 3 secs In sub Test2\n"; sleep 3; print "Not done yet gotta sleep for another 15 seconds then we can quit\n"; countDown(15); print "DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT\n"; print "===============END TEST2 SUB=================\n"; exit 0; } sub countDown { my $delay = shift; my $count = 0; while ( $count < $delay ) { print "$count\n"; sleep 1; $count++; } }