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

Re^5: System call doesn't work when there is a large amount of data in a hash (testing)

by marioroy (Prior)
on Apr 30, 2020 at 00:43 UTC ( [id://11116253]=note: print w/replies, xml ) Need Help??


in reply to Re^4: System call doesn't work when there is a large amount of data in a hash (testing)
in thread System call doesn't work when there is a large amount of data in a hash

Hi Nicolasd,

POSIX::RT::Spawn also works, a suggestion made here. Unfortunately, POSIX::RT::Spawn doesn't capture the error string (for example command not found). Well, two solutions this one using POSIX::RT::Spawn and the other using MCE::Child with MCE::Channel.

spawn:

The system command by the main process fails like before, but not syscmd calling spawn.

use strict; use warnings; use POSIX::RT::Spawn; sub syscmd { my $cmd = shift; return unless $cmd; local ($?, $!); my $pid = spawn $cmd, @_; waitpid $pid, 0; my ($status, $errmsg) = ($?, $!); if ($status == -1) { print "SYSTEM: failed to execute ($cmd): $errmsg\n"; } elsif ($status & 127) { printf "SYSTEM: $cmd died with signal %s, %s coredump\n", ($status & 127), ($status & 128) ? 'with' : 'without'; } else { printf "SYSTEM: $cmd exited with status %d\n", $status >> 8; } } # My CentOS VM has 4 GB of RAM # create big hash my $memory_eaten = 3 * 1024*1024*1024 / 2; # 3 GB, adjust to fit my %memory_eater = ( foo => scalar( ' ' x $memory_eaten ), ); # pass command and optionally args syscmd('ls'); # this one works; see status that it succeeded system('ls'); # this one fails; no ls output the 2nd time # attempt to run a command not found syscmd('something'); # sleep for 2 seconds syscmd('sleep', '2'); # busy loop, see top output in another terminal # notice the memory consumption (i.e. RES) # press Ctrl-C to exit or let it finish 1 for 1..3e8;

output:

ls output from spawn SYSTEM: ls exited with status 0 SYSTEM: something exited with status 127 SYSTEM: sleep exited with status 0

Regards, Mario

  • Comment on Re^5: System call doesn't work when there is a large amount of data in a hash (testing)
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-18 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found