#!/usr/bin/perl use 5.010; use strict; use warnings; use autodie; use POSIX ':sys_wait_h'; $SIG{CHLD} = sub {1;}; my $start = time; my %children; foreach (1 .. 10) { if (my $pid = fork) { $children{$pid} = 1; } else { my $diff = time - $start; say "$diff: Child $$"; sleep rand 10; $diff = time - $start; say "$diff: Child $$ out of here"; exit; } } while (1) { foreach my $pid (keys %children) { if (my $r = waitpid($pid, WNOHANG)) { delete $children{$pid}; my $diff = time - $start; say $r == $pid ? "$diff: Reaped $pid" : "$diff: $pid gone! +"; } } last unless %children; my $diff = time - $start; say "$diff: Sleeping"; sleep 1; } my $diff = time - $start; say "$diff: End"; __END__ 0: Child 3975 0: Child 3976 0: Child 3976 out of here 0: Child 3977 0: Child 3978 0: Child 3979 0: Child 3980 0: Child 3981 0: Child 3982 0: Child 3983 0: Child 3984 0: Reaped 3976 0: Sleeping 1: Child 3977 out of here 1: Reaped 3977 1: Sleeping 2: Child 3979 out of here 2: Child 3980 out of here 2: Reaped 3979 2: Sleeping 2: Reaped 3980 2: Sleeping 3: Child 3982 out of here 3: Reaped 3982 3: Sleeping 4: Child 3978 out of here 4: Reaped 3978 4: Sleeping 4: Child 3981 out of here 4: Reaped 3981 4: Sleeping 4: Child 3984 out of here 4: Reaped 3984 4: Sleeping 5: Child 3983 out of here 5: Reaped 3983 5: Sleeping 8: Child 3975 out of here 8: Reaped 3975 8: End
In reply to Re: subroutines which forks?
by JavaFan
in thread subroutines which forks?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |