Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Resizing IPC::Msg queue size

by Jeppe (Monk)
on Jul 08, 2004 at 09:38 UTC ( #372723=note: print w/replies, xml ) Need Help??

in reply to Re: Resizing IPC::Msg queue size
in thread Resizing IPC::Msg queue size

Unfortunately, it fails. Given the code below, I get
[jps@secanaws jps]$ perl Can't call method "pack" on an undefined value at /usr/lib/perl5/5.6.1 +/i386-linux/IPC/ line 78.
If I remove the set call, everything seems to work except the queue being too small. What am I missing?
#!/usr/bin/perl use IPC::SysV qw(IPC_PRIVATE IPC_CREAT IPC_NOWAIT); use IPC::Msg; use Data::Dumper; use strict; use warnings; my $msgtype = 1; my $message = "hello nr "; my $messages = 2000; my $buf; my $msg = new IPC::Msg('24h', IPC_CREAT); $msg->set('qbytes' => 32768); for (my $i = 0; $i < $messages; $i++) { my $rv = $msg->snd($msgtype, pack("L a*",$msgtype,$message . $i), IPC_NOWAIT); print "rv of $i is $rv\n" } while ($msg->rcv($buf,256, $msgtype, IPC_NOWAIT)) { print "$buf\n"; }

Replies are listed 'Best First'.
Re^3: Resizing IPC::Msg queue size
by hv (Parson) on Jul 08, 2004 at 12:50 UTC

    Looking at the code near the line on which the error was reported, we find this set method in 5.6.1:

    # IPC/ lines 61-79 sub set { my $self = shift; my $ds; if(@_ == 1) { $ds = shift; } else { croak 'Bad arg count' if @_ % 2; my %arg = @_; my $ds = $self->stat or return undef; my($key,$val); $ds->$key($val) while(($key,$val) = each %arg); } msgctl($$self,IPC_SET,$ds->pack); }

    There is a bug in this code: on line 11 (which is line 71 of the original file), my $ds creates a new $ds variable which goes out of scope at the end of the else block. That is why the final line has an undefined value for $ds when it tries to invoke $ds->pack.

    Removing the redundant "my" will fix that problem by ensuring that (as intended) the outer $ds variable is the one used, and indeed in later perls you'll find that exactly this change has been made: replacing

    my $ds = $self->stat
    $ds = $self->stat
    at line 71.

    If you don't have permission to make this fix to the code directly, and you can't convince the system administrator to apply the fix for you, there are a couple of ways you can get around it. One way is subclassing to replace the buggy routine:

    { package IPC::Msg::Bugfix; our @ISA = qw/ IPC::Msg /; sub set { # corrected version of set here ... } } # in your code my $msg = new IPC::Msg::Bugfix('24h', IPC_CREAT); # and continue as before

    An alternative is to recode to avoid the path with the bug in it:

    # avoiding buggy multiple-arg set() # $msg->set('qbytes' => 32768); my $ds = $msg->stat or die "stat: $!"; $ds->qbytes(32768); $msg->set($ds);

    (Please note, I haven't tested any of this code.)


      Thanks, Hugo!!

      On a side-note, I also needed to do this:

      echo "65536" > /proc/sys/kernel/msgmnb
      Thanks to all you helpful monks!!
Re^3: Resizing IPC::Msg queue size
by gellyfish (Monsignor) on Jul 08, 2004 at 10:03 UTC

    Hmm, that appears to work fine with 5.8.1 - admittedly the program doesn't do what I think it should though.


      Well - I'm still on 5.6.1 and don't have the liberty of moving. The documentation says it should work. But it just doesn't work.

      I'm on RHEL AS 2.1 - pretty much a glorified and tuned Red Hat Linux 7.3..

      The program should create a message queue for the key "24h" with 2000 entries, and then empty it.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2022-01-21 09:13 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (57 votes). Check out past polls.