use strict;
use warnings;
use Errno qw( EWOULDBLOCK ) ;
my $from_parent;
my $to_child;
my $pid;
my $mesg_parent = "Hello Child!";
my $mesg_child;
my $test;
pipe( $from_parent, $to_child );
if ( $pid = fork() ) {
# Parent
$SIG{ HUP } = sub{ kill( 15 , $pid ); print( "KILLED CHILD!!!\n" ) };
close( $from_parent );
while ( 1 ) {
eval{ $test = syswrite( $to_child, $mesg_parent, length( $mesg_parent ) ); };
select( undef, undef, undef, .5);
if ( $@ ) {
print( "trapped error when writing to pipe \$to_child_ssl\n" );
next;
}
if ( defined $test ) {
print( "Sent message \"$mesg_parent\" to child\n" );
}
}
}
else {
# Child
close( $to_child );
while ( 1 ) {
$test = sysread( $from_parent, $mesg_child, 13 );
select( undef, undef, undef, .5 );
if ( ! defined $test ) {
if ( $! == EWOULDBLOCK ) {
print( "sysread would be blocked by \$from_parent: $!\n" );
next;
}
else {
die ( "Error when trying to read from \$from_parent: $!\n" );
}
}
print( "In child\n" );
print( $mesg_child . "\n" );
$mesg_child =~ s/Child/Parent/;
print $mesg_child . "\n";
}
}
####
Sent message "Hello Child!" to child
In child
Hello Child!
Hello Parent!
Sent message "Hello Child!" to child
In child
Hello Child!
Hello Parent!
KILLED CHILD!!!
Sent message "Hello Child!" to child
Sent message "Hello Child!" to child
xyz@v32470:~/projekte/$
####
In child
Hello Child!
Hello Parent!
Terminated
xyz@v32470:~/projekte$ In child
Hello Child!
Hello Parent!
In child
In child
In child
In child
####
xyz@v32470:~$ kill 1 31055
-bash: kill: (1) - Operation not permitted