#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper;
print "".localtime(),"\n";
my $pid = fork;
die "fork failed\n" if !defined $pid;
if ($pid == 0) { # child
print "child is $$\n";
exec "/bin/sleep", "4";
die "exec failed:$!\n";
}
else { # parent
# there is no code in the parent to kill the child if the alarm is called
$SIG{ALRM} = sub {
print( "Alarm triggered, making system call in $$\n" );
unlink('/doesnt/exist'); # this will definitely fail
};
alarm(2);
my $pid = wait;
my $status = $?;
print "return value from child is $pid and status was $status\n";
}
print "".localtime(),"\n";
####
Sat Sep 29 09:04:00 2007
child is 26171
Alarm triggered, making system call in 26170
return value from child is -1 and status was -1
Sat Sep 29 09:04:02 2007
##
##
#include
#include
#include
#include
#include
void alarm_sig(int sig)
{
int rt;
puts("in alarm_sig");
if ((rt = unlink("/doesnt/exist")) == -1) {
perror("unlink failed");
}
}
int main()
{
int rt;
signal(SIGALRM, alarm_sig);
alarm(2);
rt = system("sleep 4");
printf("return value from system %d\n", rt);
}
##
##
in alarm_sig
unlink failed: No such file or directory
return value from system 0