#!/usr/bin/perl -w use strict; if (!defined(my $pid = fork)) { print "Failed to fork.\n"; exit -1; } else { if ($pid == 0) { print "PID variable is zero; I am the child.\n"; # This is where we would execute the external process # Replaced this with a sleep loop to simulate a long-running process for (my $x = 0; $x < 10; $x++) { print "Sleeping iteration $x.\n"; sleep 1; } print "Child process has finished.\n"; exit 0; } else { print "I am the parent. Child PID is $pid.\n"; my $timer = 0; while ($timer < 5) { sleep 1; $timer++; } # After 5 seconds, kill the child print "Killing the child process.\n"; kill 1, $pid; print "Parent process finishing.\n"; } } print "End of the forked code block.\n";