in reply to A question of fork efficiency

Here is my go, I like this blog entry about arguments in functions: https://www.matheus.ro/2018/01/29/clean-code-avoid-many-arguments-functions/

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use IO::Socket::INET; sub portCheck { my ($server,$port) = @_; my $sock = IO::Socket::INET->new( PeerAddr => $server, Timeout => 1, PeerPort => $port, Proto => 'tcp', ) || return 0; return 1; } my @checks = ( [ 'perlmonks.com',80 ], [ 'perlmonks.org',80 ], [ 'perlmonks.org',25], ); my @kids; for (@checks) { my $pid = fork // die; if ($pid) { push @kids,$pid; next; } exit portCheck(@$_); } for (0..$#kids) { print "@{$checks[$_]}\n"; waitpid($kids[$_],0); print $? >> 8 ,"\n"; }

Replies are listed 'Best First'.
Re^2: A question of fork efficiency
by synless (Acolyte) on Aug 06, 2019 at 14:02 UTC

    Thanks for the reply. I'll test that against what I have and check out that article soon. Also note there are quite a few arguments as I moved that function into a module so it needs to be passed arguments that use to be global vars inherited on the command line.

Re^2: A question of fork efficiency
by synless (Acolyte) on Aug 06, 2019 at 19:01 UTC

    I tested your version against mine and it is around 8 seconds faster than my current version. Though tweaking your version to give similar output may negate some of that. This is the time output running against 7052 servers:

    real 0m58.968s user 0m50.566s sys 1m3.864s