Limits max forks at one time.
#!/usr/bin/perl # https://perlmonks.org/?node_id=1228560 use strict; use warnings; use Time::HiRes qw( sleep ); my $maxforks = 5; # at any one time my $active = 0; for my $param ( 'A' .. 'M' ) { $active -= wait > 0 while $active >= $maxforks; if( my $pid = fork ) { $active++; print "active forks: $active\n"; } else { create_file($param); defined $pid and exit; } } $active -= wait > 0 while $active; sub create_file { sleep 0.001 + rand 1; my $parameter = shift; warn "pid $$ create_file('$parameter')\n"; # do somethng }
In reply to Re: Learning to use fork()
by tybalt89
in thread Learning to use fork()
by ovedpo15
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |