Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: fork(): terminating the child without killing the parent

by tybalt89 (Monsignor)
on Oct 26, 2016 at 04:02 UTC ( [id://1174755]=note: print w/replies, xml ) Need Help??


in reply to fork(): terminating the child without killing the parent

Here's a version that uses that SIGPIPE to restart a child ( with a new pipe ), but things had to be shuffled around a bit.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1174747 use strict; use warnings; $| = 1; my ($from_parent, $to_child ); while(1) { my $msg = "hello from $$\n"; print "pipe and fork, parent $$\n"; pipe( $from_parent, $to_child ) or die "$! on pipe"; if( my $pid = fork ) { # parent close $from_parent; my $more = 1; $SIG{HUP} = sub { $pid and kill 15, $pid }; $SIG{PIPE} = sub { $more = 0 }; while($more) { syswrite $to_child, $msg, length $msg; sleep 3; } } elsif( defined $pid ) { # child close $to_child; print "child $$ started\n"; while(1) { if( sysread $from_parent, my $buffer, 100 ) { print "in child $$: $buffer"; } } exit; } else { die "$! on fork"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174755]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found