#!/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++;
}
}
####
Currently in pid==0 section, sleeping for 5 seconds
0
1
2
3
4
===============TEST1 SUB=================
Sleeping for 3 seconds In sub test1
0
1
2
===============END TEST1 SUB=================
===============TEST2 SUB=================
Sleeping for 3 secs In sub Test2
Not done yet gotta sleep for another 15 seconds then we can quit
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT
####
Currently in pid==0 section, sleeping for 5 seconds
0
1
===============WAIT SUB=================
One moment, I just gotta finish this bit...
===============END WAIT SUB=================
2
===============WAIT SUB=================
One moment, I just gotta finish this bit...
===============END WAIT SUB=================
3
4
===============TEST1 SUB=================
Sleeping for 3 seconds In sub test1
0
1
2
===============END TEST1 SUB=================
===============TEST2 SUB=================
Sleeping for 3 secs In sub Test2
Not done yet gotta sleep for another 15 seconds then we can quit
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT
===============END TEST2 SUB=================