#!/usr/bin/perl use strict; use warnings; use POSIX ":sys_wait_h"; pipe PREAD, CWRITE; # child -> parent my $parent_pid = $$; if (my $pid = fork()) { parent($pid); } else { child(); } sub child { close PREAD; # child - close parent end of pipe print "child: id $$ dad $parent_pid\n"; print "child: start of proccess\n"; syswrite CWRITE, "0\n"; for (1..9) {print "child zZZZzzz\n";sleep 1;} print "child is awaking\n"; syswrite CWRITE, "$_\n" for 1 .. 10; print "child ending\n"; exit; } # sub child sub parent { my ($pid) = @_; close CWRITE; # parent - close child end of pipes print "parent id $$ child $pid\n"; local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required my @erg; eval { alarm 15; while (waitpid($pid, WNOHANG) == 0) { print "parent: kind lebt noch\n"; sleep 1; } alarm 0; }; if ($@) { die "other error : $@" unless $@ eq "alarm\n"; # propagate unexpected errors warn "parent : child got time out!\n"; # ... Maßnahmen ergreifen um klarzumachen, dass die Verarbeitung # unvollständig ist ... } else { alarm 0; # Ist das wirklich notwendig? chomp(@erg = ); print "parent got ", join(' - ', @erg), "\n"; print "parent : ", (eof(PREAD)?'ENDE':'keine Ende'), "\n"; } kill TERM => $pid; # Kind abschießen (in beiden Fällen, einmal als Zombie, # einmal läuft es noch und verbraucht Rechenzeit) } # sub parent