#!/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} = 'waitThenExit'; my $pid = fork(); if ( !defined( $pid ) ) { die("can't fork sub proc\n"); } if ( $pid == 0 ) { print "Currently in pid==0 section, sleeping for 5 seconds\n"; countDown(5); &test1; &test2; $SIG{INT} = 'DEFAULT'; } waitpid($pid,0); } exit 0 if $wantToQuit; } sub waitThenExit { print "\n\n\n===============WAIT SUB=================\n"; print "One moment, I just gotta finish this bit...\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++; } }