Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typo in perlipc man page, and suggestions for same #7226

Closed
p5pRT opened this issue Apr 11, 2004 · 4 comments
Closed

Typo in perlipc man page, and suggestions for same #7226

p5pRT opened this issue Apr 11, 2004 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Apr 11, 2004

Migrated from rt.perl.org#28456 (status was 'resolved')

Searchable as RT28456$

@p5pRT
Copy link
Author

p5pRT commented Apr 11, 2004

From axel@uni-paderborn.de

Hi,

thanks a lot for the perlipc man page, it's a great overview. There's a
tiny typo in the example demonstrating SysV message queues​: $type should
be $type_sent. (This is the perlipc man page that comes with perl 5.8.2).

I have a suggestion​: maybe you should also cover file locking with
flock() in this man page, since writing to files is a trivial but
effective method of interprocess communication. I think the following
fact is worth mentioning, because it is buried somewhere deep in unix
man pages​: if you want to do flock in processes that have been forked
from a parent process, then the child processes need to open the file
themselves; it is not sufficient to inherit a file descriptor from the
parent (flock will be ineffective and fail without error message or
warning, at least on Linux).

Finally, a nice way to communicate among processes is to create in
effect a shared hash variable by tying it to a database via the
BerkeleyDB module. I have attached an example.

Cheers,
  Axel

use BerkeleyDB;
use Fcntl '​:flock';
my $env = new BerkeleyDB​::Env -Home => '/tmp/db/', -Flags =>
DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL;
if (!$env) {
  die "could not create db environment.\n";
}
for my $num (1..100) {
  my $pid = fork();
  if (!defined $pid) {
  print "fork $num failed.\n";
  next;
  }
  if (!$pid) {
  #the following two commands have to be given in the child, not in
the parent!
  open(DB,"/tmp/db/test.db");
  my $db = tie my %hash, 'BerkeleyDB​::Hash', -Filename => "test.db",
-Flags => DB_CREATE, -Env => $env;
  if (!defined $db) {
  die "db not defined​: $!\n";
  }

  flock(DB,LOCK_EX); #necessary, or else the various children could
interleave
  $hash{1}++;
  flock(DB,LOCK_UN);
  undef $db;
  untie %hash;
  close(DB);
  exit;
  }
}
#parent waiting for children​:
while ((my $pid = wait()) != -1) {}
my $db = tie my %hash, 'BerkeleyDB​::Hash', -Filename => "test.db", -Env
=> $env;
if (!defined $db) {
  die "db not defined​: $!\n";
}
print $hash{1} . "\n"; #Will print 100, then 200 on the second run, then
300,...
undef $db;
untie %hash;

@p5pRT
Copy link
Author

p5pRT commented Apr 21, 2004

From @nwc10

On Sun, Apr 11, 2004 at 01​:08​:17PM -0000, Axel Boldt wrote​:

thanks a lot for the perlipc man page, it's a great overview. There's a
tiny typo in the example demonstrating SysV message queues​: $type should
be $type_sent. (This is the perlipc man page that comes with perl 5.8.2).

Thanks. Well spotted. It was still present in the current development tree.
Now fixed, and will be in perl 5.8.4

I'll leave others (or me when I have more time) to think about your other
useful suggestions.

Nicholas Clark

@p5pRT
Copy link
Author

p5pRT commented Apr 21, 2004

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Oct 7, 2004

@smpeters - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant