in reply to Re^3: Shared memory and asynchronous access
in thread Shared memory and asynchronous access
Thank you, huck++.
Just completed testing on WIndows, Linux, and Mac OS. We have a UDP solution that works on multiple platforms. Fortunately, Perl provides $^X, giving us the path to the perl executable.
use strict; use warnings; use Fcntl qw( F_GETFL F_SETFL O_NONBLOCK ); use IO::Socket::INET qw( SOL_SOCKET SO_REUSEADDR ); ## POSIX.pm is a big module. This covers most platforms. use constant { _WNOHANG => $^O eq 'solaris' ? 64 : 1 }; my $port = 54321; my $socket = IO::Socket::INET->new( Proto => 'udp', LocalPort => $port, LocalAddr => '127.0.0.1' ); die "Unable to bind to 127.0.0.1:$port: $!\n" unless $socket; setsockopt( $socket, SOL_SOCKET, SO_REUSEADDR, 1 ); stop_blocking( $socket ); $SIG{CHLD} = 'IGNORE'; my $pid = fork; die "Could not fork: $!\n" unless ( defined $pid ); exec( $^X, 'monitored.pl', $port ) if ( $pid == 0 ); $| = 1; while ( waitpid($pid, _WNOHANG) == 0 ) { my $addr = recv( $socket, my $time, 1024, 0 ) or select '','','',0 +.015; printf "\rProgress: %s", $time; } print "\nAll done.\n"; sub stop_blocking { my $socket = shift; if ($^O eq 'MSWin32') { my $flag = 1; ioctl($socket, 0x8004667e, \$flag); } else { my $flags; $flags = fcntl($socket, F_GETFL, 0) or die "Could not getfl: $!\n"; $flags = fcntl($socket, F_SETFL, $flags | O_NONBLOCK) or die "Could not setfl: $!\n"; } }
Not changed from before, the monitored.pl script using IO::Socket::INET.
use strict; use warnings; use IO::Socket::INET qw( pack_sockaddr_in inet_aton ); my $port = shift || 54321; my $socket = IO::Socket::INET->new( Proto => 'udp' ); my $runtime = rand 10; $| = 1; for my $num ( 0 .. $runtime * 100 ) { send ( $socket, sprintf("%.f%% ", $num / $runtime), 0, pack_sockaddr_in($port, inet_aton('127.0.0.1')) ) or die "send: $^E"; select '', '', '', 0.015; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Shared memory and asynchronous access
by huck (Prior) on Apr 06, 2017 at 18:52 UTC | |
by Anonymous Monk on Apr 06, 2017 at 19:00 UTC | |
|
Re^5: Shared memory and asynchronous access
by Anonymous Monk on Apr 06, 2017 at 18:10 UTC |