I wrote a Perl program which forks and calls a background program in the child process and has a endless while loop which does some stuff in the parent process. Now the while loop should be left when the background program in the child process terminates:
$pid = fork();
if ( !$pid ) {
exec( "program args" );
} else {
while ( 1 ) {
# do something
# needs help: if program terminated, break
}
}
Well,
forkgives your parent process the child’s PID (which you dutifully retrieve in your code). This is precisely so the parent can look after the child.To leave the loop when it terminates, you can use
kill 0 $pidto check whether the child still exists. Seeperldoc -f kill.